Tuesday, June 8, 2010

Configure Apache MaxClients via WHM

SkyHi @ Tuesday, June 08, 2010

We recently moved to a new server provider at work, and we soon noticed that at around 4pm each day the site would slow to a crawl. At this time of day we get in the region of 50-60 requests per second, but we have a beefy server so this shouldn’t be an issue.


The first request to each domain would sit for ages, anywhere up to 10 seconds, then subsequent requests would be in the order of a couple of hundred miliseconds. This lead me to think that there were no available processes on the server and the request was being queued.


Looking into this, I found that indeed Apache was reporting “150 requests currently being processed, 0 idle workers”.


I had a look at the mod_prefork settings, and they were set to the default values targetted to a much smaller site than ours. In the past I’d just have edited http.conf, rebooted and been sorted, but the new server uses WHM which means straight editing of the apache conf won’t stick – WHM will just overwrite any changes.


Having never used WHM before, I googled the issue and came accross a post advocating using the pre-main include to add these settings, however this does not work since the settings from there are over-written back to the measly defaults. Instead the changes need to be made in the pre-virtualhost include.


Here is a step-by-step guide to changing the relevant settings to allow your site to handle more concurrent clients.


  1. Log into WHM
  2. Under Service Configuration in the left menu, select Apache Configuration.
  3. Click Include Editor
  4. In the Pre-Virtualhost Include section, choose All Versions from the drop down.
  5. Enter your config settings. For our server I used the following which works for me:
    1
    2
    3
    4
    5
    6
    7
    <IfModule prefork.c>

        StartServers 32

        MinSpareServers 10

        MaxSpareServers 30

        ServerLimit 512

        MaxClients 512

    </IfModule>

    One thing to bear in mind here. ServerLimit must come before MaxClients! If you swap them you’ll find that you can never use more than 256 for MaxClients and you will get a warning from Apache:


    1
    2
    3
    WARNING: MaxClients of 512 exceeds ServerLimit value of 256 servers,

     lowering MaxClients to 256.  To increase, please see the ServerLimit

     directive.

  6. Click the Restart Apache button to apply the new config
  7. You can check this is working by clicking Server Status > Apache Status. This should tell you how many requests are being processed. If all has gone well this will be a number less than your MaxClients setting!

One word of caution with this. You need to make sure that the MaxClients setting does not cause so many processes to be spawned that you run out of RAM and the OS starts to use swap space. You can check this by finding the amount of RAM an httpd process uses. I use the following command for this, which gives you the average size of all the httpd processes (obviously change “httpd” for whatever your Apache runs as):


1
ps -ef | grep httpd | grep -v ^root | awk '{ print $2 '} | xargs pmap -d | grep ^mapped: | awk '{ print $4 }' | cut -dK -f1 | awk '{ SUM += $1} END { print SUM/NR"KB" }'

Take this value and divide that into your RAM capacity, after allowing enough for other processes such as the OS and mysql etc. This will give you a rough idea how many apache processes you can afford to spawn. On modern hardware, generally you can run more than enough, but if you find you are causing swap to be used, you will either need to remove unnecessary modules to make the apache process smaller, lower the MaxClients or other memory-related directives, or else throw more hardware at the problem!


To give you an idea though, serving all 512 clients should use approx 1.2GB of RAM with my Apache setup.


REFERENCES

http://www.karlrixon.co.uk/articles/apache/configure-apache-maxclients-via-whm/