排查资源占用情况
top 或 htop 命令查看 Apache2 进程的 CPU/内存占用,重点关注 %MEM 和 CPU% 高的进程。ps aux | grep apache2 确认 Apache2 进程的资源使用详情。优化 Apache 配置
prefork/worker/event),高并发场景优先 event。MaxRequestWorkers(控制最大并发请求数)、ThreadsPerChild(线程数),避免资源耗尽。mod_deflate 压缩内容,mod_expires 缓存静态资源,减少重复计算和传输。MaxConnectionsPerChild 防止内存泄漏,KeepAliveTimeout 设为 5-10 秒。a2dismod 禁用未使用的模块(如 mod_rewrite 若无需重写规则)。系统级优化
/etc/sysctl.conf,增加 net.core.somaxconn(提升连接队列长度)、vm.swappiness(减少内存交换)。sudo sysctl -p 使配置生效。监控与维护
apachetop、netdata)实时查看资源使用情况。/var/log/apache2/),避免日志过大占用磁盘。sudo systemctl restart apache2。负载均衡(可选)
操作前建议:备份配置文件(如 /etc/apache2/),在测试环境验证优化效果后再应用到生产环境。