From Apache: The single biggest hardware issue affecting webserver performance is RAM. A webserver should never ever have to swap, as swapping increases the latency of each request beyond a point that users consider “fast enough”. This causes users to hit stop and reload, further increasing the load.

Apache Tuning 101

These are the best places to start tuning Apache's performance:

Add more RAM

For prefork worker, lower the MaxRequestsPerChild so it flushes processes reasonably, matching system load so it does not use too much memory

Find the average number of concurrent connections (see CGP graphs) and set the MaxClients close to that so it can handle loads.

Run benchmark testing

Disable or lower KeepAlive setting

Lower Timeout setting

Apache Directives

MaxClients

From Apache: “You can, and should, control the MaxClients setting so that your server does not spawn so many children it starts swapping. This procedure for doing this is simple: determine the size of your average Apache process, by looking at your process list via a tool such as top, and divide this into your total available memory, leaving some room for other processes.”

Run benchmark testing to see how the webserver performs with actual and theoretical numbers of concurrent connections.

Tuning MaxClients Example

Let's say your amount of RAM is 992 MB. See how much free memory is available:

Find out how much memory the operating system is using with all running daemons on a regular basis. A simple way to do this would be to stop Apache, then use free -m and get the value for -/+ buffers/cache row and free column.

Once you have that, and the highest number of memory that an Apache thread is using, divide the two numbers.

echo <MB of memory from Apache> / <available system memory> | bc

The output of that should be what MaxClients should be set to.

MaxRequestsPerChild

For the prefork MPM, this is how many requests each process will accept before shutting down and causing Apache to create a new one. These will use more memory as they accept more requests, because of (possible) memory leaks in code.

If a site is not quickly receiving new requests (high load server), it's safe to lower this value so the threads start creating after less requests. It's also recommended to lower this if PHP is built as an Apache module, and you know that the PHP code is either leaking memory, or PHP is exhausting it's memory limit.

mod_deflate

mod_deflate is enabled by default on Gentoo Apache installs, but it won't compress anything with gzip unless enabled.

prefork mpm

Default Configuration

Recommendations

Apache documentation for prefork recommends that MinSpareServers and MaxSpareServers should not be modified unless working with very busy sites. Also, the StartServers directive is a good default, as Apache will handle creating new processes by itself anyway.

MaxClients is the number of simultaneous requests that can be served at one time. The default is 256. If you want to raise it higher, you will need to set ServerLimit as well. From Apache, “You can, and should, control the MaxClients setting so that your server does not spawn so many children it starts swapping.”

Tweaking the MaxClients value to a reasonable number is the best way to prevent your webserver from grinding to a halt!

mod_core

Lower default timeout

This is the amount of time the server will wait for certain events before failing a request. So, consider this a human variable – how long you would want a visitor to try connecting before giving up and dropping it.

Timeout 20

KeepAlive

The KeepAlive directive sets how long Apache will wait for another request once the initial one has been fulfilled. This can slow down clients from accessing the site, since the web server is still waiting for the first one to finish.

For a normal site, enabling the setting is fine, and keeping a low KeepAliveTimeout value (say, 2 seconds).

KeepAlive On
KeepAliveTimeout 2

For a high-traffic site, disable it so that it does not delay creating new connections.

KeepAlive Off

Disable reverse DNS lookups

# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
HostnameLookups Off

Memory Mapping and Sendfile

# EnableMMAP and EnableSendfile: On systems that support it,
# memory-mapping or the sendfile syscall is used to deliver
# files. This usually improves server performance, but must
# be turned off when serving from networked-mounted
# filesystems or if support for these functions is otherwise
# broken on your system.
EnableMMAP On
EnableSendfile On