Ubuntu上优化Apache并发连接的实用步骤
一 选择并切换合适的MPM
二 调整MPM关键参数
| MPM | 关键参数与示例值 | 说明 |
|---|---|---|
| prefork | StartServers 5;MinSpareServers 5;MaxSpareServers 10;MaxRequestWorkers 150;MaxConnectionsPerChild 0 | 进程模型,值过大易占满内存;适合非线程安全环境。 |
| event/worker | StartServers 2–4;MinSpareThreads 25;MaxSpareThreads 75;ThreadsPerChild 25;MaxRequestWorkers 150–400;MaxConnectionsPerChild 1000–10000;ThreadLimit 64 | 线程模型,并发更高;event更擅长处理长连接与空闲保持。 |
三 优化连接与超时参数
四 启用压缩与缓存减轻后端压力
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
</IfModule>
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot "/var/cache/apache2/mod_cache_disk"
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
</IfModule>
五 监控验证与进一步扩展
<Location "/server-status">
SetHandler server-status
Require local
</Location>
附 快速估算与落地步骤