在 CentOS 上优化 Apache(httpd) 性能,可以从 系统层面、Apache 配置、模块选择、缓存与压缩、以及监控 几个方面入手。下面按实战优先级给你一套常用优化方案。
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
fs.file-max = 1000000
执行:
sysctl -p
# /etc/security/limits.conf
* soft nofile 100000
* hard nofile 100000
preforkevent(推荐)查看当前 MPM:
httpd -V | grep MPM
✅ 推荐:event(高并发)或 worker
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
ServerLimit 512
MaxClients 512
MaxRequestsPerChild 3000
</IfModule>
<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
ServerLimit 16
MaxRequestWorkers 400
MaxConnectionsPerChild 10000
</IfModule>
修改后:
systemctl restart httpd
httpd -M
常用可禁用:
status_module(生产可关)autoindex_moduleuserdir_moduleKeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
Timeout 30
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
<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>
<IfModule mod_cache.c>
CacheEnable disk /
CacheRoot /var/cache/httpd
</IfModule>
推荐架构:
Nginx (前端) -> Apache + PHP-FPM
或从 Apache 直接:
yum install php-fpm
CustomLog logs/access_log combined env=!dontlog
SetEnvIf Request_URI "\.(jpg|css|js)$" dontlog
systemctl status httpd
ss -ant | grep :80 | wc -l
yum install httpd-tools
ab -n 10000 -c 500 http://localhost/
✅ CentOS + Apache 优化最佳实践:
如果你愿意,可以告诉我:
我可以直接给你一套 可复制的 httpd.conf 配置。