在 CentOS 上优化 Apache(httpd) 性能,通常从 配置调优、系统参数、模块管理、缓存与压缩、前后端架构 几个方面入手。下面给你一套实用、可落地的优化方案(以 CentOS 7/8 + Apache 2.4 为例)。
Apache 有 3 种 MPM:
httpd -V | grep -i mpm
vim /etc/httpd/conf.modules.d/00-mpm.conf
确保如下内容启用(其他注释掉):
LoadModule mpm_event_module modules/mod_mpm_event.so
重启:
systemctl restart httpd
编辑配置文件(如不存在可新建):
vim /etc/httpd/conf.d/mpm.conf
<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 64
MaxSpareThreads 128
ThreadsPerChild 64
MaxRequestWorkers 512
MaxConnectionsPerChild 10000
</IfModule>
参数说明:
MaxRequestWorkers:最大并发连接数(核心)ThreadsPerChild:每个进程线程数MaxConnectionsPerChild:防止内存泄漏减少内存和攻击面:
httpd -M
常见可禁用模块(视业务而定):
#LoadModule rewrite_module
#LoadModule autoindex_module
#LoadModule status_module
编辑:
vim /etc/httpd/conf.modules.d/00-base.conf
vim /etc/httpd/conf/httpd.conf
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
KeepAliveTimeout 不建议太大(2–5 秒最佳)
vim /etc/httpd/conf.d/deflate.conf
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript
</IfModule>
vim /etc/httpd/conf.d/expires.conf
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 30 days"
ExpiresByType text/css "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"
</IfModule>
vim /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
vim /etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
生效:
sysctl -p
比 mod_php 更快更稳
yum install php-fpm
Apache 配置:
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
Nginx(静态+反向代理) → Apache(动态)
vim /etc/httpd/conf.d/status.conf
<Location /server-status>
SetHandler server-status
Require ip 127.0.0.1
</Location>
访问:
http://localhost/server-status
ab -n 10000 -c 500 http://localhost/
✅ 用 event MPM
✅ 合理设置 MaxRequestWorkers
✅ 开 KeepAlive + 压缩 + 缓存
✅ 用 PHP-FPM
✅ 必要时 Nginx 前置
如果你能告诉我:
我可以帮你给出 精准配置参数。