Linux 当我启动 apache 并继续杀死我的机器时,如何防止大量 apache 进程产生?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/501205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:56:57  来源:igfitidea点击:

How can I prevent tons of apache processes spawning when I start apache and proceeding to kill my machine?

linuxapacheapache2debianapache-config

提问by Adam Gries

I have a highly trafficked application on one debian machine and apache has started acting strange.

我在一台 debian 机器上有一个流量很大的应用程序,而 apache 开始表现得很奇怪。

Every time I start apache, tons of apache processes are spawned, the app doesn't load at all, and very quickly the whole machine freezes and must be powercycled to reboot.

每次我启动 apache 时,都会产生大量的 apache 进程,应用程序根本没有加载,很快整个机器就死机了,必须重新启动才能重新启动。

Here is what I get for top immediately after starting apache:

这是我在启动 apache 后立即获得的内容:

top -   20:14:44    up         1:16,      2 users,    load average: 0.48, 0.10, 0.03
Tasks:  330 total,  5 running, 325 sleeping,   0 stopped,   0 zombie
Cpu(s): 12.0%us,    21.4%sy,   0.0%ni,        65.7%id,   0.2%wa,  0.1%hi,  0.7%si,  0.0%st
Mem:    8179920k    total,     404984k used,  7774936k free,    60716k buffers
Swap:   2097136k    total,     0k used,       2097136k free,    43424k cached


10251 www-data  15   0  467m 8100 4016 S    6  0.1   0:00.04 apache2
10262 www-data  15   0  467m 8092 4012 S    6  0.1   0:00.05 apache2
10360 www-data  15   0  468m 8296 4016 S    6  0.1   0:00.05 apache2
10428 www-data  15   0  468m 8272 3992 S    6  0.1   0:00.05 apache2
10241 www-data  15   0  467m 8256 4012 S    4  0.1   0:00.03 apache2
10259 www-data  15   0  467m 8092 4012 S    4  0.1   0:00.04 apache2
10274 www-data  15   0  467m 8056 4012 S    4  0.1   0:00.03 apache2
10291 www-data  15   0  468m 8292 4012 S    4  0.1   0:00.03 apache2
10293 www-data  15   0  468m 8292 4012 S    4  0.1   0:00.03 apache2
10308 www-data  15   0  468m 8296 4016 S    4  0.1   0:00.02 apache2
10317 www-data  15   0  468m 8292 4012 S    4  0.1   0:00.02 apache2
10320 www-data  15   0  468m 8292 4012 S    4  0.1   0:00.04 apache2
10325 www-data  15   0  468m 8292 4012 S    4  0.1   0:00.04 apache2

And so forth.. with more apache2 processes.

等等......更多的apache2进程。

Less than a minute later, you can see below that the load has gone from 0.48 to 2.17. If I do not stop apache at this point, the load continues to rise over a few minutes or less until the machine dies.

不到一分钟后,您可以在下方看到负载从 0.48 变为 2.17。如果此时我不停止 apache,负载会在几分钟或更短的时间内继续上升,直到机器死机。

top -    20:15:34 up 1:17,       2 users,  load average: 2.17, 0.62, 0.21
Tasks:   1850 total,  5 running, 1845 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.3%us,      2.1%sy,    0.0%ni, 96.4%id,  0.0%wa,  0.1%hi,  1.0%si,  0.0%st
Mem:     8179920k     total,     1938524k used,  6241396k free,    60860k buffers
Swap:    2097136k     total,     0k used,  2097136k free,    44196k cached

We have a firewall where we whitelist the addresses we know are allowed to hit our site.

我们有一个防火墙,我们可以将我们知道可以访问我们网站的地址列入白名单。

Any ideas about what the problem might be are very welcome.

任何关于问题可能是什么的想法都非常受欢迎。

Thanks!

谢谢!

回答by kdgregory

Have you changed your configuration file recently? If yes, I trust you keep the old version for diffing?

您最近是否更改了配置文件?如果是,我相信您会保留旧版本以供区分?

If not, search for the "StartServers", "MaxSpareServers" and "MinSpareServers" directives. Generally you want to leave these at defaults, but it's possible that they were intentionally set high (bad idea) or accidentally set that way due to a bad config edit.

如果没有,请搜索“StartServers”、“MaxSpareServers”和“MinSpareServers”指令。通常,您希望将这些保留为默认值,但它们可能是故意设置为高(坏主意)或由于错误的配置编辑而意外设置的。

If this doesn't help, it's time to look outside Apache, for some process that's opening connections at a fast rate (could be that there's a testing process that's run amok).

如果这没有帮助,那么是时候看看 Apache 之外的一些进程,它以快速打开连接的速度(可能是有一个测试进程运行异常)。

First step is the access log. Second step is to run netstat, to see where the connections might be coming from. And if it's running on the same system, you can look in /proc/*/fd to find the two ends of the connection.

第一步是访问日志。第二步是运行 netstat,以查看连接可能来自哪里。如果它在同一系统上运行,则可以在 /proc/*/fd 中查找连接的两端。

回答by MarkR

You have probably made the error of configuring Apache to use far more than all of your ram. This is an easy mistake to make.

您可能犯了将 Apache 配置为使用的内存远远超过所有内存的错误。这是一个容易犯的错误。

I am assuming you are using a Prefork Apache, and an in-process application server (such as PHP or mod_perl). In this model, you will end up with a maximum of (MaxClients * max memory usage of your application per process) memory used. If you don't have nearly that much, it's time to decrease one, the other or both.

我假设您使用的是 Prefork Apache 和进程内应用程序服务器(例如 PHP 或 mod_perl)。在此模型中,您最终将使用最多(MaxClients * 每个进程的应用程序的最大内存使用量)内存。如果你没有那么多,是时候减少一个,另一个或两个。

In the general case, this means decreasing MaxClients to the point where your server has enough ram to cope.

在一般情况下,这意味着将 MaxClients 减少到您的服务器有足够的内存来应对的程度。

The default values typically used for MaxClients (150 is typical) are not suitable for running an in-process heavyweight application server on a modest machine if you are using the Prefork model (Most application servers either don't support, or discourage, the use of threaded models).

如果您使用 Prefork 模型(大多数应用程序服务器要么不支持,要么不鼓励,使用螺纹模型)。

However, decreasing MaxClients will eventually cause the application to become unavailable, particularly if you have keepalives on and the keepalive timeout too long. Processes which are just keeping a connection alive (state K in server-status) still use a lot of RAM, and that may be a problem - try to minimise keepalive timeout, or turn it off altogether.

但是,减少 MaxClients 最终会导致应用程序变得不可用,特别是如果您启用了 keepalive 并且 keepalive 超时时间过长。只是保持连接处于活动状态(服务器状态中的状态 K)的进程仍然使用大量 RAM,这可能是一个问题 - 尝试最小化保持连接超时,或完全关闭它。

You need to keep an eye on server-status (as provided by mod_status).

您需要密切关注服务器状态(由 mod_status 提供)。

Of course you should only make ANY of these changes if you understand the consequences. Think twice, change the config once. If you have ANY ability to test the changes with simulated load on a similar spec non-production machine, do so.

当然,如果您了解后果,您应该只进行任何这些更改。三思而后行,更改一次配置。如果您有任何能力在类似规格的非生产机器上使用模拟负载测试更改,请这样做。

回答by Cory R. King

As has been said (assuming Prefork Apache) - MaxClients = max processes at once.

如前所述(假设 Prefork Apache) - MaxClients = max processes 一次。

If you find you are getting hammered with real traffic (and not a mis-configured StartServers/Min/MaxSpareServers), there are some other things you can do:

如果您发现自己受到实际流量的打击(而不是错误配置的 StartServers/Min/MaxSpareServers),您还可以执行其他一些操作:

  1. Set up a separate, lightweight apache process (or lighttpd) for your static content. That way all the small, static stuff doesn't "pollute" your heavy-weight app process. This can be on the same server, or a different one. Doesn't matter.
  2. Put a reverse proxy like Squid in front of your Apache process. The reverse proxy will quickly suck down the content from Apache and store it in memory and then parcel it back out to the client. This way AOL users on 14.4kb modems don't hog one of your valuable Apache slots. As a bonus, such a setup can be configured to cache some of your content to reduce the load on your Apache processes.
  1. 为您的静态内容设置一个单独的、轻量级的 apache 进程(或 lighttpd)。这样所有小的、静态的东西都不会“污染”你的重量级应用程序进程。这可以在同一台服务器上,也可以在不同的服务器上。没关系。
  2. 在您的 Apache 进程前面放置一个像 Squid 这样的反向代理。反向代理将快速从 Apache 中提取内容并将其存储在内存中,然后将其打包回客户端。这样,使用 14.4kb 调制解调器的 AOL 用户就不会占用您宝贵的 Apache 插槽之一。作为奖励,这样的设置可以配置为缓存您的一些内容,以减少 Apache 进程的负载。

回答by Brandon

Your 'top' output shows that you have plenty of free memory, so I don't think that MaxClients is an issue (unless there is some problem with Apache allocating more than 2GB of memory?) Your error log should show errors if it is having problems creating more children.

您的“顶部”输出显示您有足够的可用内存,因此我认为 MaxClients 不是问题(除非 Apache 分配超过 2GB 的内存存在问题?)如果是,您的错误日志应显示错误在生更多孩子方面遇到问题。

Most likely, your Apache processes really are using a lot of resources. If you are running PHP apps, try installing eAccelerator which does a good job of optimizing and caching PHP code. Other things might include heavy MySQL queries, a slow DNS resolver, etc. Beyond that, it gets more into understanding what programs are being hit and what they are doing.

最有可能的是,您的 Apache 进程确实使用了大量资源。如果您正在运行 PHP 应用程序,请尝试安装 eAccelerator,它可以很好地优化和缓存 PHP 代码。其他事情可能包括繁重的 MySQL 查询、缓慢的 DNS 解析器等。除此之外,它更深入地了解哪些程序受到攻击以及它们在做什么。

回答by David Okwii

use ps -aux | grep apache to find out the number of processes that apache is running on. Look out for the "RSS" column which gives an estimate of the memory used by each process. Alternatively you can use "top", where you shift + f and then select the %MEM column to sort the processes by memory usage.

使用 ps -aux | grep apache 找出运行 apache 的进程数。注意“RSS”列,它给出了每个进程使用的内存的估计。或者,您可以使用“top”,在其中 shift + f,然后选择 %MEM 列以按内存使用情况对进程进行排序。

The number of processes is determined by "MaxClients" directive in your apache.conf file. The way you come to this figure is as described by this page;

进程数由 apache.conf 文件中的“MaxClients”指令决定。你得到这个图的方式如本页所述

  1. SSH into your server as root.
  2. Run top.
  3. Press shift + m.
  4. Note the highest RES memory used by httpd.
  5. Hit Q to exit top.
  6. Execute: service httpd stop (In debian, sudo service apache2 stop)
  7. Once httpd is stopped, execute: free -m
  8. Note the memory listed under "used".
  9. Find the guaranteed memory for your VPS plan. Support can tell you how much you have guaranteed if you cannot find it.
  10. Subtract the memory USED from the memory that your plan is GUARANTEED. This will give you your base FREE MEMORY POOL.
  11. Multiply the value of your FREE MEMORY POOL by 0.8 to find your average AVAILABLE APACHE POOL (this will allow you a 20% memory reserve for burst periods).
  12. Divide your AVAILABLE APACHE POOL by the highest RES memory used by httpd. This will give you the MaxClients value that should be set for your system. (Round it to the nearest integer less than this value if it has a fraction component.)
  1. 以 root 身份通过 SSH 连接到您的服务器。
  2. 跑顶。
  3. 按 shift + m。
  4. 请注意 httpd 使用的最高 RES 内存。
  5. 按 Q 退出顶部。
  6. 执行:service httpd stop(在debian中,sudo service apache2 stop
  7. 一旦httpd停止,执行:free -m
  8. 请注意“已使用”下列出的内存。
  9. 为您的 VPS 计划找到保证的内存。如果您找不到,支持可以告诉您已保证了多少。
  10. 从您的计划保证的内存中减去使用的内存。这将为您提供基本的免费内存池。
  11. 将您的 FREE MEMORY POOL 的值乘以 0.8 以找到您的平均 AVAILABLE APACHE POOL(这将使您在突发期间有 20% 的内存储备)。
  12. 将您的 AVAILABLE APACHE POOL 除以 httpd 使用的最高 RES 内存。这将为您提供应为您的系统设置的 MaxClients 值。(如果它有分数分量,则将其四舍五入为小于此值的最接近的整数。)

The right value for "MaxClients" will ensure the right memory allocation for your apache server. That's how I solved it.

“MaxClients”的正确值将确保为您的 apache 服务器分配正确的内存。我就是这样解决的。

In Debian, apache conf file is at /etc/apache2/apache2.conf

在 Debian 中,apache conf 文件位于 /etc/apache2/apache2.conf

回答by realgeek

This question is ancient, but I feel compelled to add an answer here because all of the existing answers are overlooking a key piece of information from the OP: After the load has begun to rise for a few minutes, topreports that there are still ample CPU & memory resources available. There is usually one culprit remaining, and that's I/O.

这个问题很古老,但我觉得有必要在这里添加一个答案,因为所有现有的答案都忽略了来自 OP 的一个关键信息:在负载开始上升几分钟后,top报告仍然有足够的 CPU & 内存资源可用。通常还有一个罪魁祸首,那就是 I/O。

Check if there is a full partition with df -h. If not, see if your application is thrashing the disk using vmstat 1 10or iostat 1 10(these are provided by the 'sysstat' package on Debian/Ubuntu). If you still don't see an issue there, perhaps you have device level I/O errors or network trouble for network-mounted storage. Check the system and daemon log files.

检查是否有一个完整的分区df -h。如果没有,请查看您的应用程序是否正在使用vmstat 1 10iostat 1 10(这些由 Debian/Ubuntu 上的“sysstat”包提供)来处理磁盘。如果您仍然没有在那里看到问题,那么您可能遇到了设备级 I/O 错误或网络安装存储的网络问题。检查系统和守护进程日志文件。