Basic Apache and MySQL Performance Tuning: Part1: Apache

Apache and MySQL make up the backbone of many Linux based web servers. According to the August 2011 Web Server Survey by Netcraft, Apache currently runs on 65.18% (or 301,771,518!) of web servers.  Similarly, MySQL is the most popular open source database and holds a significant portion of the market share – especially for web content.  This article will detail how you can get at the ‘low-hanging fruit’ in order to make some simple changes that will yield big performance gains.

Apache Tuning

If your system is running httpd, the Apache web server, it will definitely benefit you to evaluate and modify your web server configuration to ensure that your system runs smooth.  This can be an incredibly broad topic and certain an article of its own, but here are a few things you can address that can provide a big impact in your performance.

Firstly, ensure that your MaxClients value is set appropriately for your system.  This will vary based on a great many factors – what kind of content your server is serving up, the kind of PHP or other scripts you’re running, how they’re coded, etc.  Here’s a quick bash one liner that will show you the average memory each httpd process is utilizing:

total=0; for memory in `ps aux | grep [h]ttpd | awk '{print $4}'`; do total=`echo "scale=2; $memory + $total" | bc`; done; echo -n "Average percentage of total memory: "; echo "scale=2; $total/ `ps aux | grep -c [h]ttpd`" | bc

This command will add up the total percentage of memory in use by all httpd processes and average it out among the number of processes.  It’s certainly not perfect, but it will give you a good idea of what each process is doing.  When I ran this command on one of my servers, I had an average percentage of 0.18% of total memory.

Next, you’ll want to see what your MaxClients value is set to in your Apache configuration.  Let’s say it’s set to 256:

[root@server ~]# grep MaxClients /etc/httpd/conf/httpd.conf
 # ServerLimit: maximum value for MaxClients for the lifetime of the server
 # MaxClients: maximum number of server processes allowed to start
 MaxClients       256

To determine the maximum memory that Apache can use based on on the average memory utilization per process of 0.18%, take this value times the MaxClients setting.  This gives us 46.08% of memory – well within normal limits.

For optimum performance, you’ll want to tweak Apache’s MaxClients setting such that Apache can consume no more than 70% of your available physical memory.  Any higher and you risk the box starting to dip heavily into swap space, which will drastically increase IO and CPU wait and can easily cause the server to fall to its knees.

If you’re seeing very high memory utilization per process, it’s possible that it could be due to a memory leak.  The best solution would be to examine your application’s code to locate and resolve the memory leak, but often this isn’t an option for system administrators.  You can mitigate the damage poorly coded applications can have on your server by adjusting the MaxRequestsPerChild directive.  The higher this value is set, the more requests a child process will handle before the parent process kills it off and spawns a new one.  Lowering this value will cause Apache to recycle its children processes more often, freeing up memory that would otherwise be held.  You’ll need to work to find a balance here as each time Apache recycles a child process it must utilize extra CPU time, so setting this value  too high can cause CPU utilization issues.

It’s also a good idea to only enable the modules in Apache that are actually necessary.  Depending on your installation source, you likely have several running and enabled that aren’t necessary and can in fact open up some security holes.  You can check what modules are enabled by calling the Apache binary with the following options (you may need to modify the path and binary name depending on your distro):

/usr/sbin/httpd -t -D DUMP_MODULES

You’ll have many outputted and again, some research will be required to determine exactly what is necessary.  For instance, you might not need the proxy_* modules – proxy_balancer, proxy_ftp_module, etc.  You can often simply comment these out in your httpd configuration.  The Apache httpd manual provides full information on most of these modules so you can determine their necessity:

Also read  25 Apache Performance Tuning Tips