1. Lighttpd简介与版本说明
Lighttpd(发音为”lighty”)是一个安全、快速、标准兼容且非常灵活的Web服务器,专为高性能环境进行了优化。更多学习教程www.fgedu.net.cn。Lighttpd以低内存占用和CPU高效使用而闻名,资源消耗比其他流行的Web服务器更低。
Lighttpd采用事件驱动的架构,支持FastCGI、CGI、认证、输出压缩、URL重写等高级功能。学习交流加群风哥微信: itpux-com。它非常适合资源受限的环境,如嵌入式设备、路由器和高并发场景。
Lighttpd核心特性:
– 高性能:事件驱动架构,支持大并发
– FastCGI支持:高效的动态内容处理
– CGI支持:传统CGI脚本支持
– 认证功能:多种认证方式支持
– 输出压缩:支持gzip和deflate压缩
– URL重写:强大的URL重写规则
– 虚拟主机:支持基于域名和端口的虚拟主机
– SSL/TLS:支持HTTPS加密传输
– 代理功能:支持反向代理
– WebDAV:支持WebDAV协议
– 流媒体:支持FLV流媒体
Lighttpd与其他Web服务器对比:
内存占用 极低 低 中等
CPU效率 高 高 中等
配置复杂度 中等 中等 复杂
FastCGI 支持 支持 支持
HTTP/2 支持 支持 支持
模块化 支持 支持 支持
学习曲线 中等 陡峭 陡峭
适用场景 嵌入式/高并发 通用 企业级
2. Lighttpd版本选择与下载地址
Lighttpd采用1.4.x系列作为稳定版本,持续更新维护。
Lighttpd版本状态:
1.4.82 2025-09-12 最新稳定版
1.4.81 2025-XX-XX 稳定版
1.4.80 2025-XX-XX 稳定版
1.4.79 2025-XX-XX 维护版
1.4.78 2025-XX-XX 维护版
Lighttpd 1.4.82主要更新:
– 安全漏洞修复
– 性能优化
– Bug修复
– 新功能增强
官方下载地址:
下载页面:https://www.lighttpd.net/download/
源码仓库:https://github.com/lighttpd/lighttpd
文档中心:https://redmine.lighttpd.net/projects/lighttpd/wiki
3. Lighttpd下载方式详解
方式一:源码编译安装(推荐生产环境)
$ cd /fgeudb/software
$ wget https://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.82.tar.gz
输出示例如下:
–2026-04-04 10:00:00– https://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.82.tar.gz
Resolving download.lighttpd.net… 195.30.85.210
Connecting to download.lighttpd.net|195.30.85.210|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 1023456 (1000K) [application/octet-stream]
Saving to: ‘lighttpd-1.4.82.tar.gz’
lighttpd-1.4.82.tar.gz 100%[======================================================================>] 999.47K 1.2MB/s in 0.8s
2026-04-04 10:00:01 (1.2 MB/s) – ‘lighttpd-1.4.82.tar.gz’ saved [1023456/1023456]
下载GPG签名验证:
$ wget https://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.82.tar.gz.asc
验证签名:
$ gpg –verify lighttpd-1.4.82.tar.gz.asc lighttpd-1.4.82.tar.gz
解压源码包:
$ tar -zxvf lighttpd-1.4.82.tar.gz -C /fgeudb/
安装编译依赖:
# yum install -y gcc pcre-devel zlib-devel openssl-devel bzip2-devel
或Ubuntu/Debian:
# apt install -y build-essential libpcre3-dev zlib1g-dev libssl-dev libbz2-dev
方式二:包管理器安装
# yum install -y epel-release
# yum install -y lighttpd
输出示例如下:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
–> Running transaction check
—> Package lighttpd.x86_64 0:1.4.82-1.el7 will be installed
–> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
lighttpd x86_64 1.4.82-1.el7 epel 350 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 350 k
Installed size: 850 k
Downloading packages:
lighttpd-1.4.82-1.el7.x86_64.rpm | 350 kB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : lighttpd-1.4.82-1.el7.x86_64 1/1
Verifying : lighttpd-1.4.82-1.el7.x86_64 1/1
Installed:
lighttpd.x86_64 0:1.4.82-1.el7
Complete!
Ubuntu/Debian安装:
# apt update
# apt install -y lighttpd
输出示例如下:
Reading package lists… Done
Building dependency tree… Done
The following NEW packages will be installed:
lighttpd
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 350 kB of archives.
After this operation, 850 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 lighttpd amd64 1.4.82-1 [350 kB]
Fetched 350 kB in 2s (175 kB/s)
Selecting previously unselected package lighttpd…
(Reading database … 123456 files and directories currently installed.)
Preparing to unpack …/lighttpd_1.4.82-1_amd64.deb …
Unpacking lighttpd (1.4.82-1) …
Setting up lighttpd (1.4.82-1) …
验证安装:
$ lighttpd -v
输出示例如下:
lighttpd/1.4.82 (ssl) – a light and fast webserver
Build-Date: Apr 4 2026 10:00:00
方式三:Docker容器部署
$ docker pull lighttpd:1.4.82
输出示例如下:
1.4.82: Pulling from library/lighttpd
Digest: sha256:abc123def456…
Status: Downloaded newer image for lighttpd:1.4.82
启动Lighttpd容器:
$ docker run –name lighttpd-web \
-p 80:80 \
-v /fgeudb/lighttpd/conf:/etc/lighttpd:ro \
-v /fgeudb/lighttpd/www:/var/www/html:ro \
-v /fgeudb/lighttpd/logs:/var/log/lighttpd \
-d lighttpd:1.4.82
输出示例如下:
abc123def456789…
查看容器状态:
$ docker ps | grep lighttpd
输出示例如下:
abc123def456 lighttpd:1.4.82 “lighttpd -D -f /etc…” 10 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp lighttpd-web
4. Lighttpd安装部署实战
步骤1:编译安装Lighttpd
# groupadd lighttpd
# useradd -g lighttpd -s /sbin/nologin -M lighttpd
配置编译选项:
$ cd /fgeudb/lighttpd-1.4.82
$ ./configure \
–prefix=/fgeudb/lighttpd \
–with-openssl \
–with-pcre \
–with-zlib \
–with-bzip2 \
–with-ldap \
–with-mysql
输出示例如下:
checking build system type… x86_64-pc-linux-gnu
checking host system type… x86_64-pc-linux-gnu
checking for a BSD-compatible install… /usr/bin/install -c
checking whether build environment is sane… yes
…
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating doc/Makefile
Plugins:
enabled:
– mod_accesslog
– mod_cgi
– mod_fastcgi
– mod_proxy
– mod_rewrite
– mod_ssl
– mod_compress
编译安装:
$ make -j$(nproc)
# make install
输出示例如下:
Making all in src
make[1]: Entering directory ‘/fgeudb/lighttpd-1.4.82/src’
…
make[1]: Leaving directory ‘/fgeudb/lighttpd-1.4.82’
步骤2:创建目录结构
# mkdir -p /fgeudb/lighttpd/{conf,www,logs,run}
# mkdir -p /fgeudb/lighttpd/www/{html,cgi-bin}
# chown -R lighttpd:lighttpd /fgeudb/lighttpd
# chmod -R 755 /fgeudb/lighttpd
创建测试页面:
# echo “<html><body><h1>Welcome to Lighttpd!</h1></body></html>” > /fgeudb/lighttpd/www/html/index.html
步骤3:配置systemd服务
# vi /etc/systemd/system/lighttpd.service
[Unit]
Description=Lighttpd Web Server
After=network.target
[Service]
Type=forking
PIDFile=/fgeudb/lighttpd/run/lighttpd.pid
ExecStartPre=/fgeudb/lighttpd/sbin/lighttpd -t -f /fgeudb/lighttpd/conf/lighttpd.conf
ExecStart=/fgeudb/lighttpd/sbin/lighttpd -f /fgeudb/lighttpd/conf/lighttpd.conf
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重载服务配置:
# systemctl daemon-reload
启动服务:
# systemctl start lighttpd
设置开机自启:
# systemctl enable lighttpd
查看服务状态:
# systemctl status lighttpd
输出示例如下:
● lighttpd.service – Lighttpd Web Server
Loaded: loaded (/etc/systemd/system/lighttpd.service; enabled)
Active: active (running) since Fri 2026-04-04 10:05:00 CST; 10s ago
Process: 12345 ExecStart=/fgeudb/lighttpd/sbin/lighttpd -f /fgeudb/lighttpd/conf/lighttpd.conf (code=exited, status=0/SUCCESS)
Main PID: 12346 (lighttpd)
CGroup: /system.slice/lighttpd.service
└─12346 /fgeudb/lighttpd/sbin/lighttpd -f /fgeudb/lighttpd/conf/lighttpd.conf
5. Lighttpd配置文件详解
步骤1:创建主配置文件
# vi /fgeudb/lighttpd/conf/lighttpd.conf
server.document-root = “/fgeudb/lighttpd/www/html”
server.port = 80
server.username = “lighttpd”
server.groupname = “lighttpd”
server.pid-file = “/fgeudb/lighttpd/run/lighttpd.pid”
server.errorlog = “/fgeudb/lighttpd/logs/error.log”
server.breakagelog = “/fgeudb/lighttpd/logs/breakage.log”
index-file.names = ( “index.html”, “index.htm”, “index.php” )
server.modules = (
“mod_access”,
“mod_accesslog”,
“mod_rewrite”,
“mod_redirect”,
“mod_compress”,
“mod_fastcgi”,
“mod_cgi”,
“mod_proxy”,
“mod_ssl”
)
mimetype.assign = (
“.html” => “text/html”,
“.htm” => “text/html”,
“.css” => “text/css”,
“.js” => “text/javascript”,
“.json” => “application/json”,
“.png” => “image/png”,
“.jpg” => “image/jpeg”,
“.gif” => “image/gif”,
“.ico” => “image/x-icon”,
“.svg” => “image/svg+xml”,
“.pdf” => “application/pdf”,
“.txt” => “text/plain”,
“.xml” => “application/xml”,
“.zip” => “application/zip”
)
static-file.exclude-extensions = ( “.fcgi”, “.php”, “.rb”, “~”, “.inc” )
accesslog.filename = “/fgeudb/lighttpd/logs/access.log”
compress.cache-dir = “/fgeudb/lighttpd/cache/compress”
compress.filetype = ( “text/html”, “text/css”, “text/javascript”, “application/javascript” )
server.max-keep-alive-idle = 30
server.max-keep-alive-requests = 1000
server.max-read-idle = 60
server.max-write-idle = 360
server.max-fds = 65535
server.event-handler = “linux-sysepoll”
server.network-backend = “linux-sendfile”
步骤2:配置虚拟主机
$HTTP[“host”] == “www.fgedu.net.cn” {
server.document-root = “/fgeudb/lighttpd/www/fgedu”
server.errorlog = “/fgeudb/lighttpd/logs/fgedu-error.log”
accesslog.filename = “/fgeudb/lighttpd/logs/fgedu-access.log”
}
$HTTP[“host”] == “api.fgedu.net.cn” {
server.document-root = “/fgeudb/lighttpd/www/api”
server.errorlog = “/fgeudb/lighttpd/logs/api-error.log”
accesslog.filename = “/fgeudb/lighttpd/logs/api-access.log”
}
基于端口的虚拟主机:
$SERVER[“socket”] == “:8080” {
server.document-root = “/fgeudb/lighttpd/www/test”
server.errorlog = “/fgeudb/lighttpd/logs/test-error.log”
}
正则表达式匹配:
$HTTP[“host”] =~ “^(www\.)?fgedu\.net\.cn$” {
server.document-root = “/fgeudb/lighttpd/www/fgedu”
}
步骤3:配置URL重写
url.rewrite-if-not-file = (
“^/api/(.*)$” => “/api/index.php?request=$1”,
“^/user/([0-9]+)$” => “/user.php?id=$1”,
“^/article/([a-zA-Z0-9_-]+)$” => “/article.php?slug=$1”
)
URL重定向:
url.redirect = (
“^/old/(.*)$” => “http://www.fgedu.net.cn/new/$1”,
“^/blog$” => “http://blog.fgedu.net.cn/”
)
条件重写:
$HTTP[“host”] == “fgedu.net.cn” {
url.redirect = (
“^/(.*)$” => “http://www.fgedu.net.cn/$1”
)
}
6. Lighttpd FastCGI配置
步骤1:配置PHP FastCGI
# vi /fgeudb/lighttpd/conf/conf.d/fastcgi.conf
server.modules += ( “mod_fastcgi” )
fastcgi.server = (
“.php” => ((
“bin-path” => “/usr/bin/php-cgi”,
“socket” => “/fgeudb/lighttpd/run/php-fastcgi.socket”,
“max-procs” => 4,
“bin-environment” => (
“PHP_FCGI_CHILDREN” => “4”,
“PHP_FCGI_MAX_REQUESTS” => “10000”
),
“broken-scriptfilename” => “enable”,
“check-local” => “disable”
))
)
在主配置中包含:
include “/fgeudb/lighttpd/conf/conf.d/fastcgi.conf”
启动PHP-FPM模式(推荐生产环境):
fastcgi.server = (
“.php” => ((
“host” => “127.0.0.1”,
“port” => 9000,
“check-local” => “disable”
))
)
步骤2:配置Python FastCGI
fastcgi.server = (
“.py” => ((
“bin-path” => “/usr/bin/python3”,
“socket” => “/fgeudb/lighttpd/run/python-fastcgi.socket”,
“max-procs” => 2,
“check-local” => “disable”
))
)
配置Django应用:
fastcgi.server = (
“/” => ((
“host” => “127.0.0.1”,
“port” => 8000,
“check-local” => “disable”
))
)
步骤3:配置CGI
server.modules += ( “mod_cgi” )
cgi.assign = (
“.pl” => “/usr/bin/perl”,
“.cgi” => “/usr/bin/perl”,
“.py” => “/usr/bin/python3”,
“.sh” => “/bin/bash”
)
配置CGI目录:
alias.url = ( “/cgi-bin/” => “/fgeudb/lighttpd/www/cgi-bin/” )
$HTTP[“url”] =~ “^/cgi-bin/” {
cgi.assign = ( “” => “” )
}
7. Lighttpd SSL/HTTPS配置
步骤1:生成SSL证书
$ openssl genrsa -out /fgeudb/lighttpd/ssl/fgedu.key 2048
$ openssl req -new -key /fgeudb/lighttpd/ssl/fgedu.key -out /fgeudb/lighttpd/ssl/fgedu.csr \
-subj “/C=CN/ST=Beijing/L=Beijing/O=/CN=fgedu.net.cn”
$ openssl x509 -req -days 365 -in /fgeudb/lighttpd/ssl/fgedu.csr \
-signkey /fgeudb/lighttpd/ssl/fgedu.key -out /fgeudb/lighttpd/ssl/fgedu.crt
输出示例如下:
Signature ok
subject=C=CN, ST=Beijing, L=Beijing, O=, CN=fgedu.net.cn
Getting Private key
合并证书和私钥(Lighttpd需要):
$ cat /fgeudb/lighttpd/ssl/fgedu.key /fgeudb/lighttpd/ssl/fgedu.crt > /fgeudb/lighttpd/ssl/fgedu.pem
$ chmod 600 /fgeudb/lighttpd/ssl/fgedu.pem
步骤2:配置HTTPS服务器
server.modules += ( “mod_openssl” )
$SERVER[“socket”] == “:443” {
ssl.engine = “enable”
ssl.pemfile = “/fgeudb/lighttpd/ssl/fgedu.pem”
ssl.privkey = “/fgeudb/lighttpd/ssl/fgedu.key”
ssl.ca-file = “/fgeudb/lighttpd/ssl/ca.crt”
ssl.cipher-list = “ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384”
ssl.honor-cipher-order = “enable”
ssl.protocol = “TLSv1.2 TLSv1.3”
server.document-root = “/fgeudb/lighttpd/www/html”
server.errorlog = “/fgeudb/lighttpd/logs/ssl-error.log”
accesslog.filename = “/fgeudb/lighttpd/logs/ssl-access.log”
}
HTTP到HTTPS重定向:
$HTTP[“scheme”] == “http” {
$HTTP[“host”] =~ “^(.*)$” {
url.redirect = ( “^/(.*)$” => “https://%1/$1” )
}
}
步骤3:SSL安全配置
ssl.engine = “enable”
ssl.pemfile = “/fgeudb/lighttpd/ssl/fgedu.pem”
禁用不安全的SSL选项:
ssl.disable-client-renegotiation = “enable”
ssl.honor-cipher-order = “enable”
ssl.protocol = “TLSv1.2 TLSv1.3”
配置HSTS:
setenv.add-response-header = (
“Strict-Transport-Security” => “max-age=31536000; includeSubDomains”
)
配置OCSP Stapling:
ssl.stapling-file = “/fgeudb/lighttpd/ssl/ocsp.der”
8. 安装验证与测试
查看Lighttpd状态
$ ps -ef | grep lighttpd
输出示例如下:
lighttpd 12345 1 0 10:05 ? 00:00:00 /fgeudb/lighttpd/sbin/lighttpd -f /fgeudb/lighttpd/conf/lighttpd.conf
查看端口监听:
$ netstat -tlnp | grep lighttpd
输出示例如下:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12345/lighttpd
查看Lighttpd版本:
$ lighttpd -v
输出示例如下:
lighttpd/1.4.82 (ssl) – a light and fast webserver
Build-Date: Apr 4 2026 10:00:00
验证配置文件:
$ lighttpd -t -f /fgeudb/lighttpd/conf/lighttpd.conf
输出示例如下:
Syntax OK
访问测试
$ curl -I http://192.168.1.51
输出示例如下:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1234
Date: Fri, 04 Apr 2026 10:10:00 GMT
Server: lighttpd/1.4.82
测试HTTPS访问:
$ curl -I -k https://192.168.1.51
输出示例如下:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1234
Date: Fri, 04 Apr 2026 10:10:00 GMT
Server: lighttpd/1.4.82
测试PHP:
$ curl http://192.168.1.51/test.php
输出示例如下:
<html><body>
<h1>PHP is working!</h1>
<p>PHP Version: 8.2.0</p>
</body></html>
性能测试
$ ab -n 10000 -c 100 http://192.168.1.51/
输出示例如下:
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
…
Complete requests: 10000
Failed requests: 0
Total transferred: 12340000 bytes
HTML transferred: 6170000 bytes
Requests per second: 28543.21 [#/sec] (mean)
Time per request: 3.503 [ms] (mean)
Time per request: 0.035 [ms] (mean, across all concurrent requests)
Transfer rate: 3439.87 [Kbytes/sec] received
使用wrk进行压力测试:
$ wrk -t4 -c100 -d30s http://192.168.1.51/
输出示例如下:
Running 30s test @ http://192.168.1.51/
4 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 3.52ms 1.12ms 25.34ms 83.25%
Req/Sec 7.21k 489.56 8.56k 72.50%
861234 requests in 30.02s, 1.12GB read
Requests/sec: 28689.45
Transfer/sec: 38.21MB
9. 常见问题与解决方案
问题1:端口被占用
解决方案:
1. 查看端口占用:
$ netstat -tlnp | grep :80
2. 停止占用端口的进程:
# kill -9 $(lsof -t -i:80)
3. 或修改Lighttpd监听端口:
server.port = 8080
问题2:权限不足
解决方案:
1. 检查文件权限:
$ ls -la /fgeudb/lighttpd/
2. 修改目录所有者:
# chown -R lighttpd:lighttpd /fgeudb/lighttpd
3. 检查SELinux:
# getenforce
# setenforce 0
4. 检查日志目录权限:
# chmod 755 /fgeudb/lighttpd/logs
问题3:FastCGI连接失败
解决方案:
1. 检查PHP-FPM服务状态:
# systemctl status php-fpm
2. 检查FastCGI socket:
$ ls -la /fgeudb/lighttpd/run/*.socket
3. 检查PHP-FPM配置:
# vi /etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000
4. 重启PHP-FPM:
# systemctl restart php-fpm
问题4:SSL证书错误
解决方案:
1. 检查证书文件格式:
$ openssl x509 -in /fgeudb/lighttpd/ssl/fgedu.crt -text -noout
2. 检查私钥文件格式:
$ openssl rsa -in /fgeudb/lighttpd/ssl/fgedu.key -check
3. 合并证书和私钥:
$ cat fgedu.key fgedu.crt > fgedu.pem
4. 设置正确权限:
# chmod 600 /fgeudb/lighttpd/ssl/fgedu.pem
Lighttpd服务管理命令
# systemctl start lighttpd
或
$ /fgeudb/lighttpd/sbin/lighttpd -f /fgeudb/lighttpd/conf/lighttpd.conf
停止服务:
# systemctl stop lighttpd
或
$ kill -QUIT $(cat /fgeudb/lighttpd/run/lighttpd.pid)
重载配置:
# systemctl reload lighttpd
或
$ kill -HUP $(cat /fgeudb/lighttpd/run/lighttpd.pid)
测试配置:
$ lighttpd -t -f /fgeudb/lighttpd/conf/lighttpd.conf
查看版本:
$ lighttpd -v
查看帮助:
$ lighttpd -h
1. 使用Lighttpd 1.4.82最新稳定版本;2. 配置systemd服务实现自动重启;3. 使用FastCGI模式处理动态请求;4. 启用压缩减少传输流量;5. 配置SSL/HTTPS加密传输;6. 设置合理的超时和连接参数;7. 使用事件驱动模型提升性能;8. 配置完善的日志记录;9. 定期更新版本修复安全漏洞;10. 监控服务器资源使用情况。
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
