在CentOS上优化Apache启动速度可从配置、模块、资源等方面入手,具体如下:
调整MPM(多处理模块)
event或worker),替换默认的prefork,减少进程开销。/etc/httpd/conf.modules.d/00-mpm.conf),启用event MPM:<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 1000
</IfModule>
优化启动参数
/etc/sysconfig/httpd),调整START_SERVERS、MIN_SPARE_SERVERS、MAX_SPARE_SERVERS参数,减少启动时的进程数。START_SERVERS=2
MIN_SPARE_SERVERS=2
MAX_SPARE_SERVERS=5
禁用不必要的模块
httpd -M命令查看已加载模块,禁用非必需模块(如mod_php、mod_perl等),减少内存占用。/etc/httpd/conf.modules.d/*.conf),注释掉不需要的模块。启用缓存与压缩
mod_cache和mod_expires缓存静态内容,减少重复请求。mod_deflate压缩响应数据,降低传输时间。优化KeepAlive设置
KeepAlive并合理设置KeepAliveTimeout(建议5-10秒),减少连接建立开销。KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100
调整日志级别
LogLevel设置为warn或error,减少日志记录频率,降低磁盘I/O压力。硬件与内核优化
vm.swappiness、net.ipv4.tcp_tw_reuse),优化内存和网络性能。定期维护
top、htop),及时发现性能瓶颈。注意:修改配置前需备份文件,并在测试环境验证效果,避免影响生产环境。