配置¶
您有多少时间来设置 lighttpd?5 分钟?10 分钟?更多?
5 分钟¶
想运行一个快速、低资源的静态内容服务器吗?这很简单。创建一个名为 lighttpd.conf 的文本文件,内容如下:
server.document-root = "/var/www/servers/www.example.org/pages/" server.port = 3000 # If running lighttpd earlier than lighttpd 1.4.71, uncomment (remove '#') to add the following: #mimetype.assign = ( # ".html" => "text/html", # ".txt" => "text/plain", # ".jpg" => "image/jpeg", # ".png" => "image/png" #)
lighttpd 默认将绑定到所有接口,并监听 server.port
中指定的端口(默认为端口 80)上的 HTTP 请求。然后 lighttpd 将从指定的 server.document-root
提供文件。文档根目录中的文件——即用于所有请求的基础目录——必须可由启动 Web 服务器的用户读取。对于 lighttpd 1.4.71 之前的版本,请取消注释 mimetype.assign
以分配一些重要的 MIME 类型。
首先,检查您的配置是否正确
$ lighttpd -tt -f lighttpd.conf
现在启动服务器进行测试
$ lighttpd -D -f lighttpd.conf
并将您的浏览器(在与 lighttpd 相同的机器上运行)指向 http://127.0.0.1:3000/index.html
要停止服务器并返回命令提示符,请按 Ctrl-C。
一个真正的守护进程¶
接下来您应该熟悉一些服务器安全所需的设置
server.document-root = "/var/www/servers/www.example.org/pages/" #server.port = 80 # 80 is the default listening port number, if not otherwise specified server.username = "www" server.groupname = "www" # If running lighttpd earlier than lighttpd 1.4.71, uncomment (remove '#') to add the following: #mimetype.assign = ( # ".html" => "text/html", # ".txt" => "text/plain", # ".jpg" => "image/jpeg", # ".png" => "image/png" #) static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" ) index-file.names = ( "index.html" )
现在 Web 服务器正在监听端口 80(HTTP 流量的默认端口),并将切换到用户 www
和组 www
。
服务器必须以 root 身份启动才能控制端口 80,但在获取端口后继续以 root 身份运行不是必需的,也不是一个好主意,因此服务器会切换到用户 www
。
最后,禁止查看某些类型文件的内容,因为它们用于生成动态内容。直接对目录的请求将被重写为该目录中的 index.html
文件。
假设您已经按照 InstallFromSource 的“初始化脚本”部分所述创建了 /etc/init.d/lighttpd
服务,请将配置文件放入 /etc/lighttpd/lighttpd.conf
并使用以下命令启动服务器:# /etc/init.d/lighttpd start
要停止它,请使用# /etc/init.d/lighttpd stop
对于基于 systemd 的发行版systemctl start lighttpd
和systemctl stop lighttpd
10 分钟¶
条件语句,条件语句,条件语句
lighttpd 配置的一个强大特性是使用条件语句。通过使用简单条件或正则表达式条件,可以覆盖默认设置。
server.document-root = "/var/www/servers/www.example.org/pages/" #server.port = 80 # 80 is the default listening port number, if not otherwise specified server.username = "www" server.groupname = "www" # If running lighttpd earlier than lighttpd 1.4.71, uncomment (remove '#') to add the following: #mimetype.assign = ( # ".html" => "text/html", # ".txt" => "text/plain", # ".jpg" => "image/jpeg", # ".png" => "image/png" #) static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" ) index-file.names = ( "index.html" ) $HTTP["host"] == "www2.example.org" { server.document-root = "/var/www/servers/www2.example.org/pages/" }
现在我们有了一个新的虚拟服务器,www2.example.org
,它使用与第一个服务器相同的设置,只是文档根目录不同。
以下服务器配置添加了一个下载区域并启用了内置的目录列表功能
server.document-root = "/var/www/servers/www.example.org/pages/" #server.port = 80 # 80 is the default listening port number, if not otherwise specified server.username = "www" server.groupname = "www" # If running lighttpd earlier than lighttpd 1.4.71, uncomment (remove '#') to add the following: #mimetype.assign = ( # ".html" => "text/html", # ".txt" => "text/plain", # ".jpg" => "image/jpeg", # ".png" => "image/png" #) static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" ) index-file.names = ( "index.html" ) $HTTP["host"] == "www2.example.org" { server.document-root = "/var/www/servers/www2.example.org/pages/" $HTTP["url"] =~ "^/download/" { dir-listing.activate = "enable" } }
如您所见,条件语句可以嵌套:只有 download
文件夹及其子文件夹启用了目录列表。
条件语句中还有 else
子句。尽管有这个名称,但它是一个类似于某些编程语言的 else if
结构,因为它后面必须跟另一个条件。
这是一个基于条件的虚拟主机的示例。else
用于配置只应存在于“默认”虚拟主机中的行为。
$HTTP["host"] == "example.org" { # options specific to example.org expire.url = ( "" => "access plus 25 hours" ) } else $HTTP["host"] == "static.example.org" { # options specific to static.example.org expire.url = ( "" => "access plus 2 weeks" ) } else $HTTP["host"] =~ "" { # options applied to any other vhosts present on this ip # ie. default options expire.url = ( "" => "access plus 3 hours" ) }
现在我们已经介绍了基础知识,您可以学习一些更高级的主题,例如 包含 以及使用 FastCGI 配置 PHP
30 分钟后¶
现在您已经了解了基本设置、包含文件,甚至可能知道如何设置 PHP 或 Ruby。但是还有更多等待您去发现。
其中大部分内容都可以在 默认配置文件 的示例中找到,该文件也位于压缩包的 doc
目录中。
有关配置文件语法和完整的配置选项列表,请访问维基的配置部分:文档
另请查看我们的社区。