在 CentOS 上 Apache 响应慢 的原因有很多,下面按最常见 → 较少见的顺序给你一个排查清单,基本能覆盖 90% 的情况。
Apache 默认会做 客户端 IP 的反向 DNS 解析。
表现
解决
grep HostnameLookups /etc/httpd/conf/httpd.conf
如果是:
HostnameLookups On
改为:
HostnameLookups Off
CentOS 默认 SELinux + firewalld。
检查 SELinux
getenforce
如果是 Enforcing,可临时测试:
setenforce 0
检查防火墙
systemctl status firewalld
CentOS 默认使用 prefork 模式,容易卡。
查看当前模式
httpd -V | grep MPM
优化 prefork(示例)
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 3000
</IfModule>
如果是 PHP 网站:
排查
top
看是不是 php-fpm 或 mysqld 占 CPU。
ping 你的服务器IP
traceroute 你的服务器IP
iostat -x 1
如果 %util 接近 100%,说明磁盘瓶颈。
free -h
如果有大量 swap 使用:
swapon --show
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
太长会占用连接。
du -sh /var/log/httpd
tail -f /var/log/httpd/error_log
tail -f /var/log/httpd/access_log
curl -w "%{time_total}\n" -o /dev/null http://localhost
你可以补充:
php-fpm我可以直接帮你给一套针对性优化方案。