在 Debian 上优化 Apache2,通常从性能、安全、模块、系统层面几方面入手。下面给你一套实用优化清单(适用于 Debian 10/11/12)。
Apache 有三种 MPM:
apache2ctl -V | grep -i mpm
a2dismod mpm_prefork
a2enmod mpm_event
systemctl restart apache2
⚠️ 如果用 mod_php,不能用 event,只能 prefork
✅ 推荐用 PHP-FPM
apt install php-fpm
a2enmod proxy_fcgi setenvif
a2enconf php-fpm
a2dismod php*
编辑:
nano /etc/apache2/mods-available/mpm_event.conf
示例(2G~4G 内存服务器):
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 3000
</IfModule>
重载:
systemctl restart apache2
a2enmod deflate
编辑:
nano /etc/apache2/conf-available/deflate.conf
加入:
AddOutputFilterByType DEFLATE text/html text/css application/javascript
a2enmod expires
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
</IfModule>
查看:
apache2ctl -M
常见可禁用:
a2dismod status autoindex cgi negotiation
nano /etc/apache2/apache2.conf
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
高并发建议 KeepAliveTimeout 2
ulimit -n 65535
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
sysctl -p
a2enmod http2 ssl
虚拟主机:
Protocols h2 http/1.1
apt install apache2-utils
ab -n 1000 -c 100 http://localhost/
✅ Debian + Apache2 (event) + PHP-FPM + HTTP/2 + deflate + expires
如果你愿意,可以告诉我:
我可以给你精准配置。