Skip to main content
  1. 笔记本/

Lighttpd 配置 HTTPS 访问

1 min

Lighttpd 来自"light"和“httpd"的合成,它是一个简单、符合标准、安全且灵活的 web 服务程序,它也是轻量级和开源的,以其低内存占用和高并发处理能力而闻名。

安装(以 alpine 为例) #

apk add lighttpd
rc-update add lighttpd default
rc-service lighttpd restart

如果用作静态页面服务器,它是开箱即用的,使用上述命令安装、添加并启动服务后,将任一网页或文档放入 /var/www/localhost/htdocs/ 目录下,打开浏览器就可以看到 web 服务已运行并渲染显示出该文件。

创建一个简单的网页文件示例:

echo '<!DOCTYPE html><html><head><title>✔️</title><meta charset="utf-8"></head><body>Lighttpd is running!</body></html>' > /var/www/localhost/htdocs/index.html

主要文件的路径:

  • 配置文件: /etc/lighttpd/lighttpd.conf
  • Html 位置: /var/www/localhost/htdocs/
  • 动态文件(cache, extra): /var/lib/lighttpd/
  • 日志文件(error, access, etc): /var/log/lighttpd/

配置支持 https #

1.安装 openssl

apk add openssl

2.创建放置 SSL 证书的文件夹

mkdir -p /etc/lighttpd/certs

3.创建自签名证书

openssl req -x509 -days 365 -nodes -newkey rsa:4096 \
  -keyout /etc/lighttpd/certs/$(hostname -d).pem -out /etc/lighttpd/certs/$(hostname -d).pem  

会有一些提问对话,根据实际情况输入。

4.给证书适当的权限

chmod 640 /etc/lighttpd/certs/$(hostname -d).pem

5.创建一个 SSL module 配置文件

cat > /etc/lighttpd/mod_ssl.conf << EOF
server.modules += ("mod_openssl")
\$SERVER["socket"] == "0.0.0.0:443" {
    ssl.engine  = "enable"
    ssl.pemfile = "/etc/lighttpd/certs/$(hostname -d).pem"
}
\$HTTP["scheme"] == "http" {
    url.redirect = ("" => "https://\${url.authority}\${url.path}\${qsa}")
    #url.redirect-code = 308  # (before lighttpd 1.4.75)
}
EOF

6.修改配置文件 /etc/lighttpd/lighttpd.conf 启用 mod_redirect 和 openssl module

  • 将此行 # mod_redirect 开头的注释符号删除;
  • 将此行 include "mod_ssl.conf"添加到 {{{ mod_ssl 下;
  • 可能 include "mime-types.conf" 也要打开。

7.(可选)检查配置文件 lighttpd.conf 中有无错误:

	lighttpd -t -f /etc/lighttpd/lighttpd.conf  

8.重启服务然后测试。

	rc-service lighttpd restart  

—— CC BY-NC-ND 4.0