以下是Ubuntu上优化Apache性能的关键措施:
选择合适的MPM模块
event或worker(多线程/事件驱动,低资源占用),非线程安全应用选prefork。sudo a2dismod mpm_prefork(若使用event/worker)。调整核心配置参数
KeepAlive On,MaxKeepAliveRequests 100,KeepAliveTimeout 2-5秒。MaxRequestWorkers(建议不超过内存支持的进程数)。启用缓存与压缩
mod_cache缓存静态内容,mod_deflate压缩文本类响应(如HTML/CSS/JS)。sudo a2enmod deflate cache sudo nano /etc/apache2/mods-available/deflate.conf # 添加压缩类型:AddOutputFilterByType DEFLATE text/html text/css application/javascript mod_expires设置过期头,减少重复请求。优化系统与内核参数
/etc/sysctl.conf:增加net.core.somaxconn(提升网络连接队列长度),降低vm.swappiness(减少内存交换)。禁用不必要的模块
mod_status若无需监控),减少资源占用:sudo a2dismod 模块名。硬件与监控
top/htop监控资源使用,ab(ApacheBench)测试性能瓶颈。其他高级优化
mod_pagespeed(需手动安装)自动优化页面加载。注意:每次修改配置后需重启Apache:sudo systemctl restart apache2,建议先在测试环境验证。
参考来源: