生产 Web 服务器:Lighttpd

来自 Alpine Linux
(重定向自 Setting Up Lighttpd with PHP

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

Lighttpd 安装

此生产环境将仅处理必要的软件包...因此不允许使用 doc 或 manpages。

  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 而无需链工具(只需加水),请阅读 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 重点文档是

参见