This is an old revision of the document!


Apache Performance Tuning

Apache Documentation:

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
  • Switch to worker MPM for lower memory consumption and higher performance
  • Calculate memory usage and set the MaxClients directive
  • 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:

free -m
             total       used       free     shared    buffers     cached
Mem:           992        959         32          0         30        409
-/+ buffers/cache:        519        472
Swap:         2055          1       2053

Determine the maximum amount of memory allotted to Apache threads:

ps a -ylC apache2 --sort rss | awk '{print $8'} | tail -n 1 | xargs -I {} echo {} / 1024 | bc

This will return the number in MB.

Also, here's a nice script that outputs the values cleanly:

ps -ylC apache2 | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Proccess Size (MB): "x/((y-1)*1024)}'

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.

<Location />
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/x-javascript application/javascript
</Location>

You can verify that it is working properly if you can see this line in the Network tab of Inspect Element in Chrome:

Content-Encoding: gzip

prefork mpm

prefork is the default MPM worker with our Apache installations.

Default Configuration
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 256
MaxRequestsPerChild 10000
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!

Also see Tuning the Apache MaxClients parameter

Modules

Apache 2.2 ships with a lot of modules, many of which are not needed by most clients. You can recompile Apache to exclude these modules.

Here is a list of basic modules that are needed or generally used: actions alias auth_basic authn_default auth_digest authn_file authz_default authz_host authz_user autoindex cache cgi cgid deflate dir disk_cache env expires file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif status unique_id userdir vhost_alias

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

Apache default is to enable the setting.

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