下面从 系统层、Apache 本身、PHP(如有)、缓存与架构 几个角度,系统地说一下 Ubuntu + Apache 的优化思路,适合生产环境参考。
ulimit -n
建议提高到:
* soft nofile 65535
* hard nofile 65535
写入 /etc/security/limits.conf
systemctl disable bluetooth
systemctl disable cups
apache2ctl -V | grep MPM
| MPM | 场景 |
|---|---|
| prefork | 老 PHP(无线程安全) |
| worker | 中等并发 |
| event | ✅ 推荐(高并发) |
a2dismod php*
a2enmod mpm_event
配置示例(/etc/apache2/mods-available/mpm_event.conf):
<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 10000
</IfModule>
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
✅ 减少 TCP 握手
❌ 不要设太大(防止连接占用)
apache2ctl -M
a2dismod status
a2dismod autoindex
a2dismod cgi
只保留必要模块(如 rewrite、ssl、headers)
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 1 month"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
</IfModule>
apt install php-fpm
a2enmod proxy_fcgi
a2enconf php-fpm
memory_limit = 256M
max_execution_time = 30
opcache.enable=1
opcache.memory_consumption=128
Apache 处理动态,Nginx 处理静态
a2enmod http2
需 HTTPS
CustomLog /var/log/apache2/access.log combined env=!dontlog
htopapachetopnetdataab -n 10000 -c 200 http://your-site/
✅ Ubuntu 22.04+
✅ Apache + mpm_event
✅ PHP-FPM
✅ Gzip + Expires
✅ HTTPS + HTTP/2
如果你愿意,可以告诉我:
我可以给你 针对性优化配置。