温馨提示×

centos apache为什么访问慢

小樊
38
2026-09-15 02:03:55
栏目: 智能运维

在 CentOS 上 Apache 访问慢 的原因很多,下面按「最常见 → 较少见」给你一个排查清单,你可以逐条对照。


一、最常见原因

1️⃣ DNS 解析慢(非常常见)

Apache 默认会尝试对客户端 IP 做 反向 DNS 解析。

✅ 现象:

  • 首次访问慢
  • 本地访问快,外网访问慢

✅ 解决: 编辑 Apache 配置:

vi /etc/httpd/conf/httpd.conf

修改或添加:

HostnameLookups Off

重启:

systemctl restart httpd

2️⃣ SELinux 导致延迟(CentOS 特有)

SELinux 开启时,Apache 访问文件/网络可能被拦截并等待。

✅ 检查:

getenforce

如果是 Enforcing,可临时测试:

setenforce 0

如果变快了,说明是 SELinux 问题。

✅ 正确做法(不关闭 SELinux):

setsebool -P httpd_can_network_connect on
restorecon -Rv /var/www

3️⃣ 防火墙 / 安全组延迟

  • firewalld
  • 云服务器安全组

✅ 检查:

systemctl status firewalld

二、Apache 本身配置问题

4️⃣ MPM 模式不合理

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>

5️⃣ 网站程序慢(PHP / 数据库)

Apache 本身没问题,但:

  • PHP 执行慢
  • MySQL 查询慢
  • 外部 API 超时

✅ 测试:

curl -w "%{time_total}\n" http://localhost

✅ 查看慢日志:

tail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log

三、系统资源问题

6️⃣ 内存不足 / 频繁 swap

free -h
top

如果 swap 频繁使用,Apache 会非常慢。


7️⃣ 磁盘 IO 瓶颈

iostat -x 1

如果 %util 接近 100%,说明磁盘慢。


四、网络问题

8️⃣ 公网带宽小 / 丢包

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

六、如果你愿意

你可以告诉我:

  1. CentOS 版本(7 / 8 / Stream)
  2. Apache 版本
  3. 是 静态页面慢 还是 PHP 网站慢
  4. 本地访问 & 外网访问是否都慢

我可以帮你 精准定位是哪一层的问题。

0 踩