Installing Apache on Unix
This section explains how to install Apache on Unix and Unix-like systems such as Solaris, Linux, and FreeBSD.
Checking Whether Apache Is Already Installed in Your System
If you are running a modern Linux distribution, chances are that Apache is already installed in your system. Try the following at the command-line prompt:
# httpd -v
Because some distributions name the Apache binary httpd2, you can also try the following:
# httpd2 -v
If Apache is installed and the binary is in your path, you will get a message with the version and build time:
Server version: Apache/2.0.28 Server built: Dec 29 2001 10:32:01
Otherwise, you will get command not found or a similar message. It might be that Apache is already installed but is in a different path or with a different binary name, such as httpd2. Check whether /usr/local/apache2/ or /etc/httpd2 exists and contains a valid Apache 2.0 installation.
This books covers Apache 2.0, so you must make sure that this is the version installed in your server. An existing 1.3 Apache installation is likely to interfere with your new Apache if the older installation runs at startup. Make sure that either the package is removed from the operating system or the startup script, if any, is disabled. For example, in most Linux distributions, this means modifying the startup scripts at /etc/rc.d/. Apache 1.3 and 2.0 can coexist and run simultaneously if they use different IP address and port combinations, as explained in Hour 4.
If Apache 2.0 is already installed in your system, you can skip the following sections and go directly to Hour 4, "Getting Started with Apache." You can always read this hour later if you decide to build your own server.
Installing from Source
The steps necessary to successfully install Apache from source are
Downloading the software
Running the configuration script
Compiling the code and installing it
These steps are described now in detail.
Downloading the Apache Source Code
The official Apache download site is located at http://www.apache.org/dist/httpd. You can find several Apache versions, packaged with different compression methods. The distribution files are first packed with the Unix tar utility and then compressed either with the gzip tool or the compress utility. Download the .tar.gz version if you have the gunzip utility installed in your system. This utility comes installed by default in open source operating systems such as FreeBSD and Linux. Download the tar.Z file if gunzip is not present in your system, such as in the default installation of many commercial Unix operating systems.
NOTE
The gzip, gunzip, and gtar programs are useful tools. The Gzip Web site at http://www.gzip.org provides you with links to the source code and binaries for Unix platforms such as Solaris, AIX, and HP-UX. If gunzip is not installed but gzip is available in your system, you can use gzip -d instead.
The file you want to download will be named something similar to httpd-2_0_version.tar.Z or httpd-2_0_version.tar.gz where version is the most recent release version of Apache.
Uncompressing the Source Code
If you downloaded the tarball compressed with gzip (tar.gz suffix), you can uncompress it using the gunzip utility (part of the gzip distribution).
NOTE
Tarball is a commonly used nickname for software packed using the tar utility.
You can uncompress and unpack the software by typing the following command:
# gunzip < httpd-2_0*.tar.gz | tar xvf -
If you downloaded the tarball compressed with compress (tar.Z suffix), you can issue the following command:
# cat httpd-2_0*.tar.Z | uncompress | tar xvf -
Uncompressing the tarball creates a structure of directories, with the top-level directory named httpd-2.0_version. Change your current directory to the top-level directory.
Configuring the Software
You can specify which features the resulting binary will have by using the configure script, in the top-level distribution directory. By default, Apache will be compiled with a set of standard modules compiled statically and will be installed in the /usr/local/apache2 directory. If you are happy with these settings, you can issue the following command to configure Apache:
#./configure
For the remainder of the book, it is assumed that you compiled Apache with loadable module support and built most of the modules as such. This, combined with the Apache extension utility (apxs), will enable you to extend the server later with third-party modules without the need to recompile, as described in Hour 18.
To configure Apache this way, issue the following command:
#./configure --enable-so --enable-mods-shared=most
If you are installing Apache as a normal user and you don't have write permissions on /usr/local/, or you simply want to install Apache on a different location, you can specify an alternative location using the --prefix option. For example, the following line:
#./configure --enable-so --enable-mods-shared=most --prefix=/home/username/apache2
will compile Apache to be installed in the home directory of the username user.
The purpose of the configure script is to figure out everything related to finding libraries, compile time options, platform-specific differences, and so on, and to create a set of special files called makefiles. Makefiles contain instructions to perform different tasks, called targets, such as building Apache. These files will then be read by the Unix make utility, which will carry on those tasks. If everything goes well, after executing configure, you will see a set of messages related to the different checks just performed and you will be ready to compile the software.
Compiling and Installing Apache
The make utility reads the information stored in the makefiles and builds the server and modules. Type make at the command line to build Apache. You will see several messages indicating the compilation progress. After compilation is finished, you can install Apache by typing make install. The Apache distribution files will be copied to /usr/local/apache2 or the target directory specified with the --prefix switch.
Apache Compilation Options
The Apache configuration script, configure, can take additional options. Many of them are irrelevant for most users, either because they are rarely used or they relate to building Apache distribution packages. A number of them deal with enabling or disabling specific modules, and those are explained in detail in Hour 18. Table 3.1 describes the most useful configuration options. You can get a complete listing by issuing the ./configure --help command.
Table 3.1 Configuration Options
--with-mpm=mpm |
Specifies the Apache Multi-Processing Module. If this option is not specified, the default MPM for the platform will be compiled in. In Unix, the value for mpm can be either worker, perchild, or prefork. MPMs are discussed in Hour 11, "Multi-Processing Modules." |
--enable-so |
Enables loadable module support. |
--prefix=path |
Apache will be installed relative to the value of the path directory. By default, Apache will be installed in /usr/local/apache2. |
--enable-module |
Enables or disables the specified module. Check Hour 18 for a complete module listing and descriptions. |
--enable-modules=list |
Another way of specifying which modules to build, either compiled into the server or as shared libraries. Both switches can take either a list of modules, all (all modules bundled with Apache), or most (includes the majority of the modules you will need). |
Selecting the Appropriate MPM
Hour 11 explains the role of Multi-Processing Modules. Table 3.2 shows the relationship between platforms and MPMs.
Table 3.2 MPMs and Platforms
Platform |
Available MPMs |
Windows NT/2000/XP |
winnt |
BeOS |
Beos |
OS/2 |
mpmt_os2 |
Linux |
worker, prefork (default), perchild |
Solaris |
worker, prefork (default), perchild |
HP-UX |
worker, prefork (default), perchild |
AIX |
worker (recommended), prefork (default), perchild |
Mac OS X |
worker, prefork (default), perchild |
FreeBSD |
worker, prefork (default), perchild |
Cygwin |
prefork (default) |
In the Windows, BeOS, and OS/2 platforms, there is no choice of MPM and the appropriate one will be selected. In Unix platforms, the default is the prefork MPM, although the worker MPM is probably a better choice, except for some platforms such as FreeBSD. The Cygwin platform supported only the prefork MPM at the time this book was being written. Table 3.2 gives you information about which MPM modules are available for a specific platform. Further information on which MPM to choose, their advantages and disadvantages can be found in Hour 11.
Installing Binaries
This section explains how to install a pre-built Apache server on Unix platforms.
Binaries from the Apache Web Site
You can download binaries for different platforms from the Apache Web site at http://www.apache.org/dist/httpd/binaries. Check whether binaries for your platform are available. You can download and uncompress the tarball as described in the previous section. In this case, the configuration and compilation steps are not necessary. You can install the software by executing the install-bindist script. You can pass an optional argument, the target installation directory. Otherwise, the software will be installed in /usr/local/apache2.
Distribution-Specific Packages
Operating system vendors recognize that Apache is an important server software component and either include it by default or make it available in a distribution-specific format.
For Linux distributions based on the RPM format, you can query whether Apache 2.0 is already installed by issuing this command:
#rpm -q apache2
If Apache 2.0 is not available, you can go to the distribution vendor Web site, download the Apache 2.0 RPM, and install it executing the following command as root:
#rpm -i apache2*.rpm
Apache 2.0 is part of the FreeBSD and other BSD flavors ports collection. Just change to the appropriate directory and type make install. That command will download the source code, and build and install the server.
You can get Apache binaries and packages for Solaris platforms at http://www.sunfreeware.com. Solaris 8 already bundles an Apache 1.3 version, and future releases will likely include Apache 2.0.
Building from CVS
CVS stands for concurrent versioning system and is a popular software development tool that enables programmers to simultaneously work on the same code base, keeping track of changes and revisions and helping to resolve conflicts in the code. Apache is an open source project and makes the source code available via a public CVS server. You can check out any particular release of the code or the latest, unreleased version this way. Compiling and building from CVS is an advanced topic and not recommended for beginners. The following sections comprise a step-by-step guide to compiling Apache from CVS.
The CVS Client
You need the cvs command-line utility to connect to the Apache CVS repository. It is available by default in most Linux distributions and other open source operating systems such as FreeBSD. If cvs is not available for your system, check your vendor package repository or download and compile the source from http://www.cvshome.org.
Checking Out the Source
The main Apache CVS repository is at cvs.apache.org. The first step will be to log in to that server:
# cvs -d :pserver:anoncvs@cvs.apache.org:/home/cvspublic login
You will be prompted for a password; use anoncvs.
To retrieve the Apache source code (check out the code in the jargon), issue the following command:
# cvs -d :pserver:anoncvs@cvs.apache.org:/home/cvspublic co httpd-2.0
The httpd-2.0 directory that contains the Apache source code will be created. The packaged source tarball for Apache includes a couple of libraries that are not present in CVS apr and apr-util. Change your directory to httpd-2.0/srclib/ and execute the following commands:
# cvs -d :pserver:anoncvs@cvs.apache.org:/home/cvspublic co apr # cvs -d :pserver:anoncvs@cvs.apache.org:/home/cvspublic co apr-util
Building the configure Script
You need to change your current directory to httpd-2.0 and execute the ./buildconf command. That will create a configure script. From this step on, you can follow the instructions in the "Building Apache from Source" section earlier in the hour. The buildconf script requires the autoconf utility, which is either already included with your system or can be downloaded from http://www.gnu.org/software/autoconf.
Check Out a Specific Version of Apache
You can check out a specific version of Apache by using the -r tag option to the cvs command-line utility. For example, this line
# cvs -d :pserver:anoncvs@cvs.apache.org:/home/cvspublic co -r APACHE_2_0_28 httpd-2.0
will check out the source code for Apache 2.0.28.