Apache eating up 2GB of RAM

9,086

Solution 1

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.

Solution 2

Having mysql and apache on one server is a REAL bad idea. The more users you have, the more access you have to the data base, which also uses cpu and mem to work of course. So you tell the apache to query and query, the users get impatient, reload the pages maybe, even more queries. You know where I'm getting right? In your case it is not only apaxhe but mysql which needs to use the same sparse ressources.

I tell you what, don't use an apache (cannon) to kill a bird (80 users). Go nginx(-npm), more light-weight, doesn't eat up that much ressources.

Most ppl don't understand that apache CLAIMs the ressources for POSSIBLE connect (hence the tip to lower that)

This program is written to serve pages at any time. So it will take the ressources (even if there are no connections, to show for it) an keep them alive. Sorry I'm not a native english speaker so I hope I could explain it in a way you understand. Short version 80 user and apache do not match on a small vhost! Use nginx, this should fix your problem, else move the db to another small server.

Share:
9,086

Related videos on Youtube

Sumit Gupta
Author by

Sumit Gupta

A Software developer!

Updated on September 18, 2022

Comments

  • Sumit Gupta
    Sumit Gupta about 1 year

    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.

    • cremefraiche
      cremefraiche almost 9 years
      Setting swap to 2x installed RAM is often suggested for optimal performance.
    • Sumit Gupta
      Sumit Gupta almost 9 years
      Advice accepted. Thanks. Can you put some light on why apache is taking that much memory or if I can reduce it somehow?
    • gertvdijk
      gertvdijk almost 9 years
      What is your Apache MPM configuration? And which are you using? prefork/worker/event, etc. Prefork with extensions like PHP can consume multiple GBs of memory in default configuration. Change that. It won't use memory directly, it will do later when more clients use more connections (prefork MPM will fork more Apache processes if necessary and configured to). If you don't post your configuration and application details we cannot help you.
    • Sumit Gupta
      Sumit Gupta almost 9 years
      @gertvdijk Well, I am okay to share the configuration as soon as I understand which configuration section I need to share. frankly, I didn't understand what you mean by prefork/worker/ or what is MPM configuration? I just apt-get apache and install it as is.
    • Sumit Gupta
      Sumit Gupta almost 9 years
      @gertvdijk I put the modification in question. what is PHP FPM?
    • gertvdijk
      gertvdijk almost 9 years
      @SumitGupta Your server is currently configured with a ServerLimit of 256 (times ~30 MB per fork), so that's WAY past your server dimensions. Please read up on basic Apache tuning first. I could provide you a very simple straightforward answer but that's not geared towards your situation perhaps.
    • Sumit Gupta
      Sumit Gupta almost 9 years
      @gertvdijk agree, any recommended reading? other then apache documentation?
  • Sumit Gupta
    Sumit Gupta almost 9 years
    Do you have any idea how many simultaneous user your suggest configuration handle. I believe it is around 50-80 users ? I mean the configuration you suggest is good for hardware limit, but how I do it for user experience?
  • gertvdijk
    gertvdijk almost 9 years
    Nope. MaxClients 20 tells you pretty much the limit here. For more simulateous connections you'll need more memory in prefork configuration. Change to FPM to handle more with less memory use. But really, one could write a book about Apache tuning. Read up on documentation and literature rather and adjust it to your needs. Then come back with specific questions rather than hoping for a general one-size-fits-all answer. ;)
  • Sumit Gupta
    Sumit Gupta almost 9 years
    been in development I know one-size-fits-all never works here. I understand your point and reading the topic right now. But I understand that you pick the right fault at least for now, and now I have to wait for next crash maybe. :).
  • Sumit Gupta
    Sumit Gupta over 5 years
    That is respectful answer, and I totally understand the complication of using Apache and mysql on same machine. But at time we are bound to client's will. Their will is our command :). Thanks for suggestion though.