优化Ubuntu上Apache2内存使用可从以下方面入手:
/etc/apache2/apache2.conf
:
MaxClients
限制子进程数,避免内存过度占用。KeepAlive
并合理设置MaxKeepAliveRequests
和KeepAliveTimeout
,平衡连接复用与内存释放。MaxRequestsPerChild
使子进程定期销毁释放内存。StartServers
、MinSpareServers
、MaxSpareServers
控制空闲进程数。mod_ssl
若无需HTTPS),使用a2dismod
命令操作。mod_cache
和mod_disk_cache
缓存静态资源,减少重复请求。mod_expires
设置静态资源过期时间,降低缓存压力。vm.swappiness
降低交换分区使用倾向,vm.overcommit_memory
设为2优化内存分配。top
、htop
监控内存使用,定位异常进程。systemctl restart apache2
操作。echo 3 > /proc/sys/vm/drop_caches
)。操作前建议在测试环境验证,避免影响服务稳定性。