Configuring Apache

By default, Apache reads a single configuration file called httpd.conf. Every Apache source distribution comes with a set of sample configuration files. In the standard Apache source distribution, you will find a directory called conf, which contains sample configuration files with the -dist extension.

The very first step you need to take before you modify this file is to create a backup copy of the original. The httpd.conf file has two types of information: comments and server directives. Lines starting with a leading # character are treated as a comment line; these comments have no purpose for the server software, but they serve as a form of documentation for the server administrator.

You can add as many comments as you want; the server simply ignores all comments when it parses the file. Except for the comments and blank lines, the server treats all other lines as either complete or partial directives. A directive is like a command for the server.

It tells the server to do a certain task in a particular fashion. While editing the httpd.conf file, you need to make certain decisions regarding how you want the server to behave. The httpd.conf file does not actually have any section delimiter. But it will help you understand the configuration file better if you think of it.

There are configuration directives that create the global server environment that applies to everything; there are configuration options that apply to the main (default) Web site Apache servers, and there are configuration directives that only apply to optional virtual hosts. Because Apache uses a single configuration file, a site with lots of virtual hosts will have a very large file and management of the configuration becomes very cumbersome.

The directives discussed in this section create the global environment for the Apache Server. The directives are discussed in the same order in which they appear in the httpd.conf file. The very first directive is ServerRoot, which appears as follows:

ServerRoot “/usr/local/apache”

This directive specifies the top-level directory of the Web server. The specified directory is not where you keep your Web contents. It is really a directory, which normally has these subdirectories:

{ServerRoot Directory} | |----bin |----conf |----htdocs |----htdocs/ | | | +---manual | |----developer | |----howto | |----images | |----misc | |----mod | |----platform | |----programs | |----search | +----vhosts | |----icons | | | +---small | |----logs |----cgi-bin +----include

/usr/local/apache is the parent directory for all server-related files. The default value for ServerRoot is set to whatever you choose for --prefix option during source configuration using the configure script. By default, the make install command executed during server installation copies all the server binaries in %ServerRoot%/bin, server configuration files in %ServerRoot%/conf, and so on.

Note: You should only change the value of this directive if you have manually moved the entire directory from the place on installation to another location. For example, if you simply run cp -r /usr/local/apache /home/apache and want to configure the Apache server to work from the new location, you will change this directive to ServerRoot /home/apache. Note that in such a case you will also have to change other direct references to /usr/local/apache to /home/apache.

Also note that whenever you see a relative directory name in the configuration file, Apache will prefix %ServerRoot% to the path to construct the actual path.

PidFile

The PidFile directive sets the PID (process ID) file path. By default, it is set to logs/httpd.pid, which translates to %ServerRoot%/logs/httpd.pid (that is, /usr/local/apache/logs/httpd.pid). Whenever you want to find the PID of the main Apache process that runs as root and spawns child processes, you can run the cat %ServerRoot/logs/httpd.pid command. Don’t forget to replace %ServerRoot% with an appropriate value.

ScoreBoardFile

ScoreBoardFile is encapsulated within an if condition by using the <IfModule . . .>

ScoreBoardFile logs/apache_runtime_status

This tells Apache to set the ScoreBoardFile to %ServerRoot%/logs/ apache_runtime_status file only if you have chosen a multiprocessing module (MPM) other than perchild. Because the default MPM for most operating systems, including Linux, is threaded instead of perchild, the if condition will be true and Apache will set the ScoreBoardFile directive.

This directive is used to point to a file, which is used to exchange run-time status information between Apache processes. If you have a RAM disk, you might consider putting this file in the RAM disk to increase performance a bit. In most cases, you should leave this directive alone.

Timeout, KeepAlive, MaxKeepAliveRequests, and KeepAlive

Timeout Timeout sets the server timeout in seconds. The default should be left alone. The next three directives KeepAlive, MaxKeepAliveRequests, and KeepAliveTimeout are used to control the keep-alive behavior of the server. You do not need to change them.

IfModule containers

Apache will use one of the next three <IfModule . . .> containers based on which MPM you chose. For example, if you configured Apache using the default MPM mode (threaded) on a Linux system, then the following <IfModule . . .> container will be used:

StartServers 3 MaxClients 8 MinSpareThreads 5 MaxSpareThreads 10 ThreadsPerChild 25 MaxRequestsPerChild 0

On the other hand, if you chose --with-mpm=prefork during source configuration by using the configure script, then the following container will be used:

StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 20 MaxRequestsPerChild 0

Similarly, the --with-mpm=perchild option forces Apache to use the last container.

Directives for threaded (default) MPM behavior

If you followed my advice in the previous chapter, you did not change the default MPM behavior during source compilation and used the threaded behavior, so the directives that you need to consider are discussed below.

  • StartServers - StartServers tells Apache to start three child servers as it starts. You can start more servers if you want, but Apache is pretty good at increasing number of child processes as needed based on load. So, changing this directive is not required.
  • MaxClients - In the default threaded MPM mode, the total number of simultaneous requests that Apache can process is %MaxClients% x %ThreadsPerChild%. So, because the default for MaxClients is 8 and the default for ThreadsPerChild is 25, the default maximum for simultaneous requests is 200 (that is, 8 times 5). If you use the preforking MPM mode, the maximum requests is limited to %MaxClients%. The default maximum of 200 simultaneous requests should work well for most sites, so leave the defaults.
  • MinSpareThreads - The MinSpareThreads directive specifies the minimum number of idle threads. These spare threads are used to service requests and new spare threads are created to maintain the minimum spare thread pool size. You can leave the default settings alone.
  • MaxSpareThreads - The MaxSpareThreads directive specifies the maximum number of idle threads; leave the default as is. In the default threaded mode, Apache kills child processes to control minimum and maximum thread count.
  • ThreadsPerChild - This directive defines how many threads are created per child process.
  • MaxRequestPerChild - The final directive for the global environment is MaxRequestPerChild, which sets the number of requests a child process can serve before getting killed. The default value of zero makes the child process serve requests forever. I do not like to the default value because it enables Apache processes to slowly consume large amounts of memory when a faulty mod_perl script, or even a faulty third-party Apache module, leaks memory. Thus, I prefer to set this to 30.