I have a VPS running a web application served using Apache, that on average deals with 20-50 requests per second. It's usually above this point (50 requests per second) that the amount of memory that Apache uses is too high for the VPS and errors start occuring - web pages crash and VPS falls over for a minute or two before going back to normal levels.

I believe that MaxClients is the best way to reduce the amount of RAM that Apache uses and I am planning to reduce MaxClients from 256 (default value) to around 100. Each Apache process uses ~15MB and the server has 1900MB of ram in total - the server does nothing else other than run Apache and a few crons.

Current setting are:

KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 3

# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# ServerLimit: maximum value for MaxClients for the lifetime of the server
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
ServerLimit      256
MaxClients       256
MaxRequestsPerChild  4000

I tried reducing MaxClients before which lead to massive slowness, so I need some other options as well.

Does my suggestion of reducing MaxClients to ~100 seems sensible? What are my options if the server experiences slowness again - optimise the application? What's the best way to reduce memory usage - move images to another web server?

Any suggestions gratefully received!


The best method for reducing apache's memory usage is to move away from mod_php and to something like fastcgi. Each of your apache processes is 15mb or more because of mod_php overhead (most likely). Having php requests handed off to fastcgi will reduce the average apache process size to approx 1mb or so depending on the apache configuration.

Since php is now centralized using fastcgi it's memory usage is more efficient and the total amount of memory used by the system should decrease slightly.

Another approach would be to place an http server that's more memory efficient in front of apache and have it server static content directly and proxy non-static requests to apache. Nginx would be great for this.

As a temporary fix you can also look into decreasing the MaxRequestsPerChild to 1000 or something more aggressive. Since apache processes tend to grow in size as they serve requests this will limit their size by killing them off and spawning new ones.

  • Just what I was looking for - thanks for the suggestions! – Rhodri Feb 15 '10 at 8:56
  • I like the nginx proxying to apache setup, because you can then use apache mod_rewrite etc as long as your not rewriting links to static resources. Easy setup and quite efficient if you strip down your apache processes to the minimum. – Jords Feb 16 '11 at 2:55

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.