Ubuntu 上 Apache2 启动或请求加载慢,常见原因和排查方向如下(按出现频率排序):
Apache 默认会对客户端 IP 做 反向 DNS 解析。
表现
解决
sudo nano /etc/apache2/apache2.conf
添加或修改:
HostnameLookups Off
然后:
sudo systemctl restart apache2
Ubuntu 启动时 Apache 会解析本机 hostname。
检查
hostname
cat /etc/hosts
确保有类似:
127.0.0.1 localhost
127.0.1.1 your-hostname
如果没有,添加即可。
不必要的模块会拖慢启动。
查看已启用模块
ls /etc/apache2/mods-enabled/
关闭无用模块(示例)
sudo a2dismod status
sudo a2dismod autoindex
sudo systemctl restart apache2
如果是 PHP 网站:
测试
time curl http://localhost
sudo nano /etc/apache2/apache2.conf
示例:
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
top
iostat
若 CPU 低但负载高,可能是:
systemctl status apache2
若卡在:
Waiting for network
修改:
sudo systemctl disable apache2
sudo systemctl enable apache2
或编辑:
sudo nano /etc/systemd/system/multi-user.target.wants/apache2.service
去掉 After=network.target 中不必要的依赖。
# 1. 关闭 DNS 解析
HostnameLookups Off
# 2. 检查 hosts
cat /etc/hosts
# 3. 看错误日志
sudo tail -f /var/log/apache2/error.log
# 4. 看启动耗时
systemd-analyze blame | grep apache
如果你愿意,可以告诉我:
我可以给你更精准的方案。