lighttpd
“lighttpd (发音为 /lighty/) 是一个安全、快速、符合标准且非常灵活的 web 服务器,它针对高性能环境进行了优化。 lighttpd 支持各种各样的功能,同时有效地使用内存和 CPU,使 lighttpd 成为所有系统(无论大小)的理想 web 服务器。”
安装
配置
基本设置
lighttpd 配置文件是:/etc/lighttpd/lighttpd.conf。默认情况下,它应该生成一个可工作的测试页面。
要检查您的 lighttpd.conf 是否有错误,您可以使用此命令(有助于快速找到错误配置)
$ lighttpd -tt -f /etc/lighttpd/lighttpd.conf
默认配置文件指定 /srv/http/ 作为文档目录。要测试安装,请创建一个虚拟文件
/srv/http/index.html
Hello world!
然后 启动/启用 lighttpd.service,并将您的浏览器指向 localhost,您应该在那里看到测试页面。
示例配置文件位于 /usr/share/doc/lighttpd/ 中。
基本日志记录
lighttpd 可以将错误和访问都写入日志文件。错误日志默认启用(由 server.errorlog 选项控制)。要启用访问日志,请按如下方式编辑 /etc/lighttpd/lighttpd.conf
server.modules += ( "mod_accesslog", ) accesslog.filename = "/var/log/lighttpd/access.log"
启用基于 SSL 的 https
自签名
可以生成自签名 SSL 证书,假设系统上安装了 openssl,如下所示
# mkdir /etc/lighttpd/certs # openssl req -x509 -nodes -days 7300 -newkey rsa:2048 -sha256 -keyout /etc/lighttpd/certs/server.pem -out /etc/lighttpd/certs/server.pem # chmod 600 /etc/lighttpd/certs/server.pem
修改 /etc/lighttpd/lighttpd.conf,添加以下行以启用 https
server.modules += ( "mod_openssl" )
$SERVER["socket"] == ":443" {
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/certs/server.pem"
}
有关详细信息,请参阅 lighttpd TLS 配置。
Let's Encrypt
或者,生成由 Let's Encrypt 签名的证书。
编辑 /etc/lighttpd/lighttpd.conf,添加以下行
$SERVER["socket"] == ":443" {
ssl.engine = "enable"
ssl.privkey = "/etc/letsencrypt/live/domain/privkey.pem"
ssl.pemfile = "/etc/letsencrypt/live/domain/fullchain.pem"
}
有关详细信息,请参阅 lighttpd 文档中的 bootstrap Let's Encrypt。
将 http 请求重定向到 https
您应该在 /etc/lighttpd/lighttpd.conf 的 server.modules 数组中添加 "mod_redirect"
server.modules += ( "mod_redirect" )
$HTTP["scheme"] == "http" {
url.redirect = ("" => "https://${url.authority}${url.path}${qsa}")
}
$SERVER["socket"] == ":443" {
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/certs/server.pem"
server.document-root = "..."
}
要重定向站点的一部分的所有主机(例如 secure 或 phpmyadmin)
$HTTP["url"] =~ "^/secure" {
$HTTP["scheme"] == "http" {
url.redirect = ("" => "https://${url.authority}${url.path}${qsa}")
}
}
密码保护目录
用户身份验证需要一个 passwd 文件,它是 lighttpd 中等效于系统 /etc/passwd 的文件。设置需要特定的格式和 md5sum 哈希密码,但用户可以使用以下示例快速轻松地创建条目
$ user=foo $ password=b@R102 $ realm='Password Required' $ hash=`echo -n "$user:$realm:$password" | md5sum | cut -b -32` # echo "$user:$realm:$hash" >> /etc/lighttpd/lighttpd.user
修改 /etc/lighttpd/lighttpd.conf,添加以下行以启用目录保护
server.modules += ( "mod_auth", "mod_authn_file" )
auth.backend = "htdigest"
auth.backend.htdigest.userfile = "/etc/lighttpd/lighttpd.user"
# note this entry is relative to the server.document-root
auth.require = ( "/secret" =>
(
"method" => "basic",
"realm" => "Password Required",
"require" => "valid-user"
)
)
/etc/lighttpd/lighttpd.conf 中的 realm 必须与 /etc/lighttpd/lighttpd.user 中选择的值匹配,身份验证才能工作。CGI
通用网关接口 (CGI) 脚本只需要启用 CGI 模块;包含配置文件,并确保安装了您选择的编程语言解释器。(即,对于 python,您需要安装 python)
创建文件 /etc/lighttpd/conf.d/cgi.conf 并添加以下内容
server.modules += ( "mod_cgi" )
cgi.assign = ( ".pl" => "/usr/bin/perl",
".cgi" => "/usr/bin/perl",
".rb" => "/usr/bin/ruby",
".erb" => "/usr/bin/eruby",
".py" => "/usr/bin/python",
".php" => "/usr/bin/php-cgi" )
index-file.names +=( "index.pl", "default.pl",
"index.rb", "default.rb",
"index.erb", "default.erb",
"index.py", "default.py",
"index.php", "default.php" )
对于 PHP 脚本,您需要确保在 /etc/php/php.ini 中设置了以下内容
cgi.fix_pathinfo = 1
在您的 lighttpd 配置文件 /etc/lighttpd/lighttpd.conf 中添加
include "conf.d/cgi.conf"
FastCGI
安装 fcgi。现在您已经安装了支持 fcgi 的 lighttpd。如果这就是您想要的,那么您就都设置好了。想要 Ruby on Rails、PHP 或 Python 的人应该继续。
http 身份运行,而不是组 nobody。首先,将示例配置文件从 /usr/share/doc/lighttpd/config/conf.d/fastcgi.conf 复制到 /etc/lighttpd/conf.d
以下内容需要添加到配置文件 /etc/lighttpd/conf.d/fastcgi.conf 中
server.modules += ( "mod_fastcgi" )
index-file.names += ( "dispatch.fcgi" ) #dispatch.fcgi if rails specified
server.error-handler-404 = "/dispatch.fcgi" #too
fastcgi.server = (
".fcgi" => (
"localhost" => (
"socket" => "/run/lighttpd/rails-fastcgi.sock",
"bin-path" => "/path/to/rails/application/public/dispatch.fcgi"
)
)
)
然后在 /etc/lighttpd/lighttpd.conf 中
include "conf.d/fastcgi.conf"
对于 PHP 或 Ruby on Rails,请参阅下一节。
PHP
使用 php-cgi
安装 php 和 php-cgi(另请参阅 PHP 和 LAMP)。
检查 php-cgi 是否正常工作 php-cgi --version
PHP 5.4.3 (cgi-fcgi) (built: May 8 2012 17:10:17) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
如果您得到类似的输出,则 php 已正确安装。
创建一个新的配置文件
/etc/lighttpd/conf.d/fastcgi.conf
# Make sure to install php and php-cgi. See:
# https://wiki.archlinux.org.cn/index.php/Fastcgi_and_lighttpd#PHP
server.modules += ("mod_fastcgi")
# FCGI server
# ===========
#
# Configure a FastCGI server which handles PHP requests.
#
index-file.names += ("index.php")
fastcgi.server = (
# Load-balance requests for this path...
".php" => (
# ... among the following FastCGI servers. The string naming each
# server is just a label used in the logs to identify the server.
"localhost" => (
"bin-path" => "/usr/bin/php-cgi",
"socket" => "/tmp/php-fastcgi.sock",
# breaks SCRIPT_FILENAME in a way that PHP can extract PATH_INFO
# from it
"broken-scriptfilename" => "enable",
# Launch (max-procs + (max-procs * PHP_FCGI_CHILDREN)) procs, where
# max-procs are "watchers" and the rest are "workers". See:
# https://wiki.lighttpd.net/frequentlyaskedquestions#How-many-php-CGI-processes-will-lighttpd-spawn
"max-procs" => "4", # default value
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "1" # default value
)
)
)
)
通过将以下行附加到您的 lighttpd 配置文件,使 lighttpd 使用新的配置文件
/etc/lighttpd/lighttpd.conf
include = "conf.d/fastcgi.conf"
/usr/share/doc/lighttpd/config/modules.conf 中列出。重新加载 lighttpd。
- 如果您在尝试访问 php 文件时收到类似No input file found的错误,则可能有几种可能的解释。有关更多信息,请参阅 此 FAQ。
- 确保没有其他模块(例如
mod_cgi)会尝试处理 .php 扩展名。
使用 php-fpm
对于 PHP 进程的动态管理,您可以安装 php-fpm,然后 启动 和 启用 php-fpm.service。
/etc/php/php-fpm.conf 来配置池中的服务器数量并调整其他配置选项。有关 php-fpm 的更多详细信息,请访问 php-fpm 网站。请记住,当您更改 /etc/php/php.ini 时,您需要 重启 php-fpm.service。在 /etc/lighttpd/conf.d/fastcgi.conf 中添加
server.modules += ( "mod_fastcgi" )
index-file.names += ( "index.php" )
fastcgi.server = (
".php" => (
"localhost" => (
"socket" => "/run/php-fpm/php-fpm.sock",
"broken-scriptfilename" => "enable"
))
)
uWSGI
在 /etc/lighttpd/lighttpd.conf 中添加
server.modules += ("mod_scgi")
$HTTP["url"] =~ "^/uwsgi/" {
scgi.protocol = "uwsgi"
scgi.server = (
"/uwsgi/foo" => ((
"socket" => "/path/to/socket",
"check-local" => "disable"
)),
"/uwsgi/bar" => ((
"host" => "127.0.0.1",
"port" => "8080",
"check-local" => "disable"
))
)
}
然后,您可以将 uwsgi 应用程序作为 systemd 单元 或 直接启动。这里 是 digitalocean 提供的关于如何从头开始设置 flask 应用程序的简洁指南。
输出压缩
复制示例配置文件
# mkdir /etc/lighttpd/conf.d # cp /usr/share/doc/lighttpd/config/conf.d/deflate.conf /etc/lighttpd/conf.d/
在 /etc/lighttpd/lighttpd.conf 中添加以下内容
include "conf.d/deflate.conf"
最后,重新加载 lighttpd.service,它将动态压缩纯文本和 html 内容。
deflate.conf)并在 /etc/lighttpd/lighttpd.conf 中添加所需的内容来代替。也可以选择应该压缩的内容类型。修改 /etc/lighttpd/conf.d/deflate.conf 上的参数 deflate.mimetypes
deflate.mimetypes = ("text/plain", "text/html", "text/javascript", "text/css", "text/xml")
您还可以创建一个缓存目录来存储压缩文件
# mkdir /var/cache/lighttpd/compress # chown http:http /var/cache/lighttpd/compress
然后取消注释并修改 /etc/lighttpd/conf.d/deflate.conf 中的 deflate.cache-dir 选项
deflate.cache-dir = "/var/cache/lighttpd/compress"