下面从系统、Apache 本身、模块、PHP(如适用)、安全、监控几个层面,给你一份比较实用的 Debian + Apache 优化清单。你可以按需要逐步做。
apt update && apt upgrade -y
编辑 /etc/sysctl.conf:
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
fs.file-max = 100000
生效:
sysctl -p
/etc/security/limits.conf:
* soft nofile 65535
* hard nofile 65535
Debian 默认可能是 mpm_prefork(兼容 PHP,但性能差)
apachectl -V | grep MPM
eventphp-fpm + event(最佳)apt install php-fpm
a2dismod php* mpm_prefork
a2enmod mpm_event proxy_fcgi setenvif
a2enconf php-fpm
systemctl restart apache2
/etc/apache2/mods-enabled/mpm_event.conf)<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 10000
</IfModule>
⚠️ MaxRequestWorkers 别盲目调太大,看内存:
MaxRequestWorkers ≈ 可用内存 / 单进程内存
查看已启用模块:
apache2ctl -M
常见可禁用:
a2dismod status autoindex cgi negotiation userdir
a2enmod expires headers deflate cache cache_disk
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
</IfModule>
a2enmod http2
在虚拟主机中:
Protocols h2 http/1.1
/etc/php/*/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
disable_functions = exec,passthru,shell_exec,system
a2enmod security2 # 可选(ModSecurity)
隐藏版本信息:
ServerTokens Prod
ServerSignature Off
logrotate 防止日志爆炸htopapachetopnetdataapt install apache2-utils
ab -n 1000 -c 100 http://your-site/
如果你愿意,可以告诉我:
我可以直接帮你给一套针对性配置。