一、Lighttpd概述
Lighttpd是一个轻量级的开源Web服务器,以低内存占用、低CPU负载和高性能著称,适合静态资源服务和嵌入式环境。
风哥风哥提示:
1.1 Lighttpd核心特性
- 轻量高效:内存占用低,适合资源受限环境
- FastCGI支持:高效的PHP等动态语言支持
- URL重写:灵活的URL重写规则
- 虚拟主机:支持基于名称和IP的虚拟主机
二、Lighttpd安装部署
2.1 YUM安装
# 安装EPEL仓库
yum install -y epel-release
# 安装Lighttpd
yum install -y lighttpd
# 安装PHP支持
yum install -y lighttpd-fastcgi php-fpm
# 启动服务
systemctl start lighttpd
systemctl enable lighttpd
# 查看版本
lighttpd -v
# 输出示例
lighttpd/1.4.67 (ssl) – a light and fast webserver
Build-Date: Jan 15 2024 10:30:00
lighttpd/1.4.67 (ssl) – a light and fast webserver
Build-Date: Jan 15 2024 10:30:00
2.2 源码编译安装
# 安装依赖
yum install -y gcc pcre-devel zlib-devel openssl-devel bzip2-devel
# 下载源码
wget https://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.67.tar.gz
tar -xzf lighttpd-1.4.67.tar.gz
cd lighttpd-1.4.67
# 配置编译
./configure \
--prefix=/usr/local/lighttpd \
--with-openssl \
--with-pcre \
--with-zlib \
--with-bzip2
# 编译安装
make && make install
# 创建配置目录
mkdir -p /usr/local/lighttpd/conf
mkdir -p /var/log/lighttpd
mkdir -p /var/run/lighttpd
# 创建用户
useradd -r -s /sbin/nologin lighttpd
chown -R lighttpd:lighttpd /var/log/lighttpd /var/run/lighttpd
三、主配置文件
3.1 基本配置
# /etc/lighttpd/lighttpd.conf
# 服务器文档根目录
server.document-root = "/var/www/lighttpd"
# 服务器用户和组
server.username = "lighttpd"
server.groupname = "lighttpd"
# 绑定端口
server.port = 80
# 绑定IP
server.bind = "0.0.0.0"
# 服务器标签
server.tag = "lighttpd"
# 索引文件
index-file.names = ( "index.html", "index.htm", "index.php" )
# 静态文件排除
static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" )
# 错误日志
server.errorlog = "/var/log/lighttpd/error.log"
# 访问日志
accesslog.filename = "/var/log/lighttpd/access.log"
# PID文件
server.pid-file = "/var/run/lighttpd/lighttpd.pid"
# 最大连接数
server.max-connections = 1024
# 最大请求大小
server.max-request-size = 65535
# 超时设置
server.max-read-idle = 60
server.max-write-idle = 360
server.max-keep-alive-idle = 5
# 包含模块配置
include "/etc/lighttpd/modules.conf"
include "/etc/lighttpd/conf.d/*.conf"
3.2 模块配置
# /etc/lighttpd/modules.conf
# 加载必要模块
server.modules = (
"mod_access",
"mod_accesslog",
"mod_alias",
"mod_compress",
"mod_redirect",
"mod_rewrite",
"mod_fastcgi",
"mod_status",
"mod_setenv"
)
# 压缩模块配置
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ( "text/plain", "text/html", "text/css", "application/javascript", "application/json" )
# 状态页面
status.status-url = "/server-status"
status.config-url = "/server-config"
status.statistics-url = "/server-statistics"
# 访问控制
$HTTP["remoteip"] !~ "127.0.0.1" {
$HTTP["url"] =~ "^/server-(status|config|statistics)" {
url.access-deny = ( "" )
}
}
四、虚拟主机配置
4.1 基于名称的虚拟主机
# /etc/lighttpd/conf.d/vhost.conf
# 简单虚拟主机
$HTTP["host"] == "www.fgedu.net.cn" {
server.document-root = "/var/www/example"
server.errorlog = "/var/log/lighttpd/example.error.log"
accesslog.filename = "/var/log/lighttpd/example.access.log"
}
# 多域名虚拟主机
$HTTP["host"] =~ "^(www\.)?example\.com$" {
server.document-root = "/var/www/example"
}
# 多个虚拟主机
$HTTP["host"] == "site1.fgedu.net.cn" {
server.document-root = "/var/www/site1"
}
$HTTP["host"] == "site2.fgedu.net.cn" {
server.document-root = "/var/www/site2"
}
# 正则匹配
$HTTP["host"] =~ "^([a-z0-9-]+)\.example\.com$" {
server.document-root = "/var/www/subdomains/" + "%1"
}
4.2 基于IP的虚拟主机
# 基于IP的虚拟主机
$SERVER["socket"] == "192.168.1.100:80" {
server.document-root = "/var/www/site1"
server.name = "site1.fgedu.net.cn"
}
$SERVER["socket"] == "192.168.1.101:80" {
server.document-root = "/var/www/site2"
server.name = "site2.fgedu.net.cn"
}
五、FastCGI配置
5.1 PHP-FPM集成
# /etc/lighttpd/conf.d/fastcgi.conf
# PHP-FPM配置
fastcgi.server = (
".php" => (
"fgedudb" => (
"socket" => "/run/php-fpm/www.sock",
"check-local" => "disable",
"broken-scriptfilename" => "enable",
"allow-x-send-file" => "enable"
)
)
)
# 多个PHP后端
fastcgi.server = (
".php" => (
"server1" => (
"host" => "127.0.0.1",
"port" => 9000
),
"server2" => (
"host" => "127.0.0.1",
"port" => 9001
)
)
)
# PHP-FPM配置示例
# /etc/php-fpm.d/www.conf
[www]
listen = /run/php-fpm/www.sock
listen.owner = lighttpd
listen.group = lighttpd
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
5.2 其他FastCGI应用
# Python FastCGI
fastcgi.server = (
".py" => (
"python" => (
"socket" => "/tmp/python-fcgi.sock",
"bin-path" => "/var/www/cgi-bin/python-fcgi.py",
"check-local" => "disable"
)
)
)
# Ruby FastCGI
fastcgi.server = (
".rb" => (
"ruby" => (
"socket" => "/tmp/ruby-fcgi.sock",
"bin-path" => "/var/www/cgi-bin/ruby-fcgi.rb",
"check-local" => "disable"
)
)
)
六、SSL/TLS配置
6.1 HTTPS配置
# 加载SSL模块
server.modules += ( "mod_openssl" )
# SSL虚拟主机
$SERVER["socket"] == ":443" {
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/ssl/fgedu.net.cn.pem"
ssl.privkey = "/etc/lighttpd/ssl/fgedu.net.cn.key"
ssl.ca-file = "/etc/lighttpd/ssl/ca-bundle.crt"
server.name = "www.fgedu.net.cn"
server.document-root = "/var/www/example"
# SSL协议设置
ssl.use-sslv2 = "disable"
ssl.use-sslv3 = "disable"
ssl.honor-cipher-order = "enable"
ssl.cipher-list = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256"
}
# HTTP重定向HTTPS
$HTTP["scheme"] == "http" {
$HTTP["host"] =~ "^(.*)$" {
url.redirect = ( "^/(.*)$" => "https://%1/$1" )
}
}
七、URL重写
7.1 基本重写规则
# 简单重定向
url.redirect = (
"^/old-page$" => "/new-page",
"^/blog/(.*)$" => "/articles/$1"
)
# 正则重写
url.rewrite-once = (
"^/api/([a-z]+)/([0-9]+)$" => "/api.php?module=$1&id=$2",
"^/user/([a-zA-Z0-9]+)$" => "/user.php?name=$1"
)
# 条件重写
$HTTP["host"] == "api.fgedu.net.cn" {
url.rewrite-once = (
"^/v1/(.*)$" => "/api/v1/$1",
"^/v2/(.*)$" => "/api/v2/$1"
)
}
# WordPress伪静态
url.rewrite-once = (
"^/(wp-.+).*/?" => "$0",
"^/(sitemap.xml)" => "$0",
"^/(xmlrpc.php)" => "$0",
"^/keyword/([A-Za-z_0-9\-]+)/?$" => "/index.php?keyword=$1",
"^/.*?(\?.+)?$" => "/index.php$1"
)
八、访问控制
8.1 目录访问控制
# 禁止访问特定目录
$HTTP["url"] =~ "^/(\.git|\.svn|\.ht)" {
url.access-deny = ( "" )
}
# IP访问控制
$HTTP["remoteip"] == "192.168.1.100" {
url.access-deny = ( "" )
}
# 允许特定IP访问管理目录
$HTTP["url"] =~ "^/admin/" {
$HTTP["remoteip"] !~ "^(192\.168\.1\.(100|101))$" {
url.access-deny = ( "" )
}
}
# 基于用户认证
auth.backend = "htpasswd"
auth.backend.htpasswd.userfile = "/etc/lighttpd/htpasswd"
auth.require = ( "" => (
"method" => "basic",
"realm" => "Protected Area",
"require" => "valid-user"
) )
# 对特定目录启用认证
$HTTP["url"] =~ "^/private/" {
auth.require = ( "" => (
"method" => "basic",
"realm" => "Private Area",
"require" => "user=admin"
) )
}
九、性能优化
9.1 服务器优化
# 连接优化
server.max-connections = 2048
server.max-fds = 4096
server.max-keep-alive-idle = 30
server.max-keep-alive-requests = 1000
# 网络优化
server.network-backend = "linux-sendfile"
server.event-handler = "linux-sysepoll"
# 请求优化
server.max-request-field-size = 8192
server.max-request-size = 65535
# 输出缓冲
server.stream-response-body = 1
# 文件缓存
server.stat-cache-engine = "simple"
# 压缩优化
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ( "text/plain", "text/html", "text/css", "application/javascript" )
compress.max-filesize = 65536
十、运维命令
10.1 常用管理命令
# 测试配置
lighttpd -t -f /etc/lighttpd/lighttpd.conf
# 启动服务
systemctl start lighttpd
# 停止服务
systemctl stop lighttpd
# 重启服务
systemctl restart lighttpd
# 重载配置
systemctl reload lighttpd
# 查看状态
systemctl status lighttpd
# 查看进程
ps -ef | grep lighttpd
# 查看端口
netstat -tlnp | grep lighttpd
# 查看版本
lighttpd -v
# 查看编译参数
lighttpd -V
十一、最佳实践
| 配置项 | 建议值 | 说明 |
|---|---|---|
| server.max-connections | 1024-2048 | 根据服务器配置调整 |
| compress | 启用 | 减少传输大小 |
| ssl.engine | 启用 | 生产环境必须HTTPS |
注意事项:
- 定期检查日志文件大小
- 配置文件修改后需重载
- 注意目录权限设置
- 启用必要的安全模块
十二、总结
Lighttpd是轻量高效的Web服务器。通过本培训文档,您应该掌握了:
- Lighttpd的安装部署
- 主配置文件和模块配置
- 虚拟主机配置
- FastCGI和PHP集成
- SSL/TLS安全配置
- URL重写和访问控制
IT运维培训文档系列 | 第255篇 | Lighttpd Web服务器培训
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
