在 CentOS 上解决 Apache 访问慢 / 响应慢 的问题,通常要从 系统资源、Apache 配置、PHP/数据库、网络 几个方面入手。下面给你一套实用排查与优化思路(从快到慢、从常见到深入)。
time curl -I http://localhost/
top
free -h
df -h
iostat -x 1
重点看:
查看当前 MPM:
httpd -V | grep MPM
编辑:
/etc/httpd/conf.modules.d/00-mpm.conf
✅ 内存小(<2G)用 prefork
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 3000
</IfModule>
✅ 高并发用 event(推荐)
LoadModule mpm_event_module modules/mod_mpm_event.so
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 10000
</IfModule>
修改后:
systemctl restart httpd
httpd -M
禁用不用的模块(如 status, proxy 不用就关)
/etc/httpd/conf/httpd.conf
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
CentOS 上 Apache + mod_php 很容易慢。
安装:
yum install php-fpm
Apache 使用:
yum install mod_proxy_fcgi
配置示例:
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
重启:
systemctl restart php-fpm httpd
/etc/php-fpm.d/www.conf
slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 2s
tail -f /var/log/php-fpm/slow.log
/etc/my.cnf
slow_query_log = 1
slow_query_log_file = /var/log/mysql-slow.log
long_query_time = 1
ulimit -n
修改:
/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
/etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
sysctl -p
HostnameLookups Off
setenforce 0
✅ top / free / iostat
✅ httpd -V 看 MPM
✅ 开 PHP-FPM
✅ 看 php slow log
✅ 看 mysql slow log
✅ KeepAlive 设置
✅ 关 HostnameLookups
如果你愿意,可以把下面信息发我,我可以直接帮你给一套精准配置: