To download the Apache distribution for Linux, start at the Apache Server website, http://httpd.apache.org/, and follow the Download link. This is the proper place for Linux/Unix distribution files and announcements.
Distribution files follow a naming convention, with httpd followed by the version number, and then the compression type (.tar.gz, .tar.Z, and .zip). As of this writing, the current version is 2.0.49, and I prefer *.tar.gz files, so the file used as an example throughout this section is httpd_2.0.49.tar.gz.
| Note |
The source code distribution should work for most flavors of Unix, but if you have any concerns, read through the Apache documentation at the Apache website to find a better set of files for your specific operating system. |
Once you have downloaded the file of your choice to your hard drive, the following steps will help you build a basic version of Apache.
Type cp httpd_2.0.49.tar.gz /usr/local/ and press Enter to copy the Apache installation file to the /usr/local/src/ directory.
| Note |
You can put Apache anywhere you want on your file system, such as /usr/local/bin/ or /opt/. Just be sure to substitute your path for the path indicated in these directions. |
Go to /usr/local/src/ by typing cd /usr/local/src/ and pressing Enter.
Unzip the Apache installation file by typing gunzip httpd_2.0.49.tar.gz and pressing Enter.
Extract the files by typing tar -xvf httpd_2.0.49.tar and pressing Enter. A directory structure will be created, and you'll be back at the prompt. The parent directory will be /usr/local/src/httpd_2.0.49/.
Enter the parent directory by typing cd httpd_2.0.49 and pressing Enter.
Type the following and press Enter, to prepare to build Apache:
./configure --prefix=/usr/local/apache2 --enable-module=so
The configuration script will run through its process of checking your configuration and creating makefiles, and then will put you back at the prompt.
| Note |
A makefile lists the files, dependencies, and rules required to build an executable application. |
Type make and press Enter. This second step of the installation process will produce many lines of output on your screen. When it is finished, you will be back at the prompt.
Type make install and press Enter. This final step of the installation process will again produce many lines of output on your screen. When it is finished, you will be back at the prompt.
If your installation process produces any errors up to this point, go through the process again or check the Apache website for any system-specific notes. In the next section, you make some minor changes to the Apache configuration file before you start Apache for the first time.
To run a basic installation of Apache, the only changes you need to make are to the server name, which resides in the master configuration file called httpd.conf. This file lives in the conf directory, within the Apache installation directory. So if your installation directory is /usr/local/apache2/, the configuration files will be in /usr/local/apache2/conf/.
To modify the basic configuration, most importantly the server name, open the httpd.conf file with a text editor and look for a heading called Section 2: 'Main' server configuration. You will find two important sections of text.
Change the value of ServerAdmin to your e-mail address.
Change the value of ServerName to something accurate, and remove the preceding # so that the entry looks like this: ServerName somehost. somedomain.com. You do not want it to look like this: #ServerName somehost. somedomain.com.
Save the file.
The ServerName modification is the most important change you'll make to your Apache configuration file, because if the ServerName isn't accurate, you won't be able to connect to Apache on your machine. As it states in the configuration file itself, "You cannot just invent host names and hope they work." If you do not know your full machine name, you can use an IP number. If you have a static IP number (that is, one that does not change), use it as your ServerName. If you have a dial-up connection that does not assign a static IP (that is, your IP number changes each time you connect to your Internet service provider), you will have to change the IP number in httpd.conf each time you dial up.
| Note |
The ServerName changes described here are relevant only if you want people from the outside world to be able to connect to your new web server. If you are the only person who will be accessing the server, you can use the IP number 127.0.0.1, which is recognized by machines as the local loop-back address, also known as localhost. You can use either the word localhost or the IP number 127.0.0.1 as ServerName in httpd.conf. |
Once the appropriate modifications are made to the httpd.conf file, Apache is ready to run on your machine. In the next section, you start and connect to Apache.
There's a handy utility in the bin directory within your Apache installation directory called apachectl. It allows you to issue start, stop, and restart commands. Use this utility to start Apache for the first time.
To get to the Apache installation directory, type cd /usr/local/apache2 and press Enter.
Type ./bin/apachectl start and press Enter.
You should see a message: httpd started. If you do not see this message, you have an error somewhere in your configuration file, and the error message will tell you where to look.
To stop Apache, you can type ./bin/apachectl stop and press Enter. For now, keep it running, as the next step is to connect to the server via a web browser, and this would not be a good time to shut it down.
With Apache running, you can connect to the server via your web browser of choice. The URL will be whatever you used as ServerName—an actual name or IP, or the localhost name or IP.
| Note |
Remember, only you can connect to your web server using 127.0.0.1 or the name localhost. This book assumes that you'll be using 127.0.0.1 as the ServerName, so if you are not, just substitute your machine name for 127.0.0.1 in the examples. |
To finally test your installation, open your web browser, type http://127.0.0.1/ in the location bar, and press Enter. You should see a default web page.
This default start page comes from the htdocs directory within your Apache installation directory. You can go into that directory and delete all the default files if you want to, or you can leave them. They're not hurting anything, but you'll eventually be filling the htdocs directory with your own files and subdirectories, so you might want to delete them for the sake of good housekeeping.
Move ahead to the next chapter, where you install PHP and make a few more minor changes to your Apache configuration files before you're ready for some action.