Apache is open source, powerful, and free. These compelling benefits have fueled the adoption of Apache Web Server, which now runs on more than half of all Internet servers. As a developer, I find it imperative to know the products that I'm working with; you need to know how to set up and configure your own Web server for development and possibly production. Let's take a quick look at how to get your Apache server up and running.
Obviously, the first thing you need to do is download the necessary source files. Various mirrors provide the download, so where you get the files is largely a matter of preference. (I tend to download from mirrors because there's not as much traffic as on the main site.) The files are available from the Apache Web site, as well.
At this time, there are two versions of Apache: 1.3.26 and 2.0.40. I used version 1.3.26 in this article, because I use Apache with PHP and there are some issues with Apache 2.0 and PHP. As with any download, I suggest validating it against the provided PGP keys, or at least against the MD5 hashes provided by Apache.
Apache installation and setup
Now that you've downloaded the software, you can begin the installation, which you'll handle completely from the command line. First, extract the files with the following commands:
$ tar -xvzf apache-version.tar.gz
$ cd apache-version/
Now that the source is untarred and ready to go, you can begin the configuration, which offers a wide range of customization options. If you run the command ./configure --help you can see a list of Apache's configuration options, which are divided into various sections:
- General options
- Stand-alone options
- Installation layout options
- Configuration options
- suEXEC options
- Deprecated options
The most used options include installation, configuration, and suEXEC options.
The installation layout options let you pick where specific Apache files and directories will be installed on your system. For example, you can specify where the cgi-bin or htdocs directory will be created. The configuration options install or disable specific modules, enable or disable certain rules, or configure Apache with third-party modules. The features associated with suEXEC can be found under the options with the same name.
With that said, let's get started with our demonstration of a common Apache configuration. We'd enter the following command string:
$ ./configure --prefix=/usr/local/apache-1.3.26 \
The above configuration will give you some options from the various groups. Here's a quick breakdown of what's happening:
- I'm calling the configuration script with various parameters.
- The prefix parameter is telling Apache where to install the architecture-independent files. I am specifying that directory to be /usr/local/apache-1.3.26.
- The next parameter is telling Apache to activate a third-party module. The module is specified with the location src/modules/php4/libphp4.a. This is a PHP module that I'm compiling into Apache.
- The third parameter disables an Apache module called autoindexing.
- The fourth parameter tell the installer to enable an Apache module called so.
- The fifth parameter compiles Apache with a specific Perl interpreter. I have various versions of Perl running on my system, and this parameter tells Apache which one to use. By default, Apache will try and find the Perl interpreter automatically.
After running the configuration script, we can move on with the following commands:
$ make install
If everything goes well, you should be able to run and stop Apache. You can use the following commands:
To start apache:
$ PREFIX/bin/apachectl start
To stop apache:
$ PREFIX/bin/apachectl stop
So now that you have Apache up and running, it's time to review and modify the Apache configuration file, httpd.conf.
To configure Apache, we need to edit the file httpd.conf that's found in the conf directory under the PREFIX directory. In our example install, the httpd.conf file will be located under this directory /usr/local/apache-1.3.26/conf/. You can edit the file with an editor of your choice.
For a full listing of all the run-time directives that may be specified in the configuration file, check out the Apache Web site.
Most directives follow this common format:
- Syntax: directive-name some args
- Default: directive-name default-value
- Context: context-list
- Override: override
- Status: status
- Module: module-name
- Compatibility: compatibility notes
Let's cover a few directives that we'll want to change, beginning with the ServerName directive. I'll locate ServerName in the httpd.conf file, uncomment the line, and insert the server name, as follows:
Next, we'll change the ServerAdmin directive, which defines the e-mail address to which Apache sends alerts. Sometimes this e-mail address appears on pages that the server generates, such as 404 page errors. To modify this directive, make an edit similar to:
For all you PHP lovers out there, here are the directives for setting up PHP4 in the Apache configuration file. This assumes that PHP4 is compiled into Apache and is not a dynamic shared object (DSO), because we're not using the LoadModule directive to load the module into Apache.
AddType application/x-httpd-php .php .php4 .phtml
AddType application/x-httpd-php-source .phps
The above directive is a conditional test to determine if the module name is compiled into Apache or has been dynamically loaded. This test can be of two types: if or if not. The module is loaded into Apache either compiled or loaded as a DSO.
Here is an example:
A good starting point
Our example Apache install should point you in the right direction for your own configurations. The best place I have found for documentation is Apache's Web site, which provides tons of information on configuring directives and other items, such as virtual hosts.