下面给你一份Debian + Apache 的优化清单,从系统、Apache 配置、模块、PHP、缓存、安全、监控几个层面来,适合生产环境。
# 查看内存、CPU
free -h
top
建议:
systemctl disable bluetooth
ulimit -n
修改:
vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
apache2ctl -M | grep mpm
| 场景 | MPM |
|---|---|
| 静态站点 | event |
| PHP-FPM | event |
| 老 PHP(mod_php) | prefork |
a2dismod mpm_prefork
a2enmod mpm_event
systemctl restart apache2
/etc/apache2/mods-available/mpm_event.conf
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 1000
</IfModule>
小内存服务器可降低
MaxRequestWorkers
/etc/apache2/apache2.conf
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
apache2ctl -M
禁用示例:
a2dismod autoindex cgi status
systemctl restart apache2
apt install php-fpm
a2enmod proxy_fcgi setenvif
a2enconf php8.x-fpm
systemctl restart apache2
/etc/php/8.x/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
a2enmod expires headers cache cache_disk
systemctl restart apache2
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 7 days"
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType application/javascript "access plus 7 days"
</IfModule>
a2enmod http2
虚拟主机中:
Protocols h2 http/1.1
⚠️ HTTP/2 需要 HTTPS
apt install certbot python3-certbot-apache
certbot --apache
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"
Header always set Content-Security-Policy "default-src 'self'"
LogLevel warn
CustomLog /dev/null combined
a2enmod status
systemctl restart apache2
访问:
http://your-ip/server-status
apt install apache2-utils
ab -n 1000 -c 50 http://your-site/
✅ MPM event
✅ PHP-FPM
✅ 启用缓存
✅ 关闭无用模块
✅ HTTPS + HTTP/2
✅ 合理 KeepAlive
✅ 监控 server-status
如果你愿意,可以告诉我:
我可以给你一套针对你环境的精确配置。