在 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
httpd -V | grep MPM
vi /etc/httpd/conf.modules.d/00-mpm.conf
示例(event):
LoadModule mpm_event_module modules/mod_mpm_event.so
<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 10000
</IfModule>
httpd -M
禁用示例:
# LoadModule status_module modules/mod_status.so
# LoadModule autoindex_module modules/mod_autoindex.so
Timeout 30
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
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"
</IfModule>
yum install php-fpm
Apache 配置:
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
LoadModule cache_module modules/mod_cache.so
LoadModule cache_disk_module modules/mod_cache_disk.so
CacheEnable disk /
CacheRoot /var/cache/httpd
httpd -S
systemctl status httpd
yum install httpd-tools
ab -n 10000 -c 100 http://localhost/
❌ 盲目调大 MaxRequestWorkers
❌ prefork + PHP 模块(内存爆炸)
❌ 开启所有模块
✅ event + php-fpm
✅ 动静分离
如果你能提供:
我可以给你一套针对你服务器的具体配置。