在 CentOS 上 Apache(httpd)占用内存高,通常不是“Apache 本身有 bug”,而是配置 + 使用场景共同导致的。下面按最常见原因 → 对应解决办法来说明。
CentOS 默认常用:
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
</IfModule>
问题:
✅ 解决:
MaxClientsworker / eventps aux | grep httpd
如果看到 几十上百个 httpd 进程,基本就是这个值太大。
✅ 建议:
ServerLimit 150
MaxClients 150
如果你用的是:
mod_php那 Apache 内存 ≈ PHP 内存
php -i | grep memory_limit
常见:
memory_limit = 128M
并发 50 → 50 × 128M = 6.4G
✅ 解决:
memory_limitphp-fpm(推荐)mod_php:
✅ 推荐架构:
Apache + php-fpm
httpd -M
常见吃内存模块:
✅ 关闭不用的:
yum remove mod_php
表现:
✅ 检查:
netstat -an | grep :80 | wc -l
✅ 解决:
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
问题:
✅ 建议:
KeepAliveTimeout 5
# 看 Apache 进程数
ps -ef | grep httpd | wc -l
# 看每个进程内存
ps -o pid,user,rss,comm -C httpd
# 看总内存
free -m
# 看 Apache 配置
httpd -V | grep MPM
✅ 最佳实践
你可以直接把下面信息贴出来,我可以精确告诉你怎么改:
httpd -M 输出free -m 结果我可以给你一套可直接用的配置。