Basic Apache Optimizations

Apache, the most popular web server, is a stable and reliable software for providing web pages to the world. But sometimes, it happens that Apache will struggle under high loaded traffic.

Here is an overview on how you can optimize apache so it can cope with higher loads. Those tips are not meant to be exceptional but should be sufficient to increase your web site performance by handling more traffic.

When a web site gets too many connections at the same time, the web site will start to be slow to respond to the request, either because the host is not configured properly or because the bandwidth is getting insufficient.
There is few simple steps that can be taken in order to handle more traffic.

DNS Look Up

A good way to enhance your server response time is to disable DNS LookUp. When DNS Look Up is activated, Apache will perform a name resolution each time a new client connect to the server in order to record the full host name of the client. Disabling DNS LookUp, Apache will only save the IP of the client.

Web statistics software such as awstats can perform name resolution if you still need to get that information, but keep in mind that this will increase the time required by awstats to process your logs.

MaxClients

On start up, Apache will create a number of processes (Servers), and will handle a maximum of simultaneous requests (MaxClients). Any connection attempts over MaxClients will be queued. The MaxClients maximum value is capped by Here is a default setting for a Debian/Ubuntu box:

<IfModule prefork.c>
StartServers         5
MinSpareServers      5
MaxSpareServers     10
MaxClients          20
MaxRequestsPerChild  0
</IfModule>

In order to handle more request at the same time, someone could use settings like:

<IfModule prefork.c>
StartServers       15
MinSpareServers    10
MaxSpareServers   20
ServerLimit      512
MaxClients       512
MaxRequestsPerChild  0
</IfModule>

Note that in order to exceed the 256 MaxClients default capped value, we re-defined ServerLimit with a value of 512.

A simple tips to know if your server needs a bigger MaxClients value, is to check for:

[error] server reached MaxClients setting, consider raising the MaxClients setting

in your error log file (usually /var/log/apache2/error.log)


Basic Apache Optimizations -- page 2

KeepAlive

KeepAlive is a feature that make the server keep each listening connection alive for a certain amount of time, allowing a client and the server to keep the same connection for a certain amount of time. This feature has benefits and drawbacks.
A benefit would be that a client making more than one request won't have to re-initiate a new connection for each request.
A drawback will be that ressources on the server are monopolize for a predefined time, even though the client might not request other pages.

By default, on Debian and Ubuntu, KeepAlive is set to On and the KeepAliveTimeout to 15, so if you get 256 different connections per seconds during 15 seconds, you could (theorically) have to allocate memory for 3840 connections :s .

There is 2 options in order to handle this:

Compress HTTP Content

If you are running out of bandwidth, it could be worth using mod deflate.
Mod deflate is an Apache module compressing HTTP content on the fly.
Because data will be compressed, your bandwidth will be saved, but as a drawback, you will need more CPU resources per request.