在 CentOS 上 Apache 访问慢 的原因很多,下面按「最常见 → 较少见」给你一个排查清单,你可以逐条对照。
Apache 默认会尝试对客户端 IP 做 反向 DNS 解析。
✅ 现象:
✅ 解决: 编辑 Apache 配置:
vi /etc/httpd/conf/httpd.conf
修改或添加:
HostnameLookups Off
重启:
systemctl restart httpd
SELinux 开启时,Apache 访问文件/网络可能被拦截并等待。
✅ 检查:
getenforce
如果是 Enforcing,可临时测试:
setenforce 0
如果变快了,说明是 SELinux 问题。
✅ 正确做法(不关闭 SELinux):
setsebool -P httpd_can_network_connect on
restorecon -Rv /var/www
✅ 检查:
systemctl status firewalld
CentOS 默认可能是 prefork,并发差。
✅ 查看:
httpd -V | grep MPM
✅ 推荐(高并发):
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
Apache 本身没问题,但:
✅ 测试:
curl -w "%{time_total}\n" http://localhost
✅ 查看慢日志:
tail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log
free -h
top
如果 swap 频繁使用,Apache 会非常慢。
iostat -x 1
如果 %util 接近 100%,说明磁盘慢。
ping your_ip
mtr your_ip
getenforce
systemctl status firewalld
httpd -V | grep MPM
curl -w "%{time_total}\n" http://localhost
tail -f /var/log/httpd/error_log
你可以告诉我:
我可以帮你 精准定位是哪一层的问题。