下面从 CentOS 上 Apache (httpd) 的性能调优 角度,系统性地给你整理一套常见、实用的优化方案,适合 生产环境。内容按 由易到难、由系统到应用 排序。
编辑:
vi /etc/security/limits.conf
添加:
* soft nofile 65535
* hard nofile 65535
查看:
ulimit -n
getenforce
setenforce 0
永久关闭:
vi /etc/selinux/config
SELINUX=disabled
systemctl disable firewalld --now
systemctl disable cups --now
httpd -V | grep MPM
CentOS 7/8 默认一般是:
vi /etc/httpd/conf.modules.d/00-mpm.conf
启用:
LoadModule mpm_event_module modules/mod_mpm_event.so
并注释掉:
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
vi /etc/httpd/conf/httpd.conf
<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 64
MaxRequestWorkers 800
MaxConnectionsPerChild 10000
</IfModule>
✅ 说明:
MaxRequestWorkers ≈ 并发连接数上限ThreadsPerChild 不宜过大(32–75)MaxConnectionsPerChild 防止内存泄漏KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100
✅ 建议:
常见可关闭:
# 注释掉
LoadModule autoindex_module
LoadModule userdir_module
LoadModule negotiation_module
LoadModule deflate_module modules/mod_deflate.so
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
LoadModule expires_module modules/mod_expires.so
<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>
CustomLog logs/access_log combined
# 或
# CustomLog off
减少日志级别:
LogLevel warn
yum install php-fpm
systemctl enable php-fpm
Apache 配置:
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
Nginx → Apache → PHP
✅ Nginx 处理静态 + 高并发
✅ Apache 只处理动态
RequestReadTimeout header=10-20,MinRate=500
ss -ant | grep :80 | wc -l
yum install httpd-tools
ab -n 10000 -c 500 http://localhost/
<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 64
MaxRequestWorkers 800
MaxConnectionsPerChild 10000
</IfModule>
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100
CentOS 上 Apache 调优 = 选用 event MPM + 合理 MaxRequestWorkers + 控制 KeepAlive + 关模块 + 日志优化 + 必要时用 Nginx 前置
如果你愿意,可以告诉我:
我可以 直接给你一套针对你业务的完整配置。