以下是Ubuntu Apache2性能优化技巧:
event(事件驱动,内存占用低);prefork(稳定性高,但内存消耗大)。event模块需设置StartServers、MinSpareThreads、MaxRequestWorkers等,匹配服务器资源。mod_deflate压缩响应数据(如文本、CSS);mod_expires设置静态资源过期时间,减少重复请求。KeepAlive On,合理设置KeepAliveTimeout(如5-10秒)和MaxKeepAliveRequests(如100-200),平衡连接复用与资源占用。MaxClients(或MaxRequestWorkers)限制最大并发数,防止服务器过载。a2dismod命令禁用未使用的模块(如mod_rewrite若无需重定向),减少资源消耗。vm.swappiness(减少交换分区依赖)、net.core.somaxconn(提升网络连接队列长度);top、htop、vmstat监控资源使用,通过ab(ApacheBench)进行压力测试,验证优化效果。注意:修改配置后需重启Apache服务(sudo systemctl restart apache2),建议先在测试环境验证。