优化 Debian 上的 Apache(通常是 Apache2)可以从系统层面、Apache 配置、模块管理、安全与缓存几个方面入手。下面按实用程度整理一份优化清单,适合生产或高并发环境。
apt update && apt upgrade -y
# /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
# /etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
sysctl -p
查看当前 MPM:
apachectl -V | grep MPM
推荐:
eventprefork启用 event:
a2dismod mpm_prefork
a2enmod mpm_event
/etc/apache2/mods-enabled/mpm_event.conf
<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 5000
</IfModule>
查看已启用模块:
apachectl -M
常见可关闭:
a2dismod status
a2dismod autoindex
a2dismod cgi
a2enmod deflate
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
a2enmod expires
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 30 days"
ExpiresByType text/css "access plus 7 days"
</IfModule>
apt install php-fpm
a2enmod proxy_fcgi setenvif
a2enconf php-fpm
关闭:
a2dismod php8.x
Options -Indexes
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
LogLevel warn
a2enmod http2 ssl
虚拟主机:
Protocols h2 http/1.1
apachectl -t(配置检查)htopab 压力测试:ab -n 1000 -c 100 http://localhost/
如果你能提供:
我可以给你一套针对性配置方案。