下面从系统、Apache 配置、模块、安全、缓存、监控几个层面,系统性地讲一下 Debian + Apache2 的优化方法。适用于生产服务器(非本地开发玩具)。
apt update && apt upgrade -y
保持内核、OpenSSL、Apache 为稳定新版本。
编辑:
/etc/security/limits.conf
添加:
* soft nofile 65535
* hard nofile 65535
并确认:
ulimit -n
systemctl disable bluetooth
systemctl disable cups
减少资源占用。
配置文件位置(Debian):
/etc/apache2/apache2.conf/etc/apache2/sites-enabled//etc/apache2/mods-enabled/查看当前 MPM:
apachectl -V | grep MPM
eventpreforkevent切换 MPM(示例 event):
a2dismod mpm_prefork
a2enmod mpm_event
systemctl restart apache2
编辑:
/etc/apache2/mods-enabled/mpm_event.conf
示例(2 核 4G):
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 200
MaxConnectionsPerChild 1000
</IfModule>
公式参考:
MaxRequestWorkers = ThreadsPerChild × ServerLimit
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
Timeout 30
说明:
KeepAlive On:减少 TCP 握手KeepAliveTimeout 不宜过大(5s 足够)Timeout 防止慢连接拖死 Apacheapache2ctl -M
a2dismod autoindex
a2dismod status
a2dismod cgi
a2dismod negotiation
⚠️ 保留:
rewritesslheadersdeflateproxy(如使用)a2enmod deflate
示例配置:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE text/css application/javascript
AddOutputFilterByType DEFLATE application/json
</IfModule>
a2enmod expires
a2enmod headers
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 30 days"
ExpiresByType text/css "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"
</IfModule>
apt install php-fpm
a2enmod proxy_fcgi setenvif
a2ensite conf-enabled/php-fpm.conf
关闭 prefork:
a2dismod php8.x
PHP-FPM 配置优化:
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
a2enmod ssl
a2enmod http2
Protocols h2 http/1.1
SSLEngine on
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite HIGH:!aNULL:!MD5
CustomLog ${APACHE_LOG_DIR}/access.log combined
# 高并发时可改为:
# CustomLog off
apt install logrotate
确保:
/etc/logrotate.d/apache2
apachectl status
systemctl status apache2
apt install apache2-utils
ab -n 1000 -c 100 http://localhost/
✅ Debian + Apache2 推荐组合:
Debian 12
Apache2 (mpm_event)
PHP-FPM
HTTP/2
gzip + expires
logrotate
如果你愿意,可以告诉我:
我可以给你一份直接可用的优化配置。