I've been working on improving memory usage on our Centos 5.5 VDS, which now has 512MB of RAM. I've been able to use a tuneup script to greatly improve the MySQL config but am having a bit more trouble getting Apache's memory usage down. Part of my problem is conflicting advice on a various web sites, forums, etc. Being a production server the normal modify and test cycle I would employ on a test machine would be a bit too disruptive.

To add to my confusion, I have a dev machine configured as near as I can make it to be a clone of the VDS and it uses less RAM with the exact same Apache configuration. Although it's obviously difficult to apply the same load I get pretty close when comparing the two during the quiet times of the day or night.

Is there a tuneup script for Apache, like those available for MySQL, that can help optimize the configuration by analyzing what's happening while the server is running?


Either I've been unclear or people are not reading the question, so I'll clarify.

I'm not asking anyone here to make recommendations or suggest alternatives. I'm asking if someone here knows of a script which can analyze what's happening on the server and make recommendations regarding the Apache configuration based on that information.

  • What is the current config? What is the expected workload - rate of requests / number of concurrent requests / average duration of a request? – symcbean Nov 2 '11 at 10:55

I don't know of any such script for Apache likely because, compared to MySQL, there are only a few things which affect Apache's memory usage:

  1. MaxClients: The more clients the more memory used.
  2. Mods: Similarly, the more mods/extensions installed the more memory needed.
  3. Dynamic Scripts: Running dynamic scripts, like PHP, may increase memory usage depending on your Apache settings. For example, say you have 10 small PHP files and one rarely used large/memory intensive one. Eventually all Apache clients will run this large PHP script and use more memory.
  4. Compile Time: I haven't played much with Apache compile time settings but I wouldn't be surprised if there were some memory savings to be found there.

A few specific things you can try are listed below. Try to measure the effect and test on your development platform first if possible and/or implement any changes gradually on the production site.

  1. Keep MaxClients as low as possible. On a low volume site you can likely get away with numbers well under 10. You can also keep other related settings like MaxSpareServers low.
  2. Compare the prefork vs worker modes of Apache to see if there's any significant difference in memory usage.
  3. Eliminate all mods/extensions you are not actually using. This may be more difficult than it sounds as it is not always easy to determine which ones you are using.
  4. Use a low value (100-1000) for MaxRequestsPerChild if you are using any dynamic scripts or are seeings the Apache instances slowing increasing in memory usage over time. This stops memory hungry or leaking scripts from using up too much memory.
  5. Look into compile time memory optimizations only if you really need to, or have a lot of past experience compiling Apache. Personally I would look into alternative servers before this step.

Why not looking at a webserver with small memory footprint: NGINX ? It is acclaimed for having low memory usage and is recommended for sites running on a VPS.

Anyway, 400MB isn't much for a web server these days and you may want to consider replacing the box ! Apache2 eats up a lot of memory. It must be configured, and the configuration options are confusing.

Edit your apache configs, usually in /etc/apache2/apache2.conf

In the mpm_prefork_module settings, set StartServers to 1, MinSpares to 1 or 0, and MaxSpares to something like 3. For example, 10 processes running will overload a 512mb server, (the default minimum for Apache2 is 256mb but you can't do anything with that !). For a reduced footprint/little traffic on the server you can try something like this:

<IfModule mpm_prefork_module>
     StartServers          2
     MinSpareServers       2
     MaxSpareServers       3
     MaxClients          150
     MaxRequestsPerChild   500

Change the above settings as you test traffic loads to see whether the new settings can handle it.

You can tune down Apache's KeepAlive to a lower value (2~5 in my case) who can expect less server processes waiting on connections of idle clients that may not request any more content.

 KeepAlive On
 KeepAliveTimeout 2
 MaxKeepAliveRequests 80

Lower the amount of time the server will wait before failing a request:

 Timeout 45

You can also read this: http://httpd.apache.org/docs/2.0/misc/perf-tuning.html

But again, this setup is for very little traffic and NGINX can do many more with the same hardware..

  • Not an option. NGINX (and similar) uses fewer resources but is no drop-in replacement for Apache. It would entail major code modification for our main site and a complete abandonment of another, which only runs on Apache. Even if I was prepared to do that, it goes against my need to keep disruption to a minimum. – John Gardeniers Nov 2 '11 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.