Lighttpd
lighttpd是一個安全,快速,標準,且非常靈活的網頁伺服器,並對高性能環境做了最佳化。相較於其他網頁伺服器,它佔用的內存很少,且注重CPU負載量。它的進階功能集 (FastCGI,CGI,驗證,輸出壓縮,網址重定向等等)讓Lighttpd成為每個試圖擺脫瓶頸的伺服器的完美網頁伺服器軟件。
安裝[編輯 | 編輯原始碼]
Lighttpd可以從[extra]倉庫獲得:
# pacman -S lighttpd
設置[編輯 | 編輯原始碼]
基本設置[編輯 | 編輯原始碼]
lighttpd配置文件: /etc/lighttpd/lighttpd.conf
。在安裝完成後它會自動生成一個用於測試的測試頁面。
想要檢查 lighttpd.conf
中的語法錯誤,可以使用以下命令來快速查找錯誤:
$ lighttpd -t -f /etc/lighttpd/lighttpd.conf
預設配置文件會把 /srv/http/
指定為提供HTTP服務的文件目錄。
測試安裝結果:
# chmod 755 /srv/http/index.html # echo 'TestMe!' >> /srv/http/index.html
修改日誌目錄權限:
# chown -R http:http /var/log/lighttpd
啟動服務:
# systemctl start lighttpd
然後就可以使用瀏覽器打開網址 localhost
,你應該能見到測試頁面。
開機自動啟動:
# systemctl enable lighttpd
示例配置文件在 /usr/share/doc/lighttpd/
。
基本日誌[編輯 | 編輯原始碼]
lighttpd 能夠輸出錯誤與訪問記錄到日誌文件中。錯誤日誌默認啟用(server.errorlog
選項). 如下編輯/etc/lighttpd/lighttpd.conf
啟用訪問日誌:
server.modules += ( "mod_accesslog", ) accesslog.filename = "/var/log/lighttpd/access.log"
通過SSL啟動HTTPS[編輯 | 編輯原始碼]
自簽名證書[編輯 | 編輯原始碼]
安裝openssl包後,自簽名SSL證書用以下方式生成:
# 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 文檔中的 啟用 Let's Encrypt。
重定向HTTP請求到HTTPS[編輯 | 編輯原始碼]
你需要在/etc/lighttpd/lighttpd.conf
中的server.modules數組中增加"mod_redirect"
:
server.modules += ( "mod_redirect" ) $SERVER["socket"] == ":80" { $HTTP["host"] == "example.org" { url.redirect = ( "^/(.*)" => "https://example.org/$1" ) server.name = "example.org" } } $SERVER["socket"] == ":443" { ssl.engine = "enable" ssl.pemfile = "/etc/lighttpd/certs/server.pem" server.document-root = "..." }
為了重定向所有網絡主機到它們的HTTPS安全訪問方式,用以下內容取代之前的socket 80端口配置:
$SERVER["socket"] == ":80" { $HTTP["host"] =~ ".*" { url.redirect = (".*" => "https://%0$0") } }
如僅需重定向所有網絡主機的一部分 (例如,「secure」 or 」phpmyadmin「):
$SERVER["socket"] == ":80" { $HTTP["url"] =~ "^/secure" { url.redirect = ( "^/(.*)" => "https://example.com/$1" ) } }
用密碼保護目錄[編輯 | 編輯原始碼]
注意:此模塊需要mariadb-libs包。一個近似於系統的/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" # 注意:这里的路径是相对于server.document-root变量的 auth.require = ( "/secret" => ( "method" => "basic", "realm" => "Password Required", "require" => "valid-user" ) )
/etc/lighttpd/lighttpd.conf
的」user「和」realm「項必須與/etc/lighttpd/lighttpd.user
中相符,以使認證正常運作。CGI[編輯 | 編輯原始碼]
Common Gateway Interface (CGI) 腳本對於lighttpd可以開箱即用, 你只需要開啟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包,之後你的lighttpd就有了fcgi支持, 以下內容是給需要使用Ruby,PHP或者Python的人的指導。
http
運行。首先複製一份默認配置,從 /usr/share/doc/lighttpd/config/conf.d/fastcgi.conf
複製到 /etc/lighttpd/conf.d
將以下內容添加到 /etc/lighttpd/conf.d/fastcgi.conf
server.modules += ( "mod_fastcgi" ) #server.indexfiles += ( "dispatch.fcgi" ) #这是强烈不建议的 index-file.names += ( "dispatch.fcgi" ) # 如果选用rails,增加dispatch.fcgi server.error-handler-404 = "/dispatch.fcgi" #同上 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的指導。
PHP[編輯 | 編輯原始碼]
安裝 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
# 请确保php和php-cgi已正确安装。参阅: # https://wiki.archlinux.org/index.php/Fastcgi_and_lighttpd#PHP server.modules += ("mod_fastcgi") # FCGI server # =========== # # 配置一个处理PHP请求的FastCGI服务器 # index-file.names += ("index.php") fastcgi.server = ( ".php" => ( # 下列FastCGI服务器中的负载均衡配置。 # 服务器的命名仅用作日志中服务器的标识名。 "localhost" => ( "bin-path" => "/usr/bin/php-cgi", "socket" => "/tmp/php-fastcgi.sock", # 分解SCRIPT_FILENAME 以让PHP能够从中解析出PATH_INFO "broken-scriptfilename" => "enable", # 启动 (max-procs + (max-procs * PHP_FCGI_CHILDREN)) 个进程, 其中 # max-procs 个是“watcher”进程,其余是“worker”进程。参阅: # https://redmine.lighttpd.net/projects/1/wiki/frequentlyaskedquestions#How-many-php-CGI-processes-will-lighttpd-spawn "max-procs" => 4, # 默认值 "bin-environment" => ( "PHP_FCGI_CHILDREN" => "1" # 默认值 ) ) ) )
在/etc/lighttpd/lighttpd.conf中添加以下內容使新配置能夠應用:
/etc/lighttpd/lighttpd.conf
include "conf.d/fastcgi.conf"
/usr/share/doc/lighttpd/config/modules.conf
. 任何錯誤配置都可能導致 lighttpd 崩潰。重新加載 lighttpd:
- systemctl restart lighttpd
- 如果你在訪問php文件時遇到諸如 No input file found 的錯誤, 有很多原因可以導致這個錯誤,請參閱 this 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" )) ) }
可以採用systemd unit]或[direct的方式啟動ywsgi應用。 此處是一個來自 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"
Finally, reload lighttpd.service
, 後續將會自動壓縮純文本和 html 內容。
/etc/lighttpd/lighttpd.conf
增加內容代替複製deflate.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"