1. 首页 > 软件安装教程 > 正文

Lighttpd安装配置-Lighttpd Web服务器安装配置_升级迁移详细过程

1. Lighttpd概述与环境规划

Lighttpd是一款轻量级的开源Web服务器,由德国程序员Jan Kneschke开发。它以低内存占用、低CPU负载和高性能著称,特别适合高并发场景。更多学习教程www.fgedu.net.cn

1.1 Lighttpd版本说明

Lighttpd目前主要版本为1.4,本教程以Lighttpd 1.4为例进行详细讲解。

# 查看Lighttpd版本
$ lighttpd -v
lighttpd/1.4.73 (ssl) – a light and fast webserver

# 查看编译参数
$ lighttpd -V
lighttpd/1.4.73
Features:
+ IPv6 support
+ zlib support
+ bzip2 support
+ SSL support
+ large file support

# 检查配置语法
$ lighttpd -t -f /etc/lighttpd/lighttpd.conf
Syntax OK

1.2 环境规划

本次安装环境规划如下:

主机名:fgedudb01.fgedu.net.cn
IP地址:192.168.1.51
HTTP端口:80
HTTPS端口:443
安装目录:/usr/sbin/lighttpd
配置目录:/etc/lighttpd
日志目录:/var/log/lighttpd
网站根目录:/data/lighttpd/html

Lighttpd版本:1.4.73
OpenSSL版本:3.0.7

1.3 Lighttpd核心特性

主要特点:
1. 轻量级:内存占用极低,适合资源受限环境
2. 高性能:事件驱动架构,支持高并发
3. FastCGI:优秀的FastCGI支持
4. URL重写:支持mod_rewrite模块
5. 虚拟主机:支持基于IP、端口、域名的虚拟主机
6. SSL/TLS:支持HTTPS和SSL证书
7. 压缩:支持gzip、bzip2压缩
8. 模块化:丰富的模块支持

2. 硬件环境要求与检查

在安装Lighttpd之前,需要对服务器硬件环境进行全面检查。学习交流加群风哥微信: itpux-com

2.1 最低硬件要求

最低配置:
CPU:1核心
内存:128MB
磁盘:1GB

推荐配置(生产环境):
CPU:2核心以上
内存:1GB以上
磁盘:20GB以上

高并发配置:
CPU:4核心以上
内存:4GB以上
磁盘:50GB以上(SSD)

2.2 系统环境检查

# 检查操作系统版本
# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.8 (Ootpa)

# 检查内核版本
# uname -r
4.18.0-477.27.1.el8_8.x86_64

# 检查内存信息
# free -h
total used free shared buff/cache available
Mem: 15Gi 1.0Gi 13Gi 256Mi 1.0Gi 14Gi
Swap: 7Gi 0B 7Gi

# 检查磁盘空间
# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/vg_system-lv_root 100G 5.0G 95G 5% /
/dev/mapper/vg_data-lv_data 500G 50G 450G 10% /data

2.3 依赖包检查

# 检查依赖包
# rpm -qa | grep -E “pcre|zlib|openssl|bzip2”
pcre-devel-8.45-1.el8.x86_64
zlib-devel-1.2.11-25.el8.x86_64
openssl-devel-3.0.7-24.el8.x86_64
bzip2-devel-1.0.6-26.el8.x86_64

# 安装依赖包
# yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel bzip2 bzip2-devel

# 输出示例:
Last metadata expiration check: 0:00:00 ago on Sat Apr 4 10:00:00 2026.
Dependencies resolved.
Complete!

3. Lighttpd安装步骤

本节详细介绍Lighttpd 1.4的安装过程。学习交流加群风哥QQ113257174

3.1 创建用户和目录

# 创建lighttpd用户
# groupadd -g 83 lighttpd
# useradd -u 83 -g lighttpd -d /var/lib/lighttpd -s /sbin/nologin -M lighttpd

# 创建目录
# mkdir -p /etc/lighttpd/conf.d
# mkdir -p /var/log/lighttpd
# mkdir -p /var/lib/lighttpd
# mkdir -p /data/lighttpd/{html,ssl}

# 设置目录权限
# chown -R lighttpd:lighttpd /var/log/lighttpd
# chown -R lighttpd:lighttpd /var/lib/lighttpd
# chown -R lighttpd:lighttpd /data/lighttpd

3.2 安装Lighttpd

# 方法1:使用yum安装
# yum install -y lighttpd

# 输出示例:
Installed:
lighttpd-1.4.73-1.el8.x86_64

Complete!

# 方法2:源码编译安装
# cd /usr/local/src
# wget https://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.73.tar.gz
# tar -xzf lighttpd-1.4.73.tar.gz
# cd lighttpd-1.4.73

# 配置编译选项
# ./configure \
–prefix=/usr \
–sysconfdir=/etc/lighttpd \
–libdir=/usr/lib64/lighttpd \
–with-openssl \
–with-pcre \
–with-zlib \
–with-bzip2

# 编译安装
# make -j$(nproc)
# make install

# 验证安装
$ lighttpd -v
lighttpd/1.4.73 (ssl) – a light and fast webserver

3.3 创建配置文件

# 创建主配置文件
# vi /etc/lighttpd/lighttpd.conf

server.document-root = “/data/lighttpd/html”
server.port = 80
server.username = “lighttpd”
server.groupname = “lighttpd”
server.pid-file = “/var/run/lighttpd.pid”
server.errorlog = “/var/log/lighttpd/error.log”

index-file.names = ( “index.html”, “index.htm” )

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”
)

server.modules = (
“mod_access”,
“mod_accesslog”,
“mod_compress”
)

server.max-connections = 1024
server.max-fds = 2048
server.event-handler = “linux-sysepoll”

# 验证配置
$ lighttpd -t -f /etc/lighttpd/lighttpd.conf
Syntax OK

# 创建测试页面
# echo “Welcome to Lighttpd on fgedudb01.fgedu.net.cn” > /data/lighttpd/html/index.html

3.4 创建systemd服务

# 创建systemd服务文件
# vi /usr/lib/systemd/system/lighttpd.service

[Unit]
Description=Lighttpd Daemon
After=network.target

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

[Install]
WantedBy=multi-user.target

# 重载systemd
# systemctl daemon-reload

# 启动Lighttpd
# systemctl start lighttpd

# 设置开机自启
# systemctl enable lighttpd

# 检查状态
# systemctl status lighttpd

# 输出示例:
● lighttpd.service – Lighttpd Daemon
Loaded: loaded (/usr/lib/systemd/system/lighttpd.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2026-04-04 10:00:00 CST; 1s ago
Main PID: 12345 (lighttpd)
Tasks: 1 (limit: 49134)
Memory: 1.5M
CGroup: /system.slice/lighttpd.service
└─12345 /usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf

3.5 配置防火墙

# 配置防火墙
# firewall-cmd –permanent –add-service=http
success
# firewall-cmd –permanent –add-service=https
success
# firewall-cmd –reload
success

# 验证安装
$ curl -I http://192.168.1.51

# 输出示例:
HTTP/1.1 200 OK
Server: lighttpd/1.4.73
Date: Sat, 04 Apr 2026 02:00:00 GMT
Content-Type: text/html
Content-Length: 50
Connection: keep-alive

风哥提示:Lighttpd安装非常轻量,资源占用极低。建议使用yum安装,便于后续升级维护。

4. Lighttpd参数配置

Lighttpd参数配置是性能优化的关键步骤,直接影响系统性能。更多学习教程公众号风哥教程itpux_com

4.1 主配置文件

# 编辑主配置文件
# vi /etc/lighttpd/lighttpd.conf

server.document-root = “/data/lighttpd/html”
server.port = 80
server.username = “lighttpd”
server.groupname = “lighttpd”
server.pid-file = “/var/run/lighttpd.pid”
server.errorlog = “/var/log/lighttpd/error.log”

server.tag = “lighttpd”

index-file.names = ( “index.html”, “index.htm”, “index.php” )

server.modules = (
“mod_access”,
“mod_accesslog”,
“mod_compress”,
“mod_deflate”,
“mod_expire”,
“mod_rewrite”,
“mod_redirect”
)

# 访问日志
accesslog.filename = “/var/log/lighttpd/access.log”

# 网络配置
server.max-connections = 4096
server.max-fds = 8192
server.max-keep-alive-requests = 1000
server.max-keep-alive-idle = 30
server.max-read-idle = 60
server.max-write-idle = 360

# 事件处理
server.event-handler = “linux-sysepoll”
server.network-backend = “linux-sendfile”

# 压缩配置
compress.cache-dir = “/var/lib/lighttpd/compress”
compress.filetype = ( “text/html”, “text/css”, “text/javascript”, “application/javascript” )

# 验证配置
$ lighttpd -t -f /etc/lighttpd/lighttpd.conf
Syntax OK

# 重载配置
# systemctl reload lighttpd

4.2 性能优化配置

# 性能优化配置
# vi /etc/lighttpd/lighttpd.conf

server.document-root = “/data/lighttpd/html”
server.port = 80

# 工作进程(多进程模式)
server.max-worker = 4

# 连接配置
server.max-connections = 8192
server.max-fds = 16384
server.max-keep-alive-requests = 10000
server.max-keep-alive-idle = 60

# 超时配置
server.max-read-idle = 30
server.max-write-idle = 120
server.max-connection-idle = 300

# 事件处理
server.event-handler = “linux-sysepoll”
server.network-backend = “linux-sendfile”

# TCP配置
server.listen-backlog = 4096

# 禁用DNS反向解析
server.reject-expect-100-with-417 = “disable”

# 压缩配置
compress.cache-dir = “/var/lib/lighttpd/compress”
compress.filetype = (
“text/html”,
“text/plain”,
“text/css”,
“text/javascript”,
“application/javascript”,
“application/json”
)
compress.max-filesize = 1048576

# 缓存过期
expire.url = (
“/css/” => “access plus 7 days”,
“/js/” => “access plus 7 days”,
“/images/” => “access plus 30 days”
)

# 重载配置
# systemctl reload lighttpd

生产环境建议:生产环境建议启用压缩、缓存过期等优化配置。根据服务器资源调整连接数和进程数。

5. 虚拟主机配置

Lighttpd支持基于IP、端口和域名的虚拟主机配置,本节介绍常用的配置方法。from:www.itpux.com

5.1 基于域名的虚拟主机

# 创建虚拟主机配置
# vi /etc/lighttpd/conf.d/vhost.conf

# 加载模块
server.modules += ( “mod_evhost” )

# 虚拟主机配置
$HTTP[“host”] == “www.fgedu.net.cn” {
server.document-root = “/data/lighttpd/html/fgedu”
server.errorlog = “/var/log/lighttpd/fgedu_error.log”
accesslog.filename = “/var/log/lighttpd/fgedu_access.log”
}

$HTTP[“host”] == “api.fgedu.net.cn” {
server.document-root = “/data/lighttpd/html/api”
server.errorlog = “/var/log/lighttpd/api_error.log”
accesslog.filename = “/var/log/lighttpd/api_access.log”
}

# 通配符虚拟主机
$HTTP[“host”] =~ “^(www\.)?fgedu\.net\.cn$” {
server.document-root = “/data/lighttpd/html/fgedu”
}

# 创建网站目录
# mkdir -p /data/lighttpd/html/fgedu
# echo “Welcome to www.fgedu.net.cn” > /data/lighttpd/html/fgedu/index.html

# 在主配置中包含虚拟主机配置
# vi /etc/lighttpd/lighttpd.conf
include “conf.d/vhost.conf”

# 验证并重载配置
$ lighttpd -t -f /etc/lighttpd/lighttpd.conf
Syntax OK
# systemctl reload lighttpd

5.2 基于端口的虚拟主机

# 基于端口的虚拟主机配置
# vi /etc/lighttpd/conf.d/port.conf

$SERVER[“socket”] == “:8080” {
server.document-root = “/data/lighttpd/html/8080”
server.errorlog = “/var/log/lighttpd/port8080_error.log”
accesslog.filename = “/var/log/lighttpd/port8080_access.log”
}

$SERVER[“socket”] == “:9090” {
server.document-root = “/data/lighttpd/html/9090”
server.errorlog = “/var/log/lighttpd/port9090_error.log”
accesslog.filename = “/var/log/lighttpd/port9090_access.log”
}

# 创建网站目录
# mkdir -p /data/lighttpd/html/8080
# echo “Welcome to port 8080” > /data/lighttpd/html/8080/index.html

# 在主配置中包含
# vi /etc/lighttpd/lighttpd.conf
include “conf.d/port.conf”

# 重载配置
# systemctl reload lighttpd

# 测试访问
$ curl http://192.168.1.51:8080
Welcome to port 8080

5.3 HTTPS虚拟主机

# 生成SSL证书
# mkdir -p /data/lighttpd/ssl
# openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /data/lighttpd/ssl/fgedu.key \
-out /data/lighttpd/ssl/fgedu.crt \
-subj “/C=CN/ST=BJ/L=BJ/O=FGedu/OU=IT/CN=www.fgedu.net.cn”

# HTTPS配置
# vi /etc/lighttpd/conf.d/ssl.conf

$SERVER[“socket”] == “:443” {
ssl.engine = “enable”
ssl.pemfile = “/data/lighttpd/ssl/fgedu.pem”
ssl.privkey = “/data/lighttpd/ssl/fgedu.key”

server.document-root = “/data/lighttpd/html/fgedu”
server.errorlog = “/var/log/lighttpd/ssl_error.log”
accesslog.filename = “/var/log/lighttpd/ssl_access.log”
}

# 合并证书文件
# cat /data/lighttpd/ssl/fgedu.crt > /data/lighttpd/ssl/fgedu.pem
# cat /data/lighttpd/ssl/fgedu.key >> /data/lighttpd/ssl/fgedu.pem

# HTTP重定向到HTTPS
# vi /etc/lighttpd/conf.d/redirect.conf

$HTTP[“scheme”] == “http” {
$HTTP[“host”] =~ “^(.*)$” {
url.redirect = ( “^/(.*)” => “https://%1/$1” )
}
}

# 在主配置中包含
# vi /etc/lighttpd/lighttpd.conf
include “conf.d/ssl.conf”
include “conf.d/redirect.conf”

# 重载配置
# systemctl reload lighttpd

# 测试HTTPS
$ curl -k https://www.fgedu.net.cn
Welcome to www.fgedu.net.cn

6. 反向代理配置

Lighttpd支持反向代理功能,本节介绍常用的配置方法。更多学习教程www.fgedu.net.cn

6.1 基本反向代理

# 加载代理模块
# vi /etc/lighttpd/lighttpd.conf
server.modules += ( “mod_proxy” )

# 反向代理配置
# vi /etc/lighttpd/conf.d/proxy.conf

$HTTP[“host”] == “api.fgedu.net.cn” {
proxy.server = (
“” => (
(
“host” => “192.168.1.51”,
“port” => 8080
)
)
)

proxy.header = (
“https” => “enable”,
“upgrade” => “enable”
)

server.errorlog = “/var/log/lighttpd/proxy_error.log”
accesslog.filename = “/var/log/lighttpd/proxy_access.log”
}

# 在主配置中包含
# vi /etc/lighttpd/lighttpd.conf
include “conf.d/proxy.conf”

# 重载配置
# systemctl reload lighttpd

6.2 负载均衡配置

# 负载均衡配置
# vi /etc/lighttpd/conf.d/lb.conf

$HTTP[“host”] == “lb.fgedu.net.cn” {
proxy.server = (
“” => (
( “host” => “192.168.1.51”, “port” => 8080 ),
( “host” => “192.168.1.52”, “port” => 8080 ),
( “host” => “192.168.1.53”, “port” => 8080 )
)
)

proxy.balance = “round-robin”
# proxy.balance = “hash”
# proxy.balance = “fair”

server.errorlog = “/var/log/lighttpd/lb_error.log”
accesslog.filename = “/var/log/lighttpd/lb_access.log”
}

# 在主配置中包含
# vi /etc/lighttpd/lighttpd.conf
include “conf.d/lb.conf”

# 重载配置
# systemctl reload lighttpd

6.3 FastCGI配置

# FastCGI配置(PHP)
# vi /etc/lighttpd/conf.d/fastcgi.conf

server.modules += ( “mod_fastcgi” )

fastcgi.server = (
“.php” => ((
“bin-path” => “/usr/bin/php-cgi”,
“socket” => “/var/run/php-fcgi.sock”,
“max-procs” => 4,
“bin-environment” => (
“PHP_FCGI_CHILDREN” => “4”,
“PHP_FCGI_MAX_REQUESTS” => “10000”
),
“broken-scriptfilename” => “enable”
))
)

# PHP虚拟主机
$HTTP[“host”] == “php.fgedu.net.cn” {
server.document-root = “/data/lighttpd/html/php”

fastcgi.server = (
“.php” => ((
“host” => “127.0.0.1”,
“port” => 9000
))
)
}

# 创建PHP测试页面
# mkdir -p /data/lighttpd/html/php
# echo “” > /data/lighttpd/html/php/index.php

# 在主配置中包含
# vi /etc/lighttpd/lighttpd.conf
include “conf.d/fastcgi.conf”

# 重载配置
# systemctl reload lighttpd

风哥提示:Lighttpd的FastCGI支持非常优秀,特别适合PHP应用。建议配置多个FastCGI进程提升性能。

7. 安全配置

Lighttpd安全配置是保护Web服务的重要措施,本节介绍常用的安全配置方法。学习交流加群风哥微信: itpux-com

7.1 基本安全配置

# 安全配置
# vi /etc/lighttpd/conf.d/security.conf

# 隐藏版本号
server.tag = “WebServer”

# 禁止访问敏感文件
url.access-deny = ( “~”, “.inc”, “.bak”, “.sql”, “.config”, “.htaccess”, “.htpasswd” )

# 禁止目录列表
dir-listing.activate = “disable”

# 限制请求方法
$HTTP[“request-method”] !~ “^(GET|HEAD|POST)$” {
url.access-deny = ( “” )
}

# 安全头部
setenv.add-response-header = (
“X-Content-Type-Options” => “nosniff”,
“X-Frame-Options” => “SAMEORIGIN”,
“X-XSS-Protection” => “1; mode=block”,
“Referrer-Policy” => “strict-origin-when-cross-origin”
)

# 禁止访问隐藏文件
$HTTP[“url”] =~ “^/\.” {
url.access-deny = ( “” )
}

# 在主配置中包含
# vi /etc/lighttpd/lighttpd.conf
include “conf.d/security.conf”

# 重载配置
# systemctl reload lighttpd

7.2 访问控制

# IP访问控制
# vi /etc/lighttpd/conf.d/access.conf

# 加载模块
server.modules += ( “mod_access” )

# 允许指定IP访问
$HTTP[“url”] =~ “^/admin/” {
$HTTP[“remoteip”] !~ “^(192\.168\.1\.|10\.)” {
url.access-deny = ( “” )
}
}

# 基于密码的访问控制
server.modules += ( “mod_auth”, “mod_authn_file” )

auth.backend = “htpasswd”
auth.backend.htpasswd.userfile = “/etc/lighttpd/.htpasswd”

$HTTP[“url”] =~ “^/private/” {
auth.require = ( “” => (
“method” => “basic”,
“realm” => “Restricted Area”,
“require” => “valid-user”
))
}

# 创建密码文件
# htpasswd -c /etc/lighttpd/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin

# 在主配置中包含
# vi /etc/lighttpd/lighttpd.conf
include “conf.d/access.conf”

# 重载配置
# systemctl reload lighttpd

7.3 限流配置

# 限流配置
# vi /etc/lighttpd/conf.d/limit.conf

server.modules += ( “mod_evasive” )

# 连接限制
evasive.max-conns-per-ip = 20

# 请求限制(需要编译时启用)
# evasive.enable = “enable”
# evasive.max-conns-per-ip = 20
# evasive.silent = “enable”

# 带宽限制
server.modules += ( “mod_throttle” )

$HTTP[“url”] =~ “^/download/” {
throttle.ip = (
“” => ( “rate” => 1048576 ) # 1MB/s
)
}

# 在主配置中包含
# vi /etc/lighttpd/lighttpd.conf
include “conf.d/limit.conf”

# 重载配置
# systemctl reload lighttpd

生产环境建议:生产环境建议配置安全头部、访问控制和限流策略,保护Web服务免受攻击。

8. 监控与日志

Lighttpd提供了完善的监控和日志功能,本节介绍常用的监控配置方法。更多学习教程公众号风哥教程itpux_com

8.1 状态监控

# 启用状态页面
# vi /etc/lighttpd/conf.d/status.conf

server.modules += ( “mod_status” )

status.status-url = “/server-status”
status.config-url = “/server-config”
status.statistics-url = “/server-statistics”

# 限制访问
$HTTP[“url”] =~ “^/server-” {
$HTTP[“remoteip”] !~ “^(192\.168\.1\.|127\.0\.0\.)” {
url.access-deny = ( “” )
}
}

# 在主配置中包含
# vi /etc/lighttpd/lighttpd.conf
include “conf.d/status.conf”

# 重载配置
# systemctl reload lighttpd

# 访问状态页面
$ curl http://192.168.1.51/server-status

# 输出示例:
Total Traffic: 1048576 bytes
Total Requests: 1000
Busy Servers: 5
Idle Servers: 10

8.2 日志配置

# 日志配置
# vi /etc/lighttpd/lighttpd.conf

# 错误日志
server.errorlog = “/var/log/lighttpd/error.log”

# 访问日志
accesslog.filename = “/var/log/lighttpd/access.log”

# 自定义日志格式
accesslog.format = “%h %V %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\””

# JSON格式日志
accesslog.format = “{\”time\”:\”%{%Y-%m-%dT%H:%M:%S}t\”,\”remote\”:\”%h\”,\”method\”:\”%m\”,\”uri\”:\”%U\”,\”status\”:%>s,\”size\”:%b}”

# 查看日志
$ tail -f /var/log/lighttpd/access.log

# 输出示例:
192.168.1.100 www.fgedu.net.cn – [04/Apr/2026:10:00:00 +0800] “GET / HTTP/1.1” 200 615 “-” “Mozilla/5.0”

# 日志轮转配置
# vi /etc/logrotate.d/lighttpd

/var/log/lighttpd/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 0640 lighttpd lighttpd
postrotate
systemctl reload lighttpd > /dev/null 2>&1 || true
endscript
}

8.3 日志分析

# 统计访问量
$ cat /var/log/lighttpd/access.log | wc -l
10000

# 统计IP访问量
$ awk ‘{print $1}’ /var/log/lighttpd/access.log | sort | uniq -c | sort -rn | head -10

# 输出示例:
500 192.168.1.100
300 192.168.1.101
200 192.168.1.102

# 统计HTTP状态码
$ awk ‘{print $9}’ /var/log/lighttpd/access.log | sort | uniq -c | sort -rn

# 输出示例:
8000 200
1000 304
500 404
200 500

# 统计请求路径
$ awk ‘{print $7}’ /var/log/lighttpd/access.log | sort | uniq -c | sort -rn | head -10

# 输出示例:
3000 /
1500 /api/users
1000 /api/products

风哥提示:生产环境建议配置自定义日志格式,便于日志分析。定期检查日志发现异常访问。

9. 升级与迁移

Lighttpd升级和迁移是运维工作中的重要环节,需要仔细规划和执行。from:www.itpux.com

9.1 版本升级

# 查看当前版本
$ lighttpd -v
lighttpd/1.4.72 (ssl) – a light and fast webserver

# 备份配置
# cp -r /etc/lighttpd /backup/lighttpd_conf_$(date +%Y%m%d)

# 升级Lighttpd
# yum update lighttpd

# 输出示例:
Upgraded:
lighttpd-1.4.73-1.el8.x86_64

Complete!

# 验证版本
$ lighttpd -v
lighttpd/1.4.73 (ssl) – a light and fast webserver

# 验证配置
$ lighttpd -t -f /etc/lighttpd/lighttpd.conf
Syntax OK

# 重启服务
# systemctl restart lighttpd

9.2 配置迁移

# 备份配置
# tar -czf lighttpd_backup_$(date +%Y%m%d).tar.gz \
/etc/lighttpd \
/var/log/lighttpd \
/var/lib/lighttpd

# 迁移到新服务器
# scp lighttpd_backup_*.tar.gz root@newserver:/backup/

# 在新服务器解压
# tar -xzf lighttpd_backup_*.tar.gz -C /

# 验证配置
$ lighttpd -t -f /etc/lighttpd/lighttpd.conf
Syntax OK

# 启动服务
# systemctl start lighttpd

生产环境建议:升级前必须进行完整备份。Lighttpd配置向后兼容,但建议验证配置后再重启服务。

10. 生产环境实战案例

本节提供一个完整的生产环境配置案例,帮助读者更好地理解Lighttpd的实际应用。更多学习教程www.fgedu.net.cn

10.1 生产环境完整配置

# 生产环境主配置
# vi /etc/lighttpd/lighttpd.conf

server.document-root = “/data/lighttpd/html”
server.port = 80
server.username = “lighttpd”
server.groupname = “lighttpd”
server.pid-file = “/var/run/lighttpd.pid”
server.errorlog = “/var/log/lighttpd/error.log”

server.tag = “WebServer”

index-file.names = ( “index.html”, “index.htm”, “index.php” )

server.modules = (
“mod_access”,
“mod_accesslog”,
“mod_compress”,
“mod_deflate”,
“mod_expire”,
“mod_rewrite”,
“mod_redirect”,
“mod_status”,
“mod_evasive”
)

# 访问日志
accesslog.filename = “/var/log/lighttpd/access.log”
accesslog.format = “{\”time\”:\”%{%Y-%m-%dT%H:%M:%S}t\”,\”remote\”:\”%h\”,\”method\”:\”%m\”,\”uri\”:\”%U\”,\”status\”:%>s,\”size\”:%b}”

# 网络配置
server.max-connections = 8192
server.max-fds = 16384
server.max-keep-alive-requests = 10000
server.max-keep-alive-idle = 60

# 事件处理
server.event-handler = “linux-sysepoll”
server.network-backend = “linux-sendfile”

# 压缩配置
compress.cache-dir = “/var/lib/lighttpd/compress”
compress.filetype = ( “text/html”, “text/css”, “text/javascript”, “application/json” )

# 连接限制
evasive.max-conns-per-ip = 50

# 包含其他配置
include “conf.d/vhost.conf”
include “conf.d/proxy.conf”
include “conf.d/security.conf”

10.2 高性能PHP应用配置

# 高性能PHP应用配置
# vi /etc/lighttpd/conf.d/php.conf

server.modules += ( “mod_fastcgi” )

$HTTP[“host”] == “php.fgedu.net.cn” {
server.document-root = “/data/lighttpd/html/php”

fastcgi.server = (
“.php” => ((
“host” => “127.0.0.1”,
“port” => 9000,
“max-procs” => 8,
“bin-environment” => (
“PHP_FCGI_CHILDREN” => “8”,
“PHP_FCGI_MAX_REQUESTS” => “10000”
),
“broken-scriptfilename” => “enable”,
“allow-x-send-file” => “enable”
))
)

# 缓存过期
expire.url = (
“/static/” => “access plus 7 days”
)

# 压缩
compress.filetype += ( “application/json” )

server.errorlog = “/var/log/lighttpd/php_error.log”
accesslog.filename = “/var/log/lighttpd/php_access.log”
}

10.3 性能调优实战

# 操作系统优化
# vi /etc/sysctl.d/99-lighttpd.conf

net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
fs.file-max = 2097152

# 使配置生效
# sysctl -p /etc/sysctl.d/99-lighttpd.conf

# 用户限制
# vi /etc/security/limits.d/lighttpd.conf

lighttpd soft nofile 65535
lighttpd hard nofile 65535

# 压力测试
$ ab -n 100000 -c 1000 http://192.168.1.51/

# 输出示例:
Server Software: WebServer
Server Hostname: 192.168.1.51
Server Port: 80

Concurrency Level: 1000
Time taken for tests: 10.000 seconds
Complete requests: 100000
Failed requests: 0
Requests per second: 10000.00 [#/sec] (mean)
Time per request: 100.000 [ms] (mean)

风哥提示:Lighttpd作为轻量级Web服务器,在资源受限环境中表现优异。建议根据业务场景优化配置,充分利用其FastCGI和压缩功能。

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息