Ubuntu – Apache eating up 2GB of RAM

Apache2ramserver

I have a 1GB VPS server running 12.0.4 ubuntu. Everything works just fine, most of time my RAM usage shows around 700 MB. However every week or so at random time Server goes out of memory and crashes. It recovers and start all service except Mysql.

I found that I don't have swap memory, so I create 256MB swap file and configure it. It works fine, but still crash this time after 2 weeks.

I am no expert as you already figured it out. So, I want some guidance on why in normal operation my server is taking so much Memory. I mean I observe it at different time, it is always around 700-800MB Memory and Load is stable. there is not cron that run at night or something. I have couple of wordpress blog, but they have stable load and never see any "bulk" load.

I do have Vesta Control Panel, and all reports are normal that I know off. One thing that however I notice is that Apache always shows 1900MB of Memory consumption where as my system in total have lower memory. I have nginx installed reverse web server[not sure what does that mean by vesta did it for me]. everything else is good just apache is all the time at 1900MB. and it seems that when this usage increase it crash the server.

So, is there a way I can bring down that usage of apache? What else are measure I should do to make my server stable with current load.

EDIT:

Based on suggestion here is my MPM Configuration

<IfModule mpm_prefork_module>
    StartServers          8
    MinSpareServers       5
    MaxSpareServers      20
    ServerLimit         256
    MaxClients          200
    MaxRequestsPerChild 4000
</IfModule>

and here is my apache2ctl -l

Compiled in modules:
  core.c
  mod_log_config.c
  mod_logio.c
  prefork.c
  http_core.c
  mod_so.c

Though I mention before, but I am running WordPress (PHP) based blogs on server, there is Redmine (Ruby on Rail) as well that runs using Nginx and apache in same way as other website. No, i don't use python.

Best Answer

With Apache prefork MPM configured with a ServerLimit of 256 you've configured it for a maximum memory usage of about 256 processes, roughly 15-30 MB of memory each in a typical PHP5 Apache module. Do the math and you know it will explode on a small server with a high number of clients.

Instead, configure it with much tighter limits. E.g.

<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    ServerLimit         25
    MaxClients          20
    MaxRequestsPerChild 4000
</IfModule>

Restart Apache to use the new MPM configuration.

This will make Apache start with 5 processes, ensures it has 5 processes free for new incoming clients, shut down processes only if more than 10 are unused and a hard limit of 25. Adjust to your needs.

Alternatively, look into the option of using PHP-FPM rather than PHP as an Apache module. That's a lot more efficient and also the default configuration for Ubuntu 14.04. Please note that you will have a separate PHP process pool and you will not be using the Apache prefork MPM then (and the exact reason for why it's more efficient).

Just read up on regular Ubuntu/Apache/PHP-FPM documentation how to configure all this.

Related Question