优化 Debian 上 Apache 的性能,通常可以从系统层面、Apache 配置、模块优化、缓存与压缩、PHP/后端、监控几个方面入手。下面按实用优先级给出建议。
Debian 默认可能是 mpm_prefork、worker 或 event,强烈推荐 event(性能最好):
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo systemctl restart apache2
如果是 PHP,prefork 更容易兼容;但生产环境建议用 PHP-FPM + event
编辑:
sudo nano /etc/security/limits.conf
添加:
* soft nofile 65535
* hard nofile 65535
Apache 配置中:
<IfModule mpm_event_module>
ServerLimit 256
MaxRequestWorkers 400
ThreadsPerChild 25
</IfModule>
查看已启用模块:
apache2ctl -M
常见可禁用:
sudo a2dismod status
sudo a2dismod autoindex
sudo a2dismod cgi
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
太高会占用连接,太低会增加 TCP 开销
sudo a2enmod deflate
示例:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
sudo a2enmod expires
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
</IfModule>
sudo apt install php-fpm
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php-fpm
优点:
mod_cache 或反向代理(Nginx)sudo a2enmod http2
VirtualHost 中:
Protocols h2 http/1.1
需要 HTTPS
减少日志量:
CustomLog ${APACHE_LOG_DIR}/access.log combined env=!dontlog
SetEnvIf Request_URI "\.(jpg|png|css|js)$" dontlog
apache2ctl -S
apache2ctl -M
ab -n 1000 -c 100 http://localhost/
✅ Debian + Apache(event) + PHP-FPM
✅ HTTPS + HTTP/2
✅ mod_deflate + mod_expires
✅ 静态资源走 CDN
如果你愿意,可以告诉我:
我可以给你一套可直接用的配置文件。