Codehead's Corner
Random ramblings on hacking, coding, fighting with infrastructure and general tech
Things I Learnt Today #3 – How To Configure Apache Properly
Posted: 11 Nov 2011 at 01:41 by Codehead

Recently my Ubuntu Apache server crashed during a busy period on the site, one of the last events logged was:

[error] server reached MaxClients setting, consider raising the MaxClients setting.

The default for MaxClients in apache2.conf is 100. The log suggested raising this value, many sites I Googled up suggested raising the value too. There’s also a fair amount of confusion about the correct setting for StartServers, MinSpareServers and MaxSpareServers values.

After raising MaxClients to 200, the server conitinued to fail at peak times. I raised it to 300 and got a message from Apache that the value exceeded the maximum value and had been rolled back to 256. Juggling the Min/Max values made little difference and the server continued to be unreliable.

I needed the server to be reliable, I sat down to work out the problem logically.

  • I determined the Multi-Processing Model (MPM) that my server was using. apache2 -l lists the modules that are compiled into the executable, look for prefork or worker which indicates which area of the config file to concentrate on.
  • Next I calculated the average and peak memory requirements of each client thread. ps axo ‘size cmd’ | grep apache2 | grep -v grep will list the current apache processes and the memory used by each. Running this command at regular intervals with Cron and piping the output off to file allowed me to calculate an average load of 10Mb per process, with a peak value of 35Mb for one of the larger pages.

Suddenly the reason for the crashes became clear, 256 threads of up to 35Mb works out to around 9Gb of allocated memory. My server only has 512Mb of RAM and a 1Gb swap partition.

Allowing 50Mb for MySQL and another 50Mb for the other system services, I set the MaxClients to 20. This means a maximum memory load of 700Mb from Apache. The server hasn’t crashed since, and although I get the MaxClients message in the logs, there’s no real slow down evident to visitors.

So there you go. If you’re running a site with MediaWiki or Drupal, PHP is generating heavy memory loads on your Apache processes. Even the default value of 100 for MaxClients will seriously overallocate most servers unless you’ve got around 4Gb of memory available. My site gets reasonable traffic (~7000 page views a day) and runs quite happily in 512Mb now that Apache is properly configured.

Tags: #Apache

Site powered by Hugo.
Polymer theme by pdevty, tweaked by Codehead