Saturday, January 06, 2007

Apache Performance Tips - Part 1

A few basic tips will go a long way in enhancing your Apache performance. You will have to put in the directives in your httpd.conf or the through a seperate .conf file which you can include through the httpd.conf file.

1. Cache those frequent pages!

How often do you see the Home Page being very slow to load up? A tip would be to use a caching module to cache those pages you think will be loaded very frequently. These pages will be loaded in the memory instead of being retrieved from the filesystem everytime.

Module to use:


How and What to use:

There are 2 directives that you can use MMapFile and CacheFile which you can use to Cache the pages you want.

Read the differences between MMapfile and CacheFile

For Example:

CacheFile /home/web2/index.htm
CacheFile /home/web2/about.htm

The above two directives will cache index.htm and about.htm

Don't forget to read the above module page to find out how to cache the contents of a entire directory ;-)

2. Don't use DNS lookups

You don't use the HostNameLookups directive to lookup for dns names in your log file - This is disabled by default in Apache 1.3 and above versions.

Also, make sure you always use a IP address instead of a domain names in your Allow or Deny directives either. It will cost you a lot of performance.

If you need hostnames in your logfile, you just use logresolve

How to use:

HostNameLookUps Off

3. Keep your site alive

HTTP works by requesting for a document over a new connection. So, everytime you request a document, a new socket connection is established, once served, the connection is closed. The time spent for establishing a connection and closing it can be avoided using a directive called KeepAlive

How to use:

KeepAlive On

Other Related Directives

With the above KeepAlive directive, there are 2 other related directives - MaxKeepAliveRequests and KeepAliveTimeout.

MaxKeepAliveRequests is the number of requests to be allowed over a single connection. Default value for MaxKeepAliveRequests is 100. If you have a lot of memory to spare, you could consider putting MaxKeepAliveRequests to 0 to allow unlimited number of connections

KeepAliveTimeout is used to timeout the connection - you don't want to keep the connection established with the client even though they are not using your site - do you ? 15-20 seconds should be a ideal value for KeepAliveTimeout

How to use:

MaxKeepAliveRequests 100
KeepAliveTimeout 20

4. Consider mod_perl

If you are using CGI perl based web application, it pays to consider to using mod_perl

Read this excellent article on mod_perl

How to do it:

PerlModule ModPerl::PerlRun Alias /cgi-bin/ /opt/apache2/cgi-bin/
<location >
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
Options +ExecCGI
< /location >


PerlModule ModPerl::Registry
Alias /cgi-bin/ /opt/apache2/cgi-bin/
<location >
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
Options +ExecCGI
< /location >
As you see, there are 2 ways to go about using mod_perl, one is the PerlRun method and other is the Registry method basic difference being that in Registry method, code is cached after compilation whereas it isn't . Read more about the porting guidelines

5. Share the load

If you have 2 or more webservers - you can share the load amongst them by shifting some of the pages to the other webserver.

Module to use:


How and What to use:

ProxyPass /images/
ProxyPassReverse /images/

Read about the above directives at Apache documentation be continued ..

Meanwhile you can also go through Apache Tuning

No comments: