Tuning Apache (httpd) mpm_worker under cPanel/WHM (High end server)

Tuning Apache (httpd) mpm_worker under cPanel/WHM

In this tutorial, we are going to see how to fine tune Apache server (httpd)

Tuning apache on a cPanel server with a slightly different work load (1 very heavily trafficked application) and was provided with a high performance virtual machine (example setup: 4 CPUs, 12 GB memory) to accommodate. The application was to use SSL (HTTPS), so I opted for mpm_worker (mpm_event is nice, but performs similarly to worker when SSL is in use).

By tuning mpm_worker for small (low memory) virtual private servers (VPS), but I was not readily able to find information regarding tuning apache for a larger (high end, high memory) server. Hence, this post was created.

Let will assume that we have already compiled httpd to use the worker mpm (with EasyApache) on your cPanel server. Also note, this process only applies to well-balanced VPS (CPU count scaled to match amount of memory – say 1 CPU core for every 2-3 GB of RAM).

Quick tips

  • cPanel/WHM does not allow adjustment of ThreadsPerChild via the web interface – it defaults to 25 (although you can change this in httpd.conf and distill the configuration, I advise against that)
  • Given the above, MaxClients must be divisible by 25 (ThreadsPerChild)
  • Most tuning is achieved by manipulating MaxClients (MaxRequestWorkers) and ServerLimit

Prework (initial calculations)

To tune httpd with mpm_worker under cPanel, a little background information is required. This process will vary slightly from server to server, but you must calculate the average amount of memory (RAM) that each apache process uses. When performing this calculation, the server should be under “normal” load. If it is idle, the calculation will be inaccurate.

I use top -u and specify the username that httpd runs as (on cPanel, this is “nobody” but many distributions use “apache” as well). We are most interested in the RES column – more specifically, the average RES for all apache processes.

For an example

# top c -u nobody

Tasks: 945 total, 3 running, 927 sleeping, 0 stopped, 15 zombie
Cpu(s): 13.6%us, 4.6%sy, 0.0%ni, 78.0%id, 2.6%wa, 0.0%hi, 1.2%si, 0.0%st
Mem: 32762776k total, 30673144k used, 2089632k free, 2061204k buffers
Swap: 4194300k total, 853120k used, 3341180k free, 23148336k cached

PID  USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
606107  nobody 20 0 312m 116m 2884 S 2.0 0.4 0:00.57 /usr/sbin/httpd -k start
608750 nobody 20 0 312m 117m 3008 S 2.0 0.4 0:00.48 /usr/sbin/httpd -k start
606383 nobody 20 0 312m 117m 2992 S 1.0 0.4 0:00.34 /usr/sbin/httpd -k start
610009 nobody 20 0 312m 116m 2516 S 1.0 0.4 0:00.07 /usr/sbin/httpd -k start
581242 nobody 20 0 0 0 0 Z 0.7 0.0 0:00.48 [httpd] <defunct>
600480 nobody 20 0 312m 116m 2924 S 0.7 0.4 0:00.39 /usr/sbin/httpd -k start
603036 nobody 20 0 318m 120m 2940 S 0.7 0.4 0:00.43 /usr/sbin/httpd -k start
608605 nobody 20 0 312m 116m 2832 S 0.7 0.4 0:00.26 /usr/sbin/httpd -k start
608749 nobody 20 0 312m 116m 2872 S 0.7 0.4 0:00.20 /usr/sbin/httpd -k start

Then, calculate the average memory (RES) consumed per process:

116 + 117 + 117 + 116 + 116 + 120 + 116 + 116 + 116  = 1050 / 9 = 116.6

This means each apache/httpd process consumes 116 MB of memory on average. cPanel uses the default value of 25 for ThreadsPerChild, so we can conclude that for every 25 clients, 116 MB of memory is required.

Calculating your server’s value for MaxClients

Next, we must decide how much memory we can allocate for httpd and impose that limit with Max Clients. To get a baseline, I recommend stopping apache and assessing the amount of available memory on the system.

On the example server,

let’s say that 9259430k (9.5 GB) of memory is available when httpd is not running. Say you are comfortable allocating 75% of that (7 GB) to apache. Use your judgement here – depending on your system, you may need to adjust your allocation. Several factors can affect memory usage as load increases so it is not safe to assume that what is available when apache is stopped will scale linearly. Under no circumstances should you allow apache to swap. If that occurs, your estimate/allocation is too high.

Let’s calculate!

7 GB * 1024 = 7168 MB / 116 MB (from above) = 61 processes (round down) = 100 processes

This means 100 apache processes can run in the 7 GB we allocated. Now, multiply by ThreadsPerChild (25) to obtain Max Clients:

100 * 25 = 2500

For this server, a Max Clients setting of 2500 is reasonable (and since Server Limit simply caps the upper limit, set it to 2500 or greater). Since we rounded down to the nearest 100, MaxClients will be divisible by 25 (it needs to be). If your calculation is different, round down appropriately so that your Max Clients setting is divisible by 25.

Updating cPanel’s httpd configuration

Implementing these settings in WHM is easy:

WHM ==> Service Configuration ==> Apache Configuration ==> Global Configuration

Then, update the 2 settings based on your calculation:

Tuning Apache in cpanel

Tuning Apache in cpanel

Finally, restart apache, and you should be good to go.

It could not hurt to perform a load test against your server as a sanity check. This will ensure your estimate/calculation from above is within spec. Again, you want to keep httpd out of swap at all costs. A load test will also help identify other potential bottlenecks on your server (CPU, disk performance, network bandwidth, etc.). At this point, you may also consider putting your extra threads to work (or wait) by enabling KeepAlive and increasing your Keep Alive Timeout.

Hope this post helps your need, Kindly share your comments to improve us.

To know about cPanel logs Click here

You may also like...