在 Debian 上遇到 Apache 响应慢,可以从系统、Apache 配置、应用、网络几个方面排查和优化。下面给你一套实战排查思路 + 常见优化方法。
time curl -I http://localhost/
top
htop
free -h
df -h
重点看:
Debian 默认可能是 mpm_prefork
查看:
apachectl -V | grep MPM
preforkevent(性能最好)切换:
a2dismod mpm_prefork
a2enmod mpm_event
systemctl restart apache2
编辑:
/etc/apache2/mods-enabled/mpm_event.conf
示例(4 核 8G):
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
⚠️
MaxRequestWorkers太大反而更慢
apache2ctl -M
常见可关:
a2dismod status
a2dismod autoindex
a2dismod negotiation
/etc/apache2/apache2.conf
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 2
apt install php-fpm
a2enmod proxy_fcgi setenvif
a2enconf php-fpm
systemctl restart apache2
比 mod_php 快很多
apt install php-opcache
php.ini:
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
slowlog = /var/log/php-fpm.slow.log
request_slowlog_timeout = 2s
<Directory /var/www/html>
AllowOverride None
</Directory>
iostat -x 1
HostnameLookups Off
✅ 使用 mpm_event
✅ 使用 PHP-FPM
✅ 开启 OPcache
✅ 关闭 .htaccess
✅ KeepAliveTimeout 2
✅ HostnameLookups Off
✅ 看 PHP slow log
你可以补充:
cat /etc/os-release)我可以直接给你一份可复制的配置文件。