优化CentOS上Apache内存使用可从配置、系统、应用层面入手,具体方法如下:
httpd.conf,设置StartServers、MinSpareServers、MaxSpareServers、MaxRequestWorkers(或MaxClients),根据服务器内存调整,避免进程过多占用内存。KeepAlive并设置较小的KeepAliveTimeout(如5秒),减少空闲连接占用内存;设置MaxKeepAliveRequests限制单个连接请求量。MaxRequestsPerChild(如100-5000),防止内存泄漏。/etc/sysctl.conf,降低vm.swappiness(如设为10),减少对交换空间的依赖;设置vm.overcommit_memory=1,谨慎分配内存。sudo sysctl -p使参数生效。mod_cache、mod_cache_disk缓存静态内容(如图片、CSS),减少重复请求占用内存。mod_deflate压缩传输数据,降低带宽占用。httpd -M查看已加载模块,禁用不需要的模块(如mod_status、mod_cgi等),减少内存占用。top、ps、vmstat等工具监控内存使用,根据结果调整参数。systemctl restart httpd操作。注意:修改配置前需备份文件,建议在测试环境验证后再应用到生产环境。