101 tricks to increase your Apache Web Server Performance

There are no tricks or short cuts to tune Apache for maximum performance. As per my experience, it will take a minimum 10 to 15 days to tweak and force the webserver to provide maximum performance. There are also other bottle necks like bad coding, cross over scripting, improper indexed tables  and hardware issues. So if your webserver is slow then do check the above errors before blaming apache

I would like to share some Apache performance tuning methods to boost your server performance for a minimum of 20 to 40%.

Healthy body hosts a healthy life & mind , First and most important thing is to select a good hardware for your webserver .

A webserver should have a strong CPU support , fast network card and plenty of Ram. Just like other services RAM is the most important spec and never compel a webserver to swap.Selecting the above things also depends on the number of websites hosted and the network traffic. If you are hosting a normal website with average traffic. I will suggest at least 4 GB ram

Old is not always gold , If you need fastest webserver then go for latest softwares

Select latest and stable Os and software’s to setup a perfect web server.  Most of the vendors will introduce significant performance improvements in new versions.
Also update all the 3rd party software’s on your server to latest one to avoid vulnerabilities.

Load less and perform more , Compile time configuration options

1) Load the necessary modules only : –

Apache server is a modular program and which giving a great control to admins to select the necessary modules in the installation time or later.

We can compile apache in two ways , as static binary or as Dynamic Shared Object ( DSO) . Advantage of DSO over static is , if we compile apache as DSO (–enable so)  then we can add or drop modules with out disturbing the running apache using the the tool ” apxs “. But for static installation ,  you need to recompile the whole apache to include an additional module.  For more deatils visit :  LAMP server setup

2) Select Appropriate MPM :-

Apache is comes with a list of Multi process modules ( MPM ). MPM is responsible for binding to network ports on the machine, accepting requests, and dispatching children to handle the requests . We can choose appropriate MPM as per the requirements. To know more about apache mpm modules refer here :

3) Process handling :-

For example ,

  • StartServers 10
  • MinSpareServers 10
  • MaxSpareServers 25
  • ServerLimit 300
  • MaxClients 300
  • MaxRequestsPerChild 10000

With the above configuration, we start with 10-25 processes and set a top limit of  300. Anything above this number will cause serious swapping and thrashing under a load.

The StartServers directive sets the number of child server processes created on startup. As the number of processes is dynamically controlled depending on the load, there is usually little reason to adjust this parameter

The MinSpareServers directive sets the desired minimum number of idle child server processes. An idle process is one which is not handling a request. If there are fewer than MinSpareServers idle, then the parent process creates new children at a maximum rate of 1 per second

The  MaxSpareServers directive sets the desired maximum number of idle child server processes. An idle process is one which is not handling a request. If there are more than MaxSpareServers  idle, then the parent process will kill off the excess processes.

Tuning of this parameter should only be necessary on very busy sites. Setting this parameter to a large number is almost always a bad idea. If you are trying to set the value lower than MinSpareServers, Apache will automatically adjust it to MinSpareServers + 1

The MaxRequestPerChild directive sets the limit on the number of requests that an individual child server process will handle. After MaxRequestPerChild  requests, the child process will die. If  MaxRequestPerChild is 0, then the process will never expire

Note that when more connections are attempted than there are workers, the connections are placed into a queue. The default queue size value is 511 and can be adjusted with the ListenBackLog directive. (The maximum length of the queue of pending connections. Generally no tuning is needed or desired, however on some systems it is desirable to increase this when under a TCP SYN flood attack)

Requests vs. Client Connections

1) KeepAlive

Enable HTTP persistent connections to improve latency times and reduce server load significantly [25% of original load is not uncommon].

prefork MPM:

KeepAlive On
KeepAliveTimeout 2
MaxKeepAliveRequests 80

worker MPM:

KeepAlive On
KeepAliveTimeout 15
MaxKeepAliveRequests 80

With the prefork MPM, it is recommended to set ‘KeepAlive’ to ‘Off’. Otherwise, a client will tie up an entire process for that span of time. Though in my experience, it is more useful to simply set the ‘KeepAliveTimeout’ value to something very low [2 seconds seems to be the ideal value]. This is not a problem with the worker MPM [thread-based].

With the worker, the default 15 second timeout is setup to keep the connection open for the next page request; to better handle a client going from link to link. Check logs to see how long a client remains on each page before moving on to another link. Set value appropriately [do not set higher than 60 seconds].

2) Timeout

Lower the amount of time the server will wait before failing a request.

Other RunTime Configurations : –

1) Hostname Lookups

If you are enabling hostname lookups then Apache will try to log host names instead of IP . This will give lots of additional work to apache , because to every request dns lookup to be completed before finishing the request. Hostnamelookup directive is set to default off prior with apache1.3. Leave it Off and use post-processing program such as logresolve to resolve IP addresses in Apache’s access logfiles. Logresolve is comes with Apache.

2) FollowSymLinks and SymLinkIfOwnerMatch

Wherever in your URL-space you do not have an Options FollowSymLinks, or you do have an Options SymLinksIfOwnerMatch Apache will have to issue extra system calls to check up on symlinks. One extra call per filename component. For example, if you had:

DocumentRoot /www/htdocs
<Directory />
Options SymLinksIfOwnerMatch

and a request is made for the URI /index.html. Then Apache will perform lstat(2) on /www, /www/htdocs, and /www/htdocs/index.html. The results of these lstats are never cached, so they will occur on every single request. If you really desire the symlinks security checking you can do something like this:

DocumentRoot /www/htdocs
<Directory />
Options FollowSymLinks

<Directory /www/htdocs>
Options -FollowSymLinks +SymLinksIfOwnerMatch

This at least avoids the extra checks for the DocumentRoot path. Note that you’ll need to add similar sections if you have any Alias or RewriteRule paths outside of your document root. For highest performance, and no symlink protection, set FollowSymLinks everywhere, and never set SymLinksIfOwnerMatch

3) Allow override

If you are keeping the allow override settings as enable , then apache will search for .htaccess file in each directory . For example

DocumentRoot /usr/local/apache/htdocs
<Directory />
AllowOverride all

If a request is made for URI /index.html, then Apache will attempt to open /.htaccess, /usr/.htaccess, /usr/local/.htaccess, /usr/local/apache/.htacces and /usr/local/apache/htdocs/.htaccess. These additional file system lookups add to the latency. If .htaccess is required for a particular directory, then enable it for that directory alone. If you don’t want .htaccess then use “ AllowOverride None “

4) Negotiation

If at all possible, avoid content-negotiation if you’re really interested in every last ounce of performance.

Instead of using

DirectoryIndex index

Use a complete list of options:

DirectoryIndex  index.cgi index.pl index.shtml index.html

Also use the most common one as first value.

You can read more about negotiation here : http://httpd.apache.org/docs/2.0/content-negotiation.html

5) Extended Status

If mod_status is included, make sure that directive ‘ExtendedStatus’ is set to ‘Off’. Otherwise, Apache will issue several extra time-related system calls on every request made.

Extended Status Off

Performance Boosting Modules

1) mod_expires

Include mod_expires for the ability to set expiration dates for specific content; utilizing the ‘If-Modified-Since’ header cache control sent by the user’s browser/proxy. Will save bandwidth and drastically speed up your site for [repeat] visitors.
Note that this can also be implemented with mod_headers.

2) mod_deflate

With mod_deflate, you can compress HTML, text or XML files to approx. 20 – 30% of their original sizes, thus saving you server traffic

LoadModule deflate_module modules/mod_deflate.so
<Location />
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/x-javascript

Here I have discussed only basic performance tweaks , we need to work closely with a webserver to tune its to get maximum performance .  To achieving maximum performance,  will depends on lots of other things like  how tweaked your mysql/DB server , how secure your server etc.  I am expecting your suggestions and advices on this.

Reference : –  http://httpd.apache.org/docs/2.0/misc/perf-tuning.html