Apache consuming too much CPU and memory

hum*_*ngs 5 performance memory central-processing-unit php5 apache-2.2

I am having some troubles with CPU loading an memory with Apache Web Server.

We are running a Ubuntu Server 12.04 LTS on a Virtual Machine. Our server have the following specs:

  • 8GB RAM;
  • 4 vCPUs (12ghz);

We configured the server to run a Drupal (7.23) based website. So, we installed Apache, PHP, MySQL... The versions are below:

  • Apache 2.2.22;
  • PHP 5.3.10 (The PHP are running as Apache Module.);
  • APC 3.1.7;
  • MySQL 5.5.31 (all innodb tables);

I am running some apache modules too. Take a look (apachectl -M):

  • core_module (static)
  • log_config_module (static)
  • logio_module (static)
  • mpm_prefork_module (static)
  • http_module (static)
  • so_module (static)
  • actions_module (shared)
  • alias_module (shared)
  • authz_host_module (shared)
  • deflate_module (shared)
  • dir_module (shared)
  • env_module (shared)
  • include_module (shared)
  • mime_module (shared)
  • php5_module (shared)
  • proxy_module (shared)
  • proxy_http_module (shared)
  • reqtimeout_module (shared)
  • rewrite_module (shared)
  • setenvif_module (shared)
  • ssl_module (shared)
  • status_module (shared)

On apache2.conf, we have this config:

    Timeout 90
    KeepAlive On
    MaxKeepAliveRequests 80
    KeepAliveTimeout 5
    HostnameLookups Off
    LogLevel warn

    <IfModule mpm_prefork_module>
        StartServers          10
        MinSpareServers       10
        MaxSpareServers       30
        MaxClients           120
        MaxRequestsPerChild 1000
    </IfModule>
Run Code Online (Sandbox Code Playgroud)

The Virtual Host of my site:

    <VirtualHost *:80>
        ServerName blabla.bla.bla
        ServerAdmin bla@bla.com
        DocumentRoot /l/disk0/site/public_html

        <Directory />
            AllowOverride None
        </Directory>

        <Directory /l/disk0/site/public_html>
            Options MultiViews Indexes Includes FollowSymLinks ExecCGI
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>

        LogLevel warn
        ErrorLog "/l/disk0/site/logs/apache/site/error.log"
        CustomLog "/l/disk0/site/logs/apache/sit/access.log" combined
        SSLProxyEngine on
        RewriteEngine   on
        RewriteLog     logs/rewrite_www_log
        RewriteLogLevel        0

        Include rewrites-www.conf
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

Drupal Modules:

  • ACL 7.x-1.0
  • APC - Alternative PHP Cache 7.x-1.0-beta4
  • Boost 7.x-1.0-beta2
  • Cache Expiration 7.x-2.0-beta2
  • CAPTCHA 7.x-1.0
  • Chaos tool suite (ctools) 7.x-1.3
  • Date 7.x-2.6
  • Domain Access 7.x-3.10
  • Domain Blocks 7.x-2.0
  • Domain CTools 7.x-1.3
  • Domain Locale 7.x-1.0-beta3
  • Domain Taxonomy 7.x-3.x-dev (2012-abr-29)
  • Domain Views 7.x-1.5
  • Embed Views Display 7.x-1.2
  • Entity API 7.x-1.2
  • Entity reference 7.x-1.0
  • IMCE 7.x-1.7
  • IMCE Mkdir 7.x-1.0
  • Internationalization 7.x-1.10
  • Link 7.x-1.1
  • Localization update 7.x-1.0-beta3
  • Media 7.x-1.3
  • Meta tags quick 7.x-2.7
  • Newsletter 7.x-1.0-beta9
  • Options Element 7.x-1.9
  • Page Style 7.x-1.0
  • Panels 7.x-3.3
  • Pathauto 7.x-1.2
  • pathologic 7.x-2.11
  • profile2 7.x-1.3+0-dev (2013-mai-24)
  • select_or_other 7.x-2.19
  • sheetnode 7.x-1.0-beta4+3-dev (2013-mai-25)
  • SMTP Authentication Support 7.x-1.0
  • Token 7.x-1.5
  • Transliteration 7.x-3.1
  • Variable 7.x-2.3
  • Views 7.x-3.7
  • Vocabulary Permissions Per Role 7.x-1.0
  • Webform 7.x-3.19
  • Webform Validation 7.x-1.2
  • workbench 7.x-1.2
  • workbench_access 7.x-1.2
  • workbench_media 7.x-1.1
  • workbench_profile 7.x-1.1
  • xmlsitemap 7.x-2.0-rc2

My site is simple and don't have many visitors. I am talking about 500 visitors a day maybe. Drupal can cause so much CPU loading? Or a module?

Other problem is memory usage. When a process is created, 80M is allocated for apache2. I think is too much.

My problem is that CPU (all cores) have a high load. Most of time, hitting between 90% and 100% load! The offending process is the apache2. Memory is also consumed without pity. Of a 8GB total, the consumed memory is about 6.5GB to 7.5GB. I don't know if my apache configuration is wrong or if I'm really need more hardware (I guess not). Drupal can cause high CPU load?

When the CPU load hits 100%, the site goes down and we have to restart apache. I did a workaround solution with Drupal using APC and installing Boost. had some effectiveness, but CPU load still high. Very high.

If you need more information, like Drupal modules and PHP extensions. Please let me know.

Xio*_*iov 5

另一个问题是内存使用。创建进程时,为apache2分配了80M。我觉得太多了。

那是真实内存还是虚拟内存?老实说,这不是很多。更重要的是,您应该专注于修复导致问题的事情,而不仅仅是您“认为”应该不同的事情。

如果您希望 Apache 进程占用更少的内存,您应该禁用模块(因为每个模块都是需要在内存中的更多代码)。但是,如果您需要已启用的所有模块,那么,仅此而已。

我在管理内存受限机器时使用的一种方法是将某些任务从 Apache 移到其他服务器上,这样我就可以单独调整它们。

但更简单的方法是改变

MaxClients           120
Run Code Online (Sandbox Code Playgroud)

对你的工作量更合理的东西:

如果我们考虑重写访问和主站点流量,我们每分钟大约有 70 个请求。现在,我们有 33 个传入连接。

我会暂时回过头来,但如果您只处理 33 个并发请求,则不需要 120 个工作人员!

MaxClients           40
Run Code Online (Sandbox Code Playgroud)

您可能应该分别调低MinSpareServersMaxSpareServer调至 5 和 10 之类的值。没有必要让 30 名工人坐在那里无所事事。

现在,回到

如果我们考虑重写访问和主站点流量,我们每分钟大约有 70 个请求。现在,我们有 33 个传入连接。

如果您有 33 个并发请求,但每分钟只处理 70 个,则有两种可能性:

  1. 您的请求每个需要大约 30 秒才能送达!
  2. 你的请求率不是很稳定,大部分时间你什么都不做。

如果 #1 是这种情况,我真的不知道如何提供帮助 - 有些事情非常错误,所以错误我什至不知道从哪里开始寻找。

如果它是 #2,我猜你正在从你的服务器提供你所有的静态资产(图像、js、css、字体)。最好把这些放在 CDN 上,但如果你真的做不到,你可以在它们上设置超长的缓存时间,然后重新打开 Varnish。如果您将 Apache 进程与 PHP 和一大堆其他东西一起使用来提供静态文件,那么您就是在浪费资源 - 用更简单的方法来做到这一点!

我的问题是 CPU(所有内核)的负载很高。大多数情况下,达到 90% 到 100% 的负载!有问题的进程是apache2。

这是一个常数,还是仅在您处理请求时?

磁盘 I/O 看起来如何 ( iostat -mhx 2)?MySQL 在做什么 ( show processlist;)?


对于您所描述的内容,您的服务器已被大大压倒了。这是个好消息,因为这意味着您应该能够解决这个问题。

  • 真正的问题出在 Drupal(和一个模块)中。我在这里回答了我自己的问题:http://drupal.stackexchange.com/questions/87687/what-trigger-menu-rebuild-flag-in-semaphore-table (2认同)