生产 Web 服务器:Lighttpd

来自 Alpine Linux

lighttpd 是一个简单、符合标准、安全且灵活的 Web 服务器。

Lighttpd 安装

此生产环境将仅处理必要的软件包...因此不允许使用文档或手册页。

  1. 创建 htdocs 公共 Web 根目录
  2. 将服务添加到默认运行级别,而不是启动,因为需要激活网络。
  3. 启动 Web 服务器服务
mkdir -p /var/www/localhost/htdocs /var/log/lighttpd /var/lib/lighttpd

chown -R lighttpd:lighttpd /var/www/localhost/ /var/log/lighttpd /var/lib/lighttpd 

rc-update add lighttpd default

rc-service lighttpd restart

echo "it works" > /var/www/localhost/htdocs/index.html

为了测试,打开浏览器并访问 http://<webserveripaddres> 你将看到“it works”。“webserveripaddres” 是你的设置/服务器机器的 IP 地址。

控制 Lighttpd

启动 lighttpd:

rc-service lighttpd start

你将获得关于状态的反馈。

 * Caching service dependencies                                 [ ok ]
 * Starting lighttpd...                                         [ ok ]

停止 lighttpd:

rc-service lighttpd stop

重启 lighttpd:更改配置文件后,需要重启 lighttpd。

rc-service lighttpd restart

正确的运行级别:默认情况下,没有服务添加到启动进程,系统管理员必须知道我们想要什么以及服务将做什么,另一个主要原因是由于 docker 中本身没有运行级别,并且 Alpine linux 主要用于 docker 容器中。你必须仅将服务添加到默认运行级别,而不是启动,因为需要激活网络

rc-update add lighttpd default

Lighttpd 配置

如果你只想提供简单的 HTML 页面,lighttpd 可以开箱即用。无需进一步配置。

lighttpd 配置选项

状态页面

注意状态 Web 服务器:这些特殊页面只是运行 Web 服务器的最小信息,需要在紧急情况下从外部查看,不要采取错误的隐藏在过滤的 IP 或过滤的网络后面的方法,你必须在所有时间都能从所有 Web 访问以查看问题。

mod_status

  1. 在配置文件中启用 mod_status
  2. 更改配置文件中的路径(可选),我们正在使用混淆安全。
  3. 重启服务以在浏览器中查看更改
sed -i -r 's#\#.*mod_status.*,.*#    "mod_status",#g' /etc/lighttpd/lighttpd.conf

sed -i -r 's#.*status.status-url.*=.*#status.status-url  = "/stats/server-status"#g' /etc/lighttpd/lighttpd.conf

sed -i -r 's#.*status.config-url.*=.*#status.config-url  = "/stats/server-config"#g' /etc/lighttpd/lighttpd.conf

rc-service lighttpd restart

CGI bin 目录支持

默认情况下,软件包在 localhost 主域下分配一个目录,其他 Linux 使用全局 cgi 目录和别名...最专业的方式,但请考虑一下,这种按域配置允许隔离。

mod_cgi

  1. 在配置文件中启用 mod_alias,因为需要 cgi 文件的特定路径以确保安全。
  2. 创建目录
  3. 启用 config cgi 文件
  4. 重启服务以在浏览器中查看更改
mkdir -p /var/www/localhost/cgi-bin

sed -i -r 's#\#.*mod_alias.*,.*#    "mod_alias",#g' /etc/lighttpd/lighttpd.conf

sed -i -r 's#.*include "mod_cgi.conf".*#   include "mod_cgi.conf"#g' /etc/lighttpd/lighttpd.conf

rc-service lighttpd restart

之后,/var/www/localhost/cgi-bin 目录下的所有文件都将通过 http://localhost/cgi-bin/ 路径访问

.cgi 和 .pl 脚本使用 /usr/bin/perl 运行。审查并修改 mod_cgi.conf,如果需要其他设置。然后重启 lighttpd 以应用更改。

为客户端和访问者创建特殊错误页面 (404 或 500)

当服务器上不存在页面或路径,或者发生内部错误时,将向访问者显示这些页面。这些页面替换默认的最小错误页面,并且可以是一个友好的消息或“请勿在此停留”的消息

server.errorfile-prefix

  1. 创建目录以放置在发生这些错误时显示的 HTML 文件。
  2. 在目录中为每条消息创建简单的文件。
  3. 在配置文件中设置正确的配置。
  4. 重启服务以在浏览器中查看更改(只需请求一个不存在的页面,你就会看到它)。
mkdir -p /var/www/localhost/errors

cat > /var/www/localhost/errors/status-404.html << EOF
<h1>The page that you requested are not yet here anymore, sorry was moved or updated, search or visit another one</h1>
EOF

cat > /var/www/localhost/errors/status-500.html << EOF
<h1>Please wait a moment, there's something happens and we are give support maintenance right now to resolve</h1>
EOF

cp /var/www/localhost/errors/status-404.html /var/www/localhost/errors/status-403.html

cp /var/www/localhost/errors/status-500.html /var/www/localhost/errors/status-501.html

cp /var/www/localhost/errors/status-500.html /var/www/localhost/errors/status-503.html

sed -i -r 's#.*server.errorfile-prefix.*#server.errorfile-prefix    = var.basedir + "/errors/status-"#g' /etc/lighttpd/lighttpd.conf

rc-service lighttpd restart

Userdir public_html 支持

mod_userdir

Lighttpd SSL 支持

lighttpd TLS 文档

为 lighttpd 创建 TLS 配置。最好的方法是通过外部包含文件。Debian 对应物有一个很好的机制来启用配置文件。我们将以类似的方式添加 SSL 支持。

SSL:制作自签名证书

如果我们还没有自签名证书,我们需要创建一个。

  1. 安装 openssl
  2. 创建自签名证书
  3. 设置正确的权限
  4. 为 lighttpd 创建 SSL 模块配置文件。
  5. 激活配置文件中缺少的 openssl 模块。
  6. 在全局 http 到 https 重定向的情况下激活 mod_redirect。
  7. 重启服务以查看更改
apk add openssl

mkdir -p /etc/ssl/certs/

openssl req -x509 -days 1460 -nodes -newkey rsa:4096 \
   -subj "/C=VE/ST=Bolivar/L=Upata/O=VenenuX/OU=Systemas:hozYmartillo/CN=$(hostname -d)" \
   -keyout /etc/ssl/certs/$(hostname -d).pem -out /etc/ssl/certs/$(hostname -d).pem

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

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

sed -i -r 's#\#.*mod_redirect.*,.*#    "mod_redirect",#g' /etc/lighttpd/lighttpd.conf

checkssl="";checkssl=$(grep 'include "mod_ssl.conf' /etc/lighttpd/lighttpd.conf);[[ "$checkssl" != "" ]] && echo listo || sed -i -r 's#.*include "mime-types.conf".*#include "mime-types.conf"\ninclude "mod_ssl.conf"#g' /etc/lighttpd/lighttpd.conf

rc-service lighttpd restart

为了部署使用 Lets Encrypt 而无需 chain-tools(只需加水),请阅读 HowToSimpleSSL

Lighttpd 高级

Lighttpd 具有相当不错的默认设置,但如果我们需要响应更高的服务器负载,则可以调整一些设置。

lighttpd 资源调优

Lighttpd 为高负载调优

lighttpd 性能调优

更多连接,更多文件描述符

必须谨慎使用。对于 UNIX 操作系统,一切皆文件。嗯,每次访问者访问页面时,lighttpd 使用三个文件描述符:到客户端的 IP 套接字、FastCGI 进程套接字和访问文档的文件句柄。当 90% 的可用套接字被使用时,Lighttpd 停止接受新连接,并在使用率降至 80% 时再次重新开始。使用 1024 个文件描述符的默认设置,lighttpd 最多可以处理 307 个连接。如果此数字超过,则必须增加文件描述符。这是一个精细的调整,因此必须使用 cat /proc/sys/fs/file-max 检查你的默认值,并确保它超过 10,000

checkset="";checkset=$(grep 'max-fds' /etc/lighttpd/lighttpd.conf);[[ "$checkset" != "" ]] && echo listo || sed -i -r 's#server settings.*#server settings\nserver.max-fds = 2048\n#g' /etc/lighttpd/lighttpd.conf

rc-service lighttpd restart


高负载下的 HTTP Keep-Alive

文件描述符如此快速耗尽的一个原因是 HTTP keep-alive。为了提高性能,现代 Web 服务器保持客户端连接活动状态以处理多个请求,而不是为页面中的每个项目建立和拆除连接。Keep-alive 对性能非常有益,但也倾向于保持不必要的连接处于活动状态。lighttpd 允许每个连接 1000 个 keep-alive 请求,允许空闲会话保持活动 5 秒,并分别给予读取和写入 1 分钟和 6 分钟完成时间。

  1. 服务器终止连接之前,keep-alive 会话中的最大请求数,默认值 = 1000 (server.max-keep-alive-requests)
  2. 空闲 keep-alive 连接被断开之前的最大秒数,默认值 = 5 (server.max-keep-alive-idle)
  3. 等待的非 keep-alive 读取超时并关闭连接之前的最大秒数,默认值 = 60 (server.max-read-idle)
  4. 等待的写入调用超时并关闭连接之前的最大秒数,默认值 = 360 (server.max-write-idle)

虽然 lighttpd 具有相当激进的默认值(特别是与 Apache 相比),但在高流量时期和一些慢速客户端可能会看到许多未使用的连接仍然存在。server.max-keep-alive-idle 设置的默认值 5 秒可以减少到低至 2,如果你假设你的客户端请求数据相当快,但 3 或 4 的值可能更现实。你可能想要将 server.max-keep-alive-requests 值从默认值 1000 增加,但你可能不需要。server.max-read-idle 和 server.max-write-idle 设置很诱人,但这些情况通常相当罕见,所以我们不要乱动它们。

Lighttpd_Advanced_security

请参阅 Lighttpd_Advanced_security wiki 页面。

Lighttpd 和 PHP with fpm

在生产 Web 中,LAMP 意味着 Linux + Apache + Mysql + Php 已安装并集成,但今天 apache 的“A”更多地用作 Nginx 或 Lighttpd,MySQL 的“M”更多地用作 Mariadb,LAMP 重点文档是

参见