Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

高性能輕量級 Web 服務器 Lighttpd 安裝配置與 PHP/FastCGI 整合實戰

編輯:Linux 教學

為什麼選擇 Lighttpd?

  • 單進程異步 I/O 模型,內存佔用極低(~2-3MB 靜態服務)
  • 靜態文件吞吐率比 Apache 高 2-3 倍
  • 模塊化設計,按需加載,攻擊面小
  • 原生支持 FastCGI/SCGI,PHP/Python/Ruby 部署簡單
  • 適合嵌入式設備、高併發靜態資源、廣告投放、API 閘道等場景

從源碼編譯安裝(推薦生產環境)

# 1. 依賴準備
yum install -y pcre-devel openssl-devel zlib-devel bzip2-devel \
               libattr-devel lua-devel  # 可選模塊依賴

# 2. 下載驗證
wget https://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.73.tar.xz
tar -xf lighttpd-1.4.73.tar.xz
cd lighttpd-1.4.73

# 3. 配置編譯選項
./configure --prefix=/usr/local/lighttpd \
            --with-openssl \
            --with-pcre \
            --with-bzip2 \
            --with-zlib \
            --with-lua \
            --enable-static=no \
            --enable-shared=yes

# 4. 編譯安裝
make -j$(nproc)
make install

# 5. 配置文件部署
cp doc/lighttpd.conf /usr/local/lighttpd/
mkdir -p /usr/local/lighttpd/logs /var/www/html

核心配置文件精簡模板

server.modules = (
    "mod_access",
    "mod_alias",
    "mod_compress",
    "mod_redirect",
    "mod_fastcgi",      # PHP 必須
    "mod_accesslog"
)

server.document-root = "/var/www/html"
server.upload-dirs = ("/var/cache/lighttpd/uploads")
server.errorlog = "/usr/local/lighttpd/logs/error.log"
accesslog.filename = "/usr/local/lighttpd/logs/access.log"
server.pid-file = "/var/run/lighttpd.pid"
server.username = "www"
server.groupname = "www"
server.port = 80

# 靜態文件優化
etag.use-inode = "enable"
etag.use-mtime = "enable"
etag.use-size = "enable"
static-file.etags = "enable"
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ("text/plain","text/html","application/javascript","text/css","text/xml")

# PHP FastCGI 整合(見下文)
include "conf.d/fastcgi.conf"

# 目錄瀏覽(按需啟用)
# dir-listing.activate = "enable"
# dir-listing.hide-dotfiles = "enable"

PHP + FastCGI 深度整合

PHP 編譯關鍵參數

./configure --prefix=/usr/local/php \
            --enable-fpm \
            --enable-fastcgi \
            --enable-force-cgi-redirect \
            --with-openssl \
            --with-pdo-mysql=mysqlnd \
            --with-mysqli=mysqlnd \
            --with-zlib \
            --with-curl \
            --enable-mbstring
make -j$(nproc) && make install

FastCGI 進程管理器配置

# /usr/local/php/etc/php-fpm.conf 關鍵段
[www]
listen = /tmp/php-fcgi.sock
listen.owner = www
listen.group = www
listen.mode = 0660
user = www
group = www
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 10
pm.max_requests = 500
request_terminate_timeout = 30s
slowlog = /usr/local/lighttpd/logs/php-slow.log

Lighttpd FastCGI 掛載配置

# conf.d/fastcgi.conf
fastcgi.server = ( ".php" =>
    ( "localhost" =>
        (
            "socket" => "/tmp/php-fcgi.sock",
            "broken-scriptfilename" => "enable",
            "check-local" => "disable",
            "fix-root-scriptname" => "enable"
        )
    )
)

# 多 PHP 版本共存示例
fastcgi.map-extensions = ( ".php7" => ".php", ".php8" => ".php" )
fastcgi.server += ( ".php7" =>
    ( "php7" => ( "socket" => "/tmp/php7-fcgi.sock" ) )
)
fastcgi.server += ( ".php8" =>
    ( "php8" => ( "socket" => "/tmp/php8-fcgi.sock" ) )
)

SSL/TLS 配置最佳實踐

# 生成自簽證書(生產請用 Let's Encrypt 或購買證書)
openssl req -new -x509 -keyout /etc/lighttpd/server.pem \
            -out /etc/lighttpd/server.pem -days 365 -nodes
chmod 600 /etc/lighttpd/server.pem

# lighttpd.conf 添加
$SERVER["socket"] == ":443" {
    ssl.engine = "enable"
    ssl.pemfile = "/etc/lighttpd/server.pem"
    ssl.cipher-list = "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH"
    ssl.honor-cipher-order = "enable"
    ssl.disable-client-renegotiation = "enable"
    setenv.add-environment = ( "HTTPS" => "on" )
}

虛擬主機配置模式

基於端口(IP-based)

$SERVER["socket"] == "192.168.1.100:8080" {
    server.document-root = "/var/www/site8080"
    accesslog.filename = "/usr/local/lighttpd/logs/site8080.access.log"
}

基於域名(Name-based,推薦)

$HTTP["host"] =~ "^(www\.)?example\.com$" {
    server.document-root = "/var/www/example.com"
    server.errorlog = "/usr/local/lighttpd/logs/example.error.log"
    accesslog.filename = "/usr/local/lighttpd/logs/example.access.log"
}

服務管理與監控

Systemd 服務單元

# /etc/systemd/system/lighttpd.service
[Unit]
Description=Lighttpd Web Server
After=network.target

[Service]
Type=simple
PIDFile=/var/run/lighttpd.pid
ExecStartPre=/usr/local/lighttpd/sbin/lighttpd -t -f /usr/local/lighttpd/lighttpd.conf
ExecStart=/usr/local/lighttpd/sbin/lighttpd -D -f /usr/local/lighttpd/lighttpd.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

健康檢查與日誌分析

# 配置測試
/usr/local/lighttpd/sbin/lighttpd -t -f /usr/local/lighttpd/lighttpd.conf

# 實時訪問日誌
tail -f /usr/local/lighttpd/logs/access.log | awk '{print $1,$7,$9}'

# 慢請求分析
awk '$10 > 1.0' /usr/local/lighttpd/logs/php-slow.log

性能調優清單

  • 啟用 server.max-fds = 20480 提高文件描述符限制
  • 設置 server.network-backend = "linux-sendfile" 利用零拷貝
  • 配置 server.stat-cache-engine = "fam" 減少 stat() 系統調用(需安裝 gamin)
  • 靜態資源設置長期緩存:expire.url = ( "" => "access 1 year" )
  • 啟用 GZIP 壓縮:compress.allowed-encodings = ("gzip", "deflate")
Copyright © Windows教程網 All Rights Reserved