优化 Apache2(httpd)性能通常从 配置调优、模块管理、系统资源、缓存与反向代理、监控 几个方面入手。下面按优先级和可操作性给你一套实用方案。
Apache 性能核心在于 MPM(多路处理模块)。
apachectl -V | grep MPM
常见 MPM:
使用 event
<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 10000
</IfModule>
mod_phpapachectl -M
a2dismod status
a2dismod autoindex
a2dismod negotiation
a2dismod userdir
只保留必要模块(如 rewrite、ssl、headers)
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
⚠️ 太长会占用连接,太短会增加握手
<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>
适合静态资源
CacheEnable disk /
CacheRoot /var/cache/apache2
如果后端是:
推荐:
ProxyPass /api/ http://127.0.0.1:8080/
ProxyPassReverse /api/ http://127.0.0.1:8080/
Apache 只做 入口 & 静态文件
ulimit -n 65535
net.core.somaxconn=65535
net.ipv4.tcp_tw_reuse=1
CustomLog /var/log/apache2/access.log common
或关闭访问日志(仅生产调试用)
abab -n 10000 -c 200 http://localhost/
htopmod_status(谨慎开放)如果:
✅ 建议考虑:
Apache 更适合:
✅ 用 event MPM
✅ 关无用模块
✅ 开压缩 & 缓存
✅ PHP 用 FPM
✅ 静态资源走 CDN
✅ 做压测验证
如果你愿意,可以告诉我:
我可以给你 针对性配置文件。