Archive for the ‘Apache’ Category

Editing Virtualhost Settings in WHM / cPanel

Monday, November 30th, 2009

If you are using WHM and you try to edit your Virtualhost settings in your httpd.conf, you will find the following warning message:

1
# DO NOT EDIT. AUTOMATICALLY GENERATED.  IF YOU NEED TO MAKE A CHANGE PLEASE USE THE INCLUDE FILES.

This is not the most helpful warning message. Your first thought is probably that “the include files” refers to the pre_main, pre_virtualhost and post_virtualhost includes, but obviously they are no use when you are trying to edit the Virtualhost settings themselves.

Looking a bit closer inside the Virtualhost blocks in httpd.conf, the following advice is uncovered:

1
2
# To customize this VirtualHost use an include file at the following location
# Include "/usr/local/apache/conf/userdata/std/2/username/domain.com/*.conf"

This helps a bit, but it’s still not especially clear what you need to do. For one thing this file doesn’t exist, and even if you create it the Include line is commented – and the whole point of this exercise is that the http.conf file cannot be manually edited so you can’t just uncomment it!

For anyone with this problem, hopefully the following should explain the full process needed.

First create the file mentioned inside the Virtualhost block. Note that this will be dependent on your username and domain so don’t just copy and paste the path below!

1
mkdir -p /usr/local/apache/conf/userdata/std/2/username/domain.com

Navigate to the directory you just created:

1
cd /usr/local/apache/conf/userdata/std/2/username/domain.com

Create a configuration file using your editor of choice (filename must end in .conf), and add any settings you need:

1
nano extra.conf

Now to uncomment the include, run the following command:

1
/scripts/ensure_vhost_includes --all-users

This script will uncomment any Include lines in httpd.conf Virtualhost blocks where it finds at least one *.conf file in the relevant directory. It will restart Apache for you so no need to do that – your changes should be immediate.


Configure Apache MaxClients via WHM

Monday, November 16th, 2009

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.