CentOS 上 Apache 访问速度慢,通常不是单一原因,而是系统、网络、Apache 配置、应用代码共同作用的结果。下面按常见 → 少见但关键的顺序给你梳理。
Apache 默认会尝试对客户端 IP 做 反向 DNS 解析(HostnameLookups)。
✅ 解决方法:
HostnameLookups Off
位置:httpd.conf 或 conf.d/*.conf
AllowOverride All(.htaccess 拖慢)每一个请求都会去磁盘找 .htaccess,尤其在网站根目录很深时。
✅ 建议:
<Directory "/var/www/html">
AllowOverride None
</Directory>
prefork + PHP 模式(老但慢)CentOS 默认 Apache + mod_php + prefork:
✅ 优化或替换:
event MPMhttpd -V | grep MPM
SELinux 或 firewalld 规则不当会导致:
✅ 排查:
getenforce
systemctl status firewalld
✅ 检查:
ping your_ip
traceroute your_ip
导致每次加载都传大量数据。
✅ 开启:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
✅ 检查:
iostat -x 1
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100
Apache 本身没问题,是:
✅ 工具:
top
htop
mysqltuner
# 1. 看 MPM
httpd -V | grep MPM
# 2. 看当前连接
ss -ant | grep :80 | wc -l
# 3. 看负载
top
# 4. 看 Apache 错误日志
tail -f /var/log/httpd/error_log
你可以告诉我:
我可以直接给你一套最优 Apache 配置模板。