Apache httpd 进程占用过多内存

Apache httpd 进程占用过多内存

我在 CentOS6 上运行一个专用的 Apache Web 服务器,它有:12GB 内存 4 个 CPU

我的 httpd 配置如下,来自 /etc/httpd/conf/httpd.conf

Timeout 60
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 15

服务器正在使用具有以下设置的 Prefork MPM。

<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
ServerLimit      50
MaxClients       50
MaxRequestsPerChild  300
</IfModule>

这是根据之前的设置进行调整的:

<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
ServerLimit      256
MaxClients       256
MaxRequestsPerChild  4000
</IfModule>

问题

更改这些值几次并重新启动 httpd 后,总是会发生同样的事情。其中一个 PID 占用了 11GB 内存,因此我手动将其终止,内存被释放。

这是我在杀死恶意 PID 并重新启动之后的 free -m 输出:

             total       used       free     shared    buffers     cached
Mem:         11891       1132      10759          0         34        417
-/+ buffers/cache:        679      11212
Swap:         3827        227       3600

看起来我的 httpd PID 占用了太多内存。

top - 11:26:58 up  1:20,  1 user,  load average: 2.87, 2.43, 4.25
Tasks: 174 total,  12 running, 162 sleeping,   0 stopped,   0 zombie
Cpu(s): 83.3%us,  4.4%sy,  0.0%ni, 10.4%id,  1.7%wa,  0.0%hi,  0.2%si,  0.0%st
Mem:  12177272k total,  4746532k used,  7430740k free,    50012k buffers
Swap:  3919840k total,   227860k used,  3691980k free,   776180k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3848 apache    20   0  653m 216m 8908 R 31.3  1.8   0:11.77 httpd
 3862 apache    20   0  627m 194m 6824 R 30.3  1.6   0:09.62 httpd
 3846 apache    20   0  562m 126m 8728 R 28.6  1.1   0:10.20 httpd
 3844 apache    20   0  638m 204m 6704 R 27.9  1.7   0:08.40 httpd
 3911 apache    20   0  638m 203m 5880 R 27.6  1.7   0:04.92 httpd
 3880 apache    20   0  639m 205m 6724 R 27.3  1.7   0:06.76 httpd
 3918 apache    20   0  680m 246m 5820 S 25.3  2.1   0:05.38 httpd
 3921 apache    20   0  630m 197m 4440 R 24.0  1.7   0:01.63 httpd
 3843 apache    20   0  624m 191m 6684 R 23.3  1.6   0:07.50 httpd
 2317 mysql     20   0 2463m 116m 3584 S 23.0  1.0  20:07.12 mysqld
 3907 apache    20   0  620m 187m 5856 R 23.0  1.6   0:04.35 httpd
 3927 apache    20   0  623m 188m 3624 R 22.6  1.6   0:00.68 httpd
 3906 apache    20   0  628m 195m 6080 R 20.6  1.6   0:04.27 httpd
 3908 apache    20   0  556m 123m 5880 S  8.0  1.0   0:02.79 httpd
 3917 apache    20   0  536m 104m 6064 S  2.7  0.9   0:00.39 httpd
 3909 apache    20   0  554m 121m 6120 S  2.3  1.0   0:01.19 httpd
 3915 apache    20   0  614m 182m 6372 S  2.3  1.5   0:02.51 httpd
 3849 apache    20   0  576m 144m 6700 S  1.7  1.2   0:06.54 httpd
 3838 root      20   0  508m  84m  12m S  0.3  0.7   0:00.27 httpd
 3931 apache    20   0  509m  76m 4216 S  0.3  0.6   0:00.01 httpd

然后其中一个总会突然发疯并耗尽所有内存。

 4076 apache    20   0 7834m 7.2g 6172 S 11.6 62.2   0:34.86 httpd

MYSQL 内存

[root@xxx ~]# ps aux | grep 'mysql' | awk '{print $6}'
4
148316
884

APACHE 进程大小

[root@xxx ~]#  ps aux | grep 'httpd' | awk '{print $6}'
93640
73196
133840
204352
170620
202056
120312
123600
123492
119048
131316
119744
200304
203160
118468
189300
203196
200024
124184
880

NETSTAT 达到峰值(当恶意 PID 吃掉所有内存时)

[root@xxx ~]# netstat -plan | grep :80
tcp        0      0 :::80                       :::*                        LISTEN      4235/httpd
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:208.167.230.35:47089 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:208.167.230.35:59089 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:208.167.230.35:35831 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:208.167.230.35:42075 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:208.167.230.35:49612 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:208.167.230.35:43970 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:173.199.114.19:44220 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:208.167.230.35:34405 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:208.167.230.35:33963 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:66.249.75.222:42306  ESTABLISHED 4241/httpd
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:173.199.115.67:53145 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:91.232.96.34:64675   TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:107.20.53.252:40552  ESTABLISHED 4247/httpd
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:173.199.114.19:46658 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:208.167.230.35:46954 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:74.208.104.107:33988 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:208.167.230.35:51501 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:208.167.230.35:52628 TIME_WAIT   -
tcp        0      0 ::ffff:74.208.104.107:80    ::ffff:74.208.104.107:33989 TIME_WAIT   -

该服务器有几个不同的用途,它:运行大量 Wordpress 网站。用于不同自动化任务的 CRON 作业(我终止了其中大部分,但问题仍然存在)让移动应用程序通过 POST 连接到它。

问题:

1) What ELSE can I do to troubleshoot this problem?
2) How do I find WHICH PHP script might be running to take up all the memory?  Perhaps this might be some runaway script that I haven't isolated.
3) Am I editing the right file for my httpd config, /etc/httpd/conf/httpd.conf?
4) What are the optimal settings for my server?
5) Could there be some kind of attack or rootkit thing happening?

编辑

我使用 /etc/security/limits.conf 对 maxlock 和 as(虚拟内存)设置了 2 个永久 ulimit 限制,上限为 1048576KB(1GB)。这似乎已经阻止了这种行为,但我相信这只是权宜之计。我仍然需要一些答案。

里克

答案1

注意到这个问题自 2013 年以来一直没有答案,希望现在能得到解决

  1. 您的 Web 服务器可能正在遭受攻击,例如 XML-RPC 攻击,大量流量不停地通过您的服务器。检查access.log以找出文件访问模式。

  2. 要了解哪个 PHP 消耗了最多的资源,请按照此回答

  3. 是的。

  4. 取决于你的 PHP 如何工作,关注并发用户还是计算能力?

  5. 与 #1 相同。检查访问日志并根据需要启用应用程序防火墙。

希望能帮助到你。

相关内容