在 Debian 上优化 Apache2,一般可以从 系统层面、Apache 配置、模块管理、PHP/后端、缓存与压测 几个方面入手。下面是一套比较实用的优化清单(兼顾稳定与性能)。
free -h
top
df -h
确保:
编辑:
/etc/security/limits.conf
添加:
* soft nofile 65535
* hard nofile 65535
Apache 用户也要生效(www-data)。
查看已启用模块:
apache2ctl -M
常见可关闭的模块:
auth_basicstatus(生产可关)autoindexnegotiationuserdir禁用示例:
a2dismod autoindex negotiation userdir
apache2ctl -V | grep MPM
Debian 常见:
a2dismod mpm_prefork
a2enmod mpm_event
编辑配置:
/etc/apache2/mods-available/mpm_event.conf
示例(4核8G):
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 1000
</IfModule>
编辑:
/etc/apache2/conf-available/optimize.conf
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
Timeout 30
启用:
a2enconf optimize
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"
ExpiresByType application/javascript "access plus 7 days"
</IfModule>
apt install php-fpm
a2enmod proxy_fcgi
a2enconf php8.2-fpm
禁用 prefork PHP:
a2dismod php8.2
/etc/php/8.2/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
CustomLog ${APACHE_LOG_DIR}/access.log combined env=!dontlog
HostnameLookups Off
a2enmod headers
ServerTokens Prod
ServerSignature Off
apt install apache2-utils
ab -n 1000 -c 100 http://localhost/
观察:
✅ Debian + Apache2 + mpm_event
✅ PHP-FPM
✅ gzip + expires
✅ 关闭无用模块
如果你愿意,可以告诉我:
我可以给你一份 针对性的 Apache 配置模板。