在 CentOS 上优化 Apache(通常是 httpd),一般从系统层面、Apache 配置、模块管理、缓存、安全与监控几个方面入手。下面按实用性给你一套常见优化方案(以 CentOS 7/8 + Apache 2.4 为例)。
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
fs.file-max = 1000000
执行:
sysctl -p
ulimit -n 65535
永久生效:
/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
httpd -V | grep MPM
event(高并发)或 worker编辑:
/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>
如果是老程序(如部分 PHP),可用
prefork
编辑:
/etc/httpd/conf/httpd.conf
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
Timeout 30
常见可关闭:
#LoadModule status_module
#LoadModule autoindex_module
#LoadModule userdir_module
yum install mod_deflate
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
yum install mod_expires
<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>
yum install php-fpm
Apache 使用:
ProxyPassMatch ^/(.*\.php)$ unix:/run/php-fpm/www.sock|fcgi://localhost/var/www/html/$1
PHP-FPM 配置优化:
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
CustomLog /dev/null common
/etc/logrotate.d/httpd
<IfModule mod_reqtimeout.c>
RequestReadTimeout header=10-20,MinRate=500
</IfModule>
ServerTokens Prod
ServerSignature Off
systemctl status httpd
apachectl -t
yum install httpd-tools
ab -n 1000 -c 100 http://localhost/
✅ CentOS + Apache 2.4
✅ MPM event
✅ PHP-FPM
✅ gzip + expires
✅ KeepAlive 5s
✅ 文件句柄 65535
如果你愿意,可以告诉我:
我可以给你一套针对性配置文件。