在 CentOS 上让 Apache 稳定承载高并发
一 架构与 MPM 选择
httpd -V | grep -i mpmLoadModule mpm_event_module modules/mod_mpm_event.so,并注释其他 MPM 模块;重启 systemctl restart httpd。二 Apache 关键配置
<IfModule mpm_event_module> 内):StartServers 8
MinSpareThreads 64
MaxSpareThreads 256
ThreadsPerChild 64
ServerLimit 32
MaxRequestWorkers 2048
MaxConnectionsPerChild 10000
Timeout 30
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 2
计算关系:MaxRequestWorkers ≈ ServerLimit × ThreadsPerChild(上例为 32 × 64 = 2048)。<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/json
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
</IfModule>
CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/access_log.%Y%m%d 86400" combined
<Location /server-status>
SetHandler server-status
Require ip 192.168.1.0/24
</Location>
三 操作系统与内核参数
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
apache soft nofile 65535
apache hard nofile 65535
echo "fs.file-max = 1000000" >> /etc/sysctl.confsysctl -p 生效):net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0 # NAT/云环境建议关闭
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_max_tw_buckets = 20000
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 16384 16777216
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_max_syn_backlog = 4096
net.core.somaxconn = 4096
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_slow_start_after_idle = 0
echo noop > /sys/block/sda/queue/scheduler
echo 0 > /proc/sys/vm/swappiness
这些调整可缓解 TIME_WAIT 堆积、提升连接队列与网络吞吐,为高并发提供底层支撑。四 监控与压测
ss -tulnp | grep httpdnetstat -nat | grep -i "80" | wc -lnetstat -na | grep ESTABLISHED | wc -lnetstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'http://your-server/server-status?auto(需启用 mod_status)ab -n 1000 -c 100 http://your-server/index.htmlMaxRequestWorkers、内核参数联动微调。五 架构与扩展建议