以下是CentOS上优化Apache性能的关键措施:
调整MPM模块配置
/etc/httpd/conf.modules.d/00-mpm.conf
,根据服务器资源调整StartServers
、MinSpareServers
、MaxSpareServers
、MaxRequestWorkers
等参数,平衡并发处理能力与资源占用。mpm_event
或mpm_worker
模块(需安装对应模块)。启用缓存与压缩
mod_expires
模块设置缓存时间,减少重复请求。<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 30 days"
ExpiresByType image/jpeg "access plus 1 year"
</IfModule>
mod_deflate
模块压缩HTML/CSS/JS等文本文件,降低传输体积。<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
优化KeepAlive设置
KeepAlive On
并设置合理参数:MaxKeepAliveRequests 100
(限制单连接请求数)、KeepAliveTimeout 5
(超时时间,单位秒),减少连接建立开销。精简模块与服务
mod_cgi
),减少内存占用。内核与文件系统优化
/etc/sysctl.conf
参数,如增大net.core.somaxconn
(TCP连接队列长度)、vm.swappiness
(减少swap使用)。noatime
挂载选项优化文件系统,减少磁盘I/O。硬件与网络优化
监控与维护
top
、htop
等工具实时监控资源使用情况,及时发现瓶颈。注意:修改配置前需备份文件,建议在测试环境验证后再应用到生产环境。
参考来源:[1,2,3,4,5,6,7,8,9,10,11]