423,103 Members | 1,428 Online
Bytes IT Community

Optimizing Apache Web Server

By Jillian Carroll
Administrator, TheScripts.com

Optimize your Apache Server

Once your web site becomes popular, you will have to start worry about another issue, how to get the most out of your server. With the traffic loads pounding it constantly, you will have to find a way to optimize it to handle the traffic a bit better.

That is where this part of the tutorial comes into play. Before we begin, I must state that all of these tips will be aimed at unix servers, as Apache is not optimized for Windows at all.

When looking at speed issues and Apache, we must look at the hardware/operating system, your configuration files, and how you compiled the server from the start.

For those of you that want to read all of your performance options, check out http://www.apache.org/docs/misc/perf-tuning.html and http://www.apache.org/docs/misc/perf.html. Also you might want to check out the O'Reilly book, Web Performance Tuning.

File Lookups

This is something you might not have even thought of. The two culprits here are simply .htaccess files and symbollic links. For those of you that are looking at the page funny, .htaccess files are a type of access control file. They can contain Apache Directives, and apply to the directory they are in and everything below it, overridden only by other .htaccess files. By default, the Apache Directive AccessFileName is set to .htaccess, so it is known as that unless you changed it.

So you can really think of it as just another Apache configuration file, though it is optional.

When returning a document to the client the server looks for an access control file with this name in every directory of the path to the document, if access control files are enabled for that directory. For example, say I was getting a document found at http://www.mysite.com/test/test2/test3/test4.html and my DocumentRoot directive is set to /usr/local/apache/htdocs. Apache would look for an .htaccess file in the following directories;

/
/usr
/usr/local
/usr/local/apache
/usr/local/apache/htdocs
/usr/local/apache/htdocs/test
/usr/local/apache/htdocs/test/test2
/usr/local/apache/htdocs/test/test2/test3

Do you see what I'm getting at? With access control files enabled, Apache has to do quite a bit of checking for unnecessary files, which robs you of some performance.

So how do we fix this problem? We can use the Directory container to specify no overrides are acceptable within the container. In this case, we use the root directory.

<Directory />
AllowOverride None
</Directory>

After that, we may want to be able to still use .htaccess files. If this is the case, you can enable them again,

<Directory /usr/local/apache/htdocs>
AllowOverride All
</Directory>

And there you go. The AllowOverride directive has a few options actually, to see them, check out http://www.apache.org/docs-1.2/mod/core.html#allowoverride and http://www.apache.org/docs-1.2/mod/core.html#options.

Symbollic links have a similar issue, and the fix is nearly the exact same, except different directives are used.

<Directory />
    Options FollowSymLinks
</Directory>

Followed by

<Directory /usr/local/apache/htdocs>
    Options -FollowSymLinks +SymLinksIfOwnerMatch
</Directory>

Now we got rid of the file lookup problem, as there are no unnecessary file checks.

Hostname Lookups

Apache, by default, has the directive HostnameLookups set to off. This is a good thing, as, with it on, each client request to your server would result in a lookup request to the nameserver.

This is also a problem if you are restricting access to particular sections of your site with the order directive, accompanied by allow and deny. It is best if you use IP addresses in this case, as otherwise, you have to do another nameserver lookup.

With that out of the way, your Apache server doesn't have to do any unnecessary DNS stuff, and can stick to it's job.

If your CGI's, PHP or whatever else might require using hostname lookups, you can use a file container to specify this.

<Files ~ "\.(php3|cgi)$>
    HostnameLookups on
</Files>

Content Negotiation

Here I go again with my fancy words, content negotiation. I know I suggested using it earlier, but that's only if it would benefit you. Otherwise, you should stay away from it.

Why? Since it requires searching for particular files, and not exact names, the searching part puts a little more load on the server, therefore slowing it down just a tad. Also, for directives that ask you for files, such as DirectoryIndex, instead of just putting index, put the actual file name. The example is below;

DirectoryIndex index

Index is a wild card, as there is no extension specified, and it has to do some searching again to find the file. Putting a list of names would be a much quicker, more efficient method. Put the most common name first, and in descending order.

DirectoryIndex index.html index.shtml index.cgi

Hardware

As with most software, Apache needs RAM, especially if you get a lot of traffic. If it gets to the point that your operating system has to start swapping memory, with the hard drive, you must either get more RAM or set the directive MaxClients a little lower.

Swapping memory isn't necessarily slow, but for the client, it is. They will get irritated, hit stop-reload a few times, and further the load on your server. Not a good thing.

Besides that, with a fast enough hard drive, network card and CPU, your server should perform quite well (hardware-wise at least). You will have to experiment though to optimize it the best.

Server Processes

Settings such as MinSpareServers, MaxSpareServers, StartServers, and KeepAliveTimeout (if KeepAlive is set to 'On'). The SpareServers directives instruct the server how many 'child' processes to create. Apache uses these directives to determine the load it has to adapt to.

If your server has children waiting for requests, and the amount exceeds the MaxSpareServers setting, some of those child processes will be killed. If the amount is less than MinSpareServers, some child processes will be created.

The settings of these directives will directly relate to your server performance. If you don't have enough child processes to handle the current server load, some requests will be delayed, causing your server to be slower. Too many processes will also slow down your server, if you don't have enough RAM and CPU power to handle it.

« Configure Apache Part 2