CentOS 上优化 Apache2 性能
一 基线评估与准备
二 系统层面优化
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
fs.file-max = 6815744。net.core.somaxconn = 65535。net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 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
sudo sysctl -p /etc/sysctl.conf。vm.swappiness = 10
vm.overcommit_memory = 1
vm.dirty_ratio = 40
vm.dirty_background_ratio = 10
三 Apache MPM 与核心参数
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 1000
</IfModule>
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "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"
ExpiresByType application/javascript "access plus 30 days"
</IfModule>
sudo a2enmod http2 并在虚拟主机启用 Protocols h2 http/1.1。sudo a2enmod ssl 并配置有效证书。四 模块与内容分发优化
五 验证与持续监控
sudo httpd -tsudo systemctl restart httpdps aux | grep httpd、netstat -an | grep :80 | wc -ltop/vmstat/iostat/sar,关注CPU、内存、I/O、网络与TIME_WAIT变化。