My article ”Installing Apache Web server on Linux” told you where to download Apache and explained how to install it on your Linux machine. Now I’ll follow up with the basics of configuring the Apache Web server. I’ll also show you how to make sure Apache starts automatically at boot time.
Working with the configuration files
As with many Linux-based programs, configuration for Apache is done through the placement of directives in plain text files. In this case, the files are known as httpd.conf, srm.conf, and access.conf. Their location and importance may differ based on the particular installation, but the format remains the same. Through these files, you may choose where to serve your Web documents from, what modules are dynamically loaded at start-up, and even which port Apache will listen on.
First, let’s look at the reasoning behind the three separate configuration files. This has an almost Holy Trinity-like mystique in the Apache community. They are basically a legacy of the NCSA 1.3 Web server and can be handled in many different ways.
One option is to use httpd.conf as the server-wide configuration, srm.conf as controlling naming and resources, and access.conf as setting access control for the server. Another approach is to use only httpd.conf, which will support any directive that can be placed in either of the other two files. By placing the options AccessConfig /dev/null and ResourceConfig /dev/null in httpd.conf, you can tell Apache to look only to it for its configuration.
Another interesting method is to name directories httpd.conf, srm.conf, and access.conf, respectively. The server will automatically parse any files and subdirectories located there into the aforementioned configuration files. This will let you break down items such as VirtualHosts to a very specific degree.
Location, location, location
Now that we’ve looked at why the configuration files exist in their current state, let’s see where they’re located. Although the Apache root directory will vary depending on installation options, these files will usually reside in the /conf subdirectory. For example, if you installed Apache with all defaults, the configuration files would be located in /usr/local/apache/conf.
This does not mean that you must use those particular files every time. The –f command-line option allows for Apache to load its configuration from a user-defined file at run time. Also, preconfigured packages from distributions such as Red Hat and Debian may install to different directories. Under the current version of Debian, the configuration files can be found in /etc/apache. If you’re using Red Hat, you may want to check under /etc/httpd/conf.
If you’ve run more than one version of Linux before, you know that the directory structure and file locations can vary. When in doubt, running locate httpd or locate apache with an updated database of files can be helpful in pinpointing the exact location.
Configuring the server
Directives are entered into the configuration files one per line and are case-sensitive. Placing a backslash at the end of the command can extend longer entries into multiple lines. If you are not quite sure you entered the directives correctly, Apache provides for some basic error checking of your syntax by running apachectlconfigtest. If Apache sees a problem with a particular command, it will give you the line number. Remember that you must still run apachectl start to initialize the server. Let’s take a look at some standard Apache directives taken from httpd.conf (which you can open with the text editor of your choice, such as vi or pico):
# ServerType is either inetd or standalone.
# Port: The port the standalone listens to. For ports < 1023, you will
# need httpd to be run as root initially.
# Document Root: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
# Dynamic Shared Object (DSO) Support
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding ‘LoadModule' lines at this location.
LoadModule php4_module /usr/lib/apache/1.3/libphp4.so
# AddType allows you to tweak mime.types without actually editing it, or to
# make certain files to be certain types.
AddType application/x-httpd-php .php .php3 .php4
Notice that these directives have comments located before them that help to explain the syntax and functionality. Almost every configuration option is explained to some degree in the files themselves—a nice feature if you’re like me and tend to read the documentation only when you can’t figure something out otherwise. An important one to notice is the LoadModule command. Upon installing programs related to Apache, you may need to edit the configuration file(s) to load the modules at start-up. If you installed PHP after Apache, you will need the above command in combination with an AddType directive to make sure Apache will process PHP files.
The above examples are fairly standard and may not need editing. Some directives, however, will change from server to server. Some examples of these are:
- · ServerName
- · ServerAdmin
- · BindAddress
The ServerName directive sets the host name of the server and is usually a fully qualified domain name. This saves the server from trying to do a lookup of the host name by IP address, which may not return the result you want. An example of how to use this would be:
The ServerAdmin option is also straightforward. It is merely the e-mail address of the person to whom administrative and error e-mails are sent. If multiple Web servers are being run, a centralized address used here can be helpful.
BindAddress tells Apache to listen to either all IP addresses on the machine or just one. This can be important if you are multihoming and don’t want certain IP addresses to respond to HTTP requests. The default for this command is BindAddress *, which tells the server to listen for connections on every IP address.
Apache can either be started manually or automatically at boot time. For manual operation, you use the command apachectl. As mentioned previously, multiple arguments can be passed to this command. To start Apache, you execute apachectl start. To restart the server after making changes to the configuration, you use apachectl restart or apachectl graceful. The graceful switch sends a SIGUSR1, as opposed to the SIGHUP used with restart.
To load the server during boot time, you can either place a script in init.d orrc.N or place a call to apachectl from rc.local or rc.boot. (These files and locations will vary depending on your distribution.) For example, here is my script in /etc/init.d/ on a Debian/GNU system.
There will be as many ways to load Apache at boot time as there are versions of Linux. You may need to consult your specific documentation if the above suggestions do not work on your Linux system.
The Apache Web server, as with any full-featured program, has a variety of configuration options. Stored in plain text files, the main configuration files are easily accessible through the text editor of your choice. In addition to the comments in the config files, Apache provides excellent documentation on directives and configuration options at http://httpd.apache.org/docs.
This article introduced the basics of setting up Apache on a Linux system. Hopefully, I’ve provided you with enough fundamental knowledge to configure and start your server. However, I would recommend taking a further look at the documentation before beginning your configuration and making note of the features you will need. That way, you can configure your Apache server to do exactly what you need it to do.
What tips do you have for working with Apache?
We look forward to getting your input and hearing about your experiences regarding this topic. Join the discussion below or send the editor an e-mail.