优化Ubuntu Apache内存使用可从以下方面入手:
mod_ssl
、mod_php
),仅保留核心模块。MaxClients
:根据服务器内存和单个进程占用量设置,避免过多进程占用内存。MaxRequestsPerChild
:设置合理值(如1000-5000),防止内存泄漏。KeepAlive
:低并发场景设为Off
,高并发设为On
并调整KeepAliveTimeout
(建议5-15秒)和MaxKeepAliveRequests
(如100)。StartServers
、MinSpareServers
、MaxSpareServers
:根据负载调整,避免空闲进程过多。mod_expires
缓存静态资源,减少重复请求。mod_deflate
压缩传输数据,降低带宽占用。prefork
切换至event
或worker
模式,减少内存消耗。vm.swappiness
设为较低值(如10),减少交换空间使用。sudo sync && echo 3 > /proc/sys/vm/drop_caches
。top
、htop
监控内存使用,定位异常进程。systemctl reload apache2
重载配置。参考来源: