Apache is one of the most successful open source projects of our times. It runs on a very large number of servers on the Internet. A big advantage of this popularity is that over the years people have spent a great deal of time to figure out ways to fine tune the software to get a better performance out of it; several parameters can be tuned to get the most out of your server. We'll look at a few of them. Note that it is not advisable to be testing any of the suggested modifications to your web server's settings on a production server.
Why Performance Tuning?
This is a valid question that I ask myself often. We live in times when hardware resources are cheap and easily available. Memory and CPU are two very critical elements that define the performance of a server. There are times when I prefer going with a more or less out of the box configuration of Apache for a server rather than spending time tweaking and testing the performance bump. There is no straightforward answer to this question, really. It's a judgment call that you need to make after assessing the requirements of the situation along with the hardware at hand.
One important point to note before we move on to the actual tuning is that the suggested changes in this article are for a generic requirement where you are running a LAMP server. There are many more tweaks that you can make for specific types of setup. Say for example, if you are running the Apache web server as a front end for an application server such as JBoss or Apache Tomcat, things will need to be configured quite differently. All right, enough talk. Now let's get our hands dirty with the performance tuning of Apache. Before proceeding, I suggest you take a backup of your Apache configuration file httpd.conf.
DNS Resolution
HostnameLookups Off
If you feel you need DNS resolution you can optionally allow it for specific file formats. Say, if you want DNS info for hits on PHP and HTML pages, use the following configuration:
HostnameLookups Off HostnameLookups On
One important point to note if you decide to turn off hostname lookup in Apache is that you should avoid using host names in your configuration and use IP addresses instead. If you do use host names, it will take much longer to resolve than usual.
MaxClients
MaxClients is the Apache configuration parameter that sets how many client requests your server should deal with concurrently. This is the hard limit, which is usually set to 256 in the default compiled binaries of Apache. To increase this you need to recompile Apache from source while changing the limit. You can, however, use a lower limit in the default configuration. Depending upon your expected server load you can drop this number down to 150. To make the change open your Apache configuration file, find, and change the following parameter:
MaxClients 150
Persistent Connections
Persistent connections allow your web server to handle multiple requests over the same TCP connection. There are two parameters related to persistent connections - KeepAlive and KeepAliveTimeout. If KeepAlive is set to Off, Apache has to make new connections for each item it transmits. So if your client computer tried to access a web page on your computer that has two images and some text, three client connections will be made to access this page. Whereas, if KeepAlive is turned On the entire contents of the page will be sent over one single connection, thereby allowing you to host more connections on a lower MaxClients limit.
KeepAliveTimout is the parameter that defines in seconds how long the persistent connection should wait before timing out. Another parameter that's useful to configure along with these two is the MaxKeepAliveRequests parameter. This parameter sets how many requests a client can issue across a single connection. The configuration for these parameters would look something like this:
KeepAlive On KeepAliveTimeout 15 MaxKeepAliveRequests 100
Monitoring and Testing
While making the changes suggested above it is a good idea to run tests to see if the changes you made worked as desired, and if they are the changes you need for your setup. For this, you need two sets of tools. One for simulating a real-world like load so that you can push the server to a limit where the parameters you tuned come hit their limits. The other is a set of monitoring tools, which will allow you to monitor the performance of your server under the load.
There are several good web server load simulation software out there. Some are free while others cost money. Some of my favorite ones are apachebench, httperf, and autobench. These are all free tools that you can install on your server infrastructure to simulate a load. The tools I like using for monitoring my server's performance during the load tests are top, vmstat, dstat, and htop.
Conclusion
I have covered some of basic performance tuning parameters offered by Apache. There are several more, which I would recommend you read about in the Apache project's fine documentation. In addition, the testing and monitoring are quite important. If you make the configuration changes to a production server without proper testing it might backfire badly.
Original: December 18, 2009