Apache配置优化降低服务器压力
一 核心思路与前置检查
二 关键配置与示例
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 2
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresDefault "access plus 2 days"
</IfModule>
Timeout 30
LogLevel warn
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access_log.%Y-%m-%d 86400" combined
ErrorLog "|/usr/sbin/rotatelogs /var/log/apache2/error_log.%Y-%m-%d 86400"
apachectl -M查看已加载模块,注释不必要的LoadModule行。三 按MPM进行并发与内存调优
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event # 或 mpm_worker
sudo systemctl restart apache2
/etc/httpd/conf.modules.d/00-mpm.conf中启用对应模块并重启。<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 10000
</IfModule>
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 5000
</IfModule>
MaxRequestWorkers,缩短KeepAliveTimeout,减少进程/线程争用。MaxRequestWorkers,并检查后端(数据库、外部API)与慢查询。MaxRequestWorkers或ThreadsPerChild,并启用更激进的静态资源缓存/CDN。四 架构层优化与运维实践
ulimit -n 65535)、合理内核网络参数;精简模块、隐藏版本信息、保持组件及时更新。