Apache2 在 Linux 上的性能优化指南
一 基线检查与模块准备
二 选择并配置合适的 MPM
<IfModule mpm_prefork_module>
StartServers 10
MinSpareServers 30
MaxSpareServers 45
MaxRequestWorkers 1000
MaxConnectionsPerChild 3000
</IfModule>
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>
三 关键运行时参数与静态资源优化
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
Timeout 5
# Gzip 压缩
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript
</IfModule>
# 浏览器缓存
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 30 days"
ExpiresByType application/javascript "access plus 30 days"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
</IfModule>
SSLEngine on
SSLSessionCache "shmcb:/var/cache/mod_ssl/scache(512000)"
SSLCompression off
四 操作系统与网络栈优化
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
net.core.somaxconn = 65535
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
vm.swappiness = 10
fs.file-max = 6815744
应用:执行 sysctl -p。五 验证 监控与回滚