在Ubuntu上优化Apache内存使用可从以下方面入手:
/etc/apache2/apache2.conf
或/etc/apache2/mods-enabled/mpm_*.conf
:
MaxRequestsPerChild
(如1000),限制子进程处理请求数,防止内存泄漏。event
MPM(sudo a2dismod prefork && sudo a2enmod event
),减少线程/进程开销。StartServers
、MinSpareServers
、MaxSpareServers
,控制空闲进程数。KeepAlive
或设置MaxKeepAliveRequests
(如100),减少长连接内存占用。apachectl -M
查看已加载模块,通过sudo a2dismod <模块名>
禁用不需要的模块(如mod_rewrite
若未使用)。mod_expires
设置静态资源缓存(如图片、CSS),减少重复请求:sudo a2enmod expires
sudo nano /etc/apache2/mods-enabled/expires.conf
# 添加:ExpiresActive On
/etc/sysctl.conf
:
vm.swappiness=10
,减少交换分区使用。vm.overcommit_memory=2
,允许内存超发。sudo sysctl -p
使配置生效。top
、htop
或apachetop
监控内存使用,定位异常进程。sudo systemctl restart apache2
。sudo journalctl --vacuum-time=3d
。注意:修改配置前建议备份文件,并在测试环境验证,避免影响服务稳定性。