在 Ubuntu 上,Apache 变慢通常不是单一原因,而是多个因素叠加。下面按最常见 → 较少见的顺序帮你梳理,并给出对应的排查/解决思路。
Apache 在 Ubuntu 默认使用 MPM event / prefork,配置不对会非常慢。
查看当前 MPM:
apache2ctl -V | grep -i mpm
MaxClients 太小ThreadsPerChild 太低<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
修改后:
sudo systemctl restart apache2
如果你跑的是 PHP 网站:
❌ 错误方式:
mod_php + prefork(内存大、慢)✅ 推荐方式:
php-fpm + mpm_event安装:
sudo apt install php-fpm
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm
sudo a2dismod php8.1
sudo systemctl restart apache2
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100
❌ 关闭 KeepAlive 会显著变慢(尤其是前端资源多)
Apache 默认会尝试解析客户端 IP。
关闭:
HostnameLookups Off
free -h
top
如果 Swap 频繁使用,Apache 会“假死”。
解决:
MaxRequestWorkersiostat -x 1
如果是 HDD + 大量日志 / 上传,会非常慢。
tail -f /var/log/apache2/access.log
关闭不必要日志:
CustomLog /dev/null common
mod_securityfail2ban临时关闭测试:
sudo a2dismod security2
sudo systemctl restart apache2
sudo apt install libapache2-mod-evasive
或限制:
RequestReadTimeout header=10-20,minrate=500
# 1. 看 Apache 状态
systemctl status apache2
# 2. 看连接数
netstat -ant | grep :80 | wc -l
# 3. 看错误日志
tail -n 50 /var/log/apache2/error.log
# 4. 看负载
uptime
你可以直接贴出:
apache2ctl -M 输出我可以直接告诉你改哪一行配置最有效。