Installing and Configuring Apache - Getting Up and Running - Sams Teach Yourself PHP, MySQL and Apache All in One (2012)

Sams Teach Yourself PHP, MySQL and Apache All in One (2012)

Part I. Getting Up and Running

Chapter 3. Installing and Configuring Apache


In this chapter, you learn the following:

How to install the Apache server

How to make configuration changes to Apache

Where Apache log and configuration files are stored


In this chapter, you install the Apache web server and familiarize yourself with its main components, including log and configuration files.

Current and Future Versions of Apache

The Apache HTTPD server website at http://httpd.apache.org shows announcements for releases of the Apache 2.0.x, Apache 2.2.x, and Apache 2.4.x versions. As you can understand by the version numbers, Apache 2.0.x is the oldest of the three versions. The Apache Software Foundation maintains all three versions, but the features in Apache 2.4.x include the latest and greatest; it is the version used in this chapter. However, if you choose to install (or already have installed either in a local or external development environment) Apache 2.2.x or even Apache 2.0.x, all the PHP and MySQL code in this book will still work as described. In fact, you will find a number of hosting providers still using Apache 2.0.x—not even Apache 2.2.x, let alone the newest Apache 2.4.x branch. If you experience any issues with installing Apache 2.4.x as described in this chapter, try an earlier version (for example, Apache 2.2.x); the installation instructions are remarkably similar. The installation instructions in this chapter refer to Apache HTTPD server version 2.4.1 except where noted, which is the best available version of the software at the time of this writing.

The Apache Software Foundation uses minor and revision numbers for updates containing security enhancements or bug fixes. Neither minor nor revision releases follow a set release schedule. When enhancements or fixes are added to the code and thoroughly tested, the Apache Software Foundation releases a new version.

By the time you purchase this book, the version number might have changed to 2.4.1 or later. If so, read the list of changes, which is linked from the download area at http://httpd.apache.org/download.cgi, for any installation/configuration process changes. These processes make up the bulk of this chapter.

Although it is unlikely that any installation instructions will change between version updates, always check the changelog of software that you install and maintain. If a minor or revision change does occur while you are reading this book but the changelog notes no installation changes, just make a mental note and substitute the new version number wherever it appears in the installation instructions and accompanying figures.

Choosing the Appropriate Installation Method

You have several options when it comes to getting a basic Apache installation in place. Apache is open source, meaning that you can have access to the full source code of the software, which in turn enables you to build your own custom server. In addition, prebuilt Apache binary distributions are available for most modern UNIX platforms. Finally, Apache comes already bundled with a variety of Linux distributions, and you can even purchase commercial versions with support packages from vendors. The examples in this chapter teach you how to build Apache from source if you are using Linux/UNIX, and how to use the installer if you plan to run Apache on a Windows system.

Building from Source

Building from source gives you the greatest flexibility because it enables you to build a custom server, remove modules you do not need, and extend the server with third-party modules. Building Apache from source code enables you to easily upgrade to the latest versions and quickly apply security patches, whereas updated versions from vendors can take days or weeks to appear. The process of building Apache from source is not especially difficult for simple installations, but can grow in complexity when third-party modules and libraries are involved.

Installing a Binary

Linux/UNIX binary installations are available from vendors or you can download them from the Apache Software Foundation website. Binary installations provide a convenient way to install Apache for users with limited system administration knowledge or with no special configuration needs. Third-party commercial vendors provide prepackaged Apache installations together with an application server, additional modules, support, and so on. The Apache Software Foundation provides an installer for Windows systems—a platform where a compiler is less commonly available than in Linux/UNIX systems.

Installing Apache on Linux/UNIX

This section explains how to install a fresh build of Apache 2.4.1 on Linux/UNIX. The general steps necessary to successfully install Apache from source are as follows:

1. Download the software.

2. Run the configuration script.

3. Compile the code and install it.

The following sections describe these steps in detail.

Downloading the Apache Source Code

The official Apache download site is located at http://httpd.apache.org/download.cgi. You can find several versions of the Apache source code, packaged with different compression methods. The distribution files are first packed with the tar utility and then compressed with either the gziptool or the compress utility. Download the *.tar.gz version if you have the gunzip utility installed on 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. (It isn’t included in the default installation of many commercial UNIX operating systems.)

The file you want to download will be named something similar to httpd-VERSION.tar.gz, where VERSION is the most recent release of Apache. For example, Apache version 2.4.1 is distributed as a file named httpd-2.4.1.tar.gz. Keep the downloaded file in a directory reserved for source files, such as /usr/src/ or /usr/local/src/.

Uncompressing the Source Code

If you downloaded the tarball compressed with gzip (it will have a 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.4*.tar.gz | tar xvf -

Uncompressing the tarball creates a structure of directories, with the top-level directory named httpd-VERSION. Change your current directory to this top-level directory to prepare for configuring the software.

Preparing to Build Apache

You can specify which features the resulting binary will have by using the configure script in the top-level distribution directory. By default, Apache is compiled with a set of standard modules compiled statically and is installed in the /usr/local/apache2 directory. If you are happy with these settings, you can issue the following command to configure Apache:

# ./configure

However, in preparation for the PHP installation in Chapter 4, “Installing and Configuring PHP,” you need to make sure that mod_so is compiled into Apache. This module, named for the UNIX shared object (*.so) format, enables the use of dynamic modules such as PHP with Apache. To configure Apache to install itself in a specific location (in this case, /usr/local/apache2/) and to enable the use of mod_so, issue the following command:

# ./configure --prefix=/usr/local/apache2 --enable-so

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. The make utility reads these files and carries out the targets’ tasks. If everything goes well, after executing configure, you will see a set of messages related to the different checks just performed and will return to the prompt:

...
configure ok
creating test/Makefile
config.status: creating docs/conf/httpd.conf
...
config.status: executing default commands
#

If the configure script fails, warnings appear, alerting you to track down additional software that must be installed, such as compilers or libraries. After you install any missing software, you can try the configure command again, after deleting the config.log and config.status files from the top-level directory.


Caution

If the configuration process ends with a warning that you do not have APR installed, go to httpd://apr.apache.org/ and download both the APR and APR-util packages, and unpack them in the srclib subdirectory of your httpd-VERSION source directory. Once they are installed, rerun the configure command.

Similarly, if the configuration process ends with a warning that you do not have PCRE installed, go to http://www.pcre.org and download the files and install it on your system according to the instructions found at the website. Once installed, rerun the configure command.

Both of these requirements are changes in the requirements for the Apache 2.4.x installation process, different from the Apache 2.2.x process.


Building 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 progress of the compilation, and you will end up back at the prompt. After compilation is finished, you can install Apache by typing make install at the prompt. The makefiles install files and directories and return you to the prompt:

...
Installing header files
Installing build system files
Installing man pages and online manual
...
make[1]: Leaving directory '/usr/local/bin/httpd-2.4.1'
#

The Apache distribution files should now be in the /usr/local/apache2 directory, as specified by the --prefix switch in the configure command. To test that the httpd binary built correctly, type the following at the prompt:

# /usr/local/apache2/bin/httpd -v

You should see the following output (your version and build date will be different):

Server version: Apache/2.4.1 (Unix)
Server built: March 12 2012 11:47:22

Unless you want to learn how to install Apache on Mac OS X or Windows, skip ahead to the “Apache Configuration File Structure” section to learn about the Apache configuration file.

Installing Apache on Mac OS X

Lucky you, Apache is already installed on Mac OS X! By default, the Apache server binary is located at /usr/sbin/httpd. Configuration files such as httpd.conf, the master configuration file for Apache, are in /etc/httpd. Because Apache is ready to go and fully prepared to use PHP, skip ahead to the “Apache Configuration File Structure” section to learn more about the Apache configuration file and how to use it.


Note

If you want to use an all-in-one package installer for Mac OS X, you can do so as shown with XAMPP in Chapter 1, “Installation QuickStart Guide with XAMPP,” or you can install the MAMP package from http://www.mamp.info.


Installing Apache on Windows


Caution

At the time of this writing, Apache 2.4.x is not available for Windows. Therefore, the instructions that follow are for Apache 2.2.x. The installation process will be similar for Apache 2.4.x when it becomes available.


Apache 2.2 runs on most Windows platforms and offers increased performance and stability over earlier versions for Windows. You can build Apache from source, but because not many Windows users have compilers, this section deals with the MSI installer version.

Before installing Apache, make sure that you are not currently running a web server (for instance, a previous version of Apache or Microsoft Internet Information Services) on your machine. You might want to uninstall or otherwise disable existing servers. You can run several web servers, but they must run in different address and port combinations.

Before downloading the installer, take a moment—a very important moment—and look for a statement on the downloads page (found at http://httpd.apache.org/download.cgi) that says “If you are downloading the Win32 distribution, please read these important notes.” The direct URL to these notes is http://www.apache.org/dist/httpd/binaries/win32/README.html.

The Apache Software Foundation maintains this page for the benefit of Windows users who want to run a version of the Apache server. On this page, there are notes for nearly every flavor of Windows still in use, and it is in your best interest to read the information presented. I guarantee that if you are running Apache as either a production or development server, you will find relevant information on the notes page.

When you’re ready to begin the installation, look for the link labeled Win32 Binary including OpenSSL(MSI Installer). After you download the installer, double-click the file to start the installation process. You will get a welcome screen, as shown in Figure 3.1.

image

Figure 3.1 The Windows installer welcome screen.

Click Next to continue the installation process, and you will be prompted to accept the Apache license. Basically, the license says that you can do whatever you want with the software—including making proprietary modifications—except claim that you wrote it, but be sure to read the license so that you fully understand the terms.

After you accept the license, the installer presents you with a brief introduction to Apache. Following that, it asks you to provide basic information about your computer, as shown in Figure 3.2. This includes the full network address for the server (for instance, mycomputer.mydomain.com) and the administrator’s email address. The server name is the name your clients will use to access your server, and the administrator email address is added to error messages so that visitors know how to contact you when something goes wrong.

image

Figure 3.2 The basic information screen.

Also on this screen, you are prompted to select which installation shortcuts should be installed—those for starting Apache as a service or those for starting Apache manually. Installing Apache as a service causes it to run every time Windows starts, and you can control it through the standard Windows service administration tools. Installing Apache for the current user requires you to start Apache manually and set the default port on which Apache listens to requests to 8080 (instead of 80). Select the appropriate radio button and click Next to continue.


Tip

If your machine does not have a full network address, use localhost or 127.0.0.1 as the server name.


The next screen enables you to choose the type of installation: typical or custom. A typical installation means that Apache binaries and documentation are installed, but headers and libraries are not. This is the best option to choose unless you plan to compile your own modules.

A custom installation enables you to choose whether to install header files or documentation. After selecting the target installation directory, which defaults to c:\Program Files (x86)\Apache Software Foundation\Apache 2.2, the program proceeds with the installation process. If everything goes well, it presents you with the final screen, shown in Figure 3.3.

image

Figure 3.3 The successful installation screen.

In the next section, you learn about the Apache configuration file and eventually start up your new server.

Apache Configuration File Structure

Apache keeps all its configuration information in text files. The main file is httpd.conf. This file contains directives and containers that enable you to customize your Apache installation. Directives configure specific settings of Apache, such as authorization, performance, and network parameters. Containers specify the context to which those settings refer. For example, authorization configuration can refer to the server as a whole, to a directory, or to a single file.

Directives

The following rules apply for Apache directive syntax:

• The directive arguments follow the directive name.

• The directive arguments are separated by spaces.

• The number and type of arguments vary from directive to directive; some have no arguments.

• A directive occupies a single line, but you can continue it on a different line by ending the previous line with a backslash character (\).

• The pound sign (#) should precede the directive, and must appear on its own line.

The Apache server documentation offers a quick reference for directives at http://httpd.apache.org/docs/2.4/mod/quickreference.html. You’ll soon learn about some of the basic directives, but you should supplement your knowledge using the online documentation.

The Apache documentation for directives typically follows this model:

Description—This entry provides a brief description of the directive.

Syntax—This entry explains the format of the directive options. Compulsory parameters appear in italics, and optional parameters appear in italics and brackets.

Default—If the directive has a default value, it appears here.

Context—This entry details the containers or sections in which the directive can appear. The next section explains containers. The possible values are server config, virtual host, directory, and .htaccess.

Override—Apache directives belong to different categories. The Override field specifies which directive categories can appear in .htaccess per-directory configuration files.

Status—This entry indicates whether the directive is built in Apache (core), belongs to one of the bundled modules (base or extension, depending on whether they are compiled by default), is part of a multiprocessing module (MPM), or is bundled with Apache but not ready for use in a production server (experimental).

Module—This entry indicates the module to which the directive belongs.

Compatibility—This entry contains information about which versions of Apache support the directive.

Further explanation of the directive follows these entries in the documentation, and a reference to related directives or documentation might appear at the end.

Containers

Directive containers, also called sections, limit the scope for which directives apply. If directives are not inside a container, they belong to the default server scope (server config) and apply to the server as a whole.

These are the default Apache directive containers:

<VirtualHost>—A VirtualHost directive specifies a virtual server. Apache enables you to host different websites with a single Apache installation. Directives inside this container apply to a particular website. This directive accepts a domain name or IP address and an optional port as arguments. You learn more about virtual hosts in Chapter 29, “Apache Performance Tuning and Virtual Hosting.”

<Directory>, <DirectoryMatch>—These containers allow directives to apply to a certain directory or group of directories in the filesystem. Directory containers take a directory or directory pattern argument. Enclosed directives apply to the specified directories and their subdirectories. The DirectoryMatch container allows regular expression patterns to be specified as an argument. For example, the following allows a match of all second-level subdirectories of the www directory and made up of four numbers, such as a directory named after a year and month (0212 for February 2012):

<DirectoryMatch "^/www/.*/[0-9]{4}">

<Location>, <LocationMatch>—These containers allow directives to apply to certain requested URLs or URL patterns. They are similar to their Directory counterparts. LocationMatch takes a regular expression as an argument. For example, the following matches directories containing either "/my/data" or "/your/data":

<LocationMatch "/(my|your)/data">

<Files>, <FilesMatch>—Similar to the Directory and Location containers, Files sections allow directives to apply to certain files or file patterns.

Containers surround directives, as shown in Listing 3.1.

Listing 3.1 Container Directives Example


1: <Directory "/some/directory">
2: SomeDirective1
3: SomeDirective2
4: </Directory>
5: <Location "/downloads/*.html">
6: SomeDirective3
7: </Location>
8: <Files "\.(gif|jpg)">
9: SomeDirective4
10: </Files>


Directives SomeDirective1 and SomeDirective2 apply to the directory /some/directory and its subdirectories. SomeDirective3 applies to URLs referring to pages with the .html extension under the /downloads/ URL. SomeDirective4 applies to all files with .gif or .jpg extensions.

Conditional Evaluation

Apache provides support for conditional containers. Directives enclosed in these containers are processed only if certain conditions are met:

<IfDefine>—Directives in this container are processed if a specific command-line switch is passed to the Apache executable. The directive in Listing 3.2 is processed only if the -DMyModule switch is passed to the Apache binary being executed. You can pass this directly or by modifying the apachectl script, as described in the “Apache-Related Commands” section later in this chapter.

IfDefine containers also allow you to negate the argument. That is, directives inside a <IfDefine !MyModule> section—notice the exclamation point before the MyModule name—are processed only if no -DMyModule parameter is passed as a command-line argument.

<IfModule>—Directives in an IfModule section are processed only if the module passed as an argument is present in the web server. For example, Apache ships with a default httpd.conf configuration file that provides support for different MPMs. Only the configuration belonging to the MPM compiled into Apache is processed, as you can see in Listing 3.3. The purpose of the example is to illustrate that only one of the directive groups will be evaluated.

Listing 3.2 IfDefine Example


1: <IfDefine MyModule>
2: LoadModule my_module modules/libmymodule.so
3: </IfDefine>


Listing 3.3 IfModule Example


1: <IfModule prefork.c>
2: StartServers 5
3: MinSpareServers 5
4: MaxSpareServers 10
5: MaxClients 20
6: MaxRequestsPerChild 0
7: </IfModule>
8:
9: <IfModule worker.c>
10: StartServers 3
11: MaxClients 8
12: MinSpareThreads 5
13: MaxSpareThreads 10
14: ThreadsPerChild 25
15: MaxRequestsPerChild 0
16: </IfModule>


The ServerRoot Directive

The ServerRoot directive takes a single argument: a directory path pointing to the directory where the server lives. All relative path references in other directives are relative to the value of ServerRoot. If you compiled Apache from source on Linux/UNIX, as described earlier in this chapter, the default value of ServerRoot is /usr/local/apache2. The ServerRoot for Mac OS X users defaults to /Library/WebServer. If you used the Windows installer, the ServerRoot is C:\Program Files (x86)\Apache Software Foundation\Apache 2.2\.

Per-Directory Configuration Files

Apache uses per-directory configuration files to allow directives to exist outside the main configuration file, httpd.conf. These special files can be placed in the filesystem. Apache processes the content of these files if a document is requested in a directory containing one of these files or any subdirectories under it. The contents of all the applicable per-directory configuration files are merged and processed. For example, if Apache receives a request for the /usr/local/apache2/htdocs/index.html file, it looks for per-directory configuration files in the /, /usr, /usr/local,/usr/local/apache2, and /usr/local/apache2/htdocs directories, in that order.


Caution

Enabling per-directory configuration files has a performance penalty. Apache must perform expensive disk operations looking for these files in every request, even if the files do not exist.


Per-directory configuration files are called .htaccess by default. This is for historical reasons; they originally protected access to directories containing HTML files.

The AccessFileName directive enables you to change the name of the per-directory configuration files from .htaccess to something else. It accepts a list of filenames that Apache will use when looking for per-directory configuration files.

To determine whether you can override a directive in the per-directory configuration file, check whether the Context: field of the directive syntax definition contains .htaccess. Apache directives belong to different groups, as specified in the Override field in the directive syntax description. Possible values for the Override field are as follows:

AuthConfig—Directives controlling authorization

FileInfo—Directives controlling document types

Indexes—Directives controlling directory indexing

Limit—Directives controlling host access

Options—Directives controlling specific directory features

You can control which of these directive groups can appear in per-directory configuration files by using the AllowOverride directive. AllowOverride can also take an All or a None argument. All means that directives belonging to all groups can appear in the configuration file. None disables per-directory files in a directory and any of its subdirectories. Listing 3.4 shows how to disable per-directory configuration files for the server as a whole. This improves performance and is the default Apache configuration.

Listing 3.4 Disabling Per-Directory Configuration Files


1: <Directory />
2: AllowOverride none
3: </Directory>


Apache Log Files

Apache includes two log files by default. The access_log file is for tracking client requests. The error_log file is for recording important events, such as errors or server restarts. These files don’t exist until you start Apache the first time. The names of the files are access.log and error.log in Windows platforms.

The access_log File

When a client requests a file from the server, Apache records several parameters associated with the request, including the IP address of the client, the document requested, the HTTP status code, and the current time. Listing 3.5 shows an example of access_log entries. Chapter 26, “Logging and Monitoring Web Server Activity,” shows you how to modify which parameters are logged.

Listing 3.5 access_log Entries


1: 127.0.0.1 - - [12/Mar/202:08:33:25 -0700] "GET / HTTP/1.1" 200 44
2: 127.0.0.1 - - [12/Mar/2012:08:33:25 -0700] "GET /favicon.ico HTTP/1.1" 404 209


The error_log File

The error_log file includes error messages, startup messages, and any other significant events in the life cycle of the server. This is the first place to look when you have a problem with Apache. Listing 3.6 shows an example of error_log entries.

Listing 3.6 error_log Entries


1: Starting the Apache2.4 service [The Apache2.4 service is running.]
2: Apache/2.4.1 (Unix) configured -- resuming normal operations
3: [Tue Mar 13 08:29:34 2012] [notice] Server built: Mar 12 2012 11:47:22
4: [Tue Mar 13 08:29:34 2012] [notice] Parent: Created child process 3504
5: [Tue Mar 13 08:29:35 2012] [notice] Child 3504: Child process is running
6: [Tue Mar 13 08:29:35 2012] [notice] Child 3504: Acquired the start mutex.


Additional Files

The httpd.pid file contains the process ID of the running Apache server. You can use this number to send signals to Apache manually, as described in the next section. The scoreboard file, the present on Linux/UNIX Apache, is used by the process-based MPMs to communicate with their children. In general, you do not need to worry about these files.

Apache-Related Commands

The Apache distribution includes several executables. This section covers only the server binary and related scripts. Chapter 25, “Restricting Access to Your Applications,” and Chapter 29, “Apache Performance Tuning and Virtual Hosting,” cover additional utilities included with the Apache distribution.

Apache Server Binary

The name of the Apache executable is httpd in Linux/UNIX and Mac OS X, and httpd.exe in Windows. It accepts several command-line options, some of which are described in Table 3.1. You can get a complete listing of options by typing /usr/local/apache2/bin/httpd -h on Linux/UNIX, by typing /usr/sbin/httpd -h on Mac OS X, or by typing httpd.exe -h from a command prompt on Windows.

Table 3.1 Some httpd Options

image

After Apache is running, you can use the kill command on Linux/UNIX and Mac OS X to send signals to the parent Apache process. Signals provide a mechanism to send commands to a process. To send a signal, execute the following command:

# kill -SIGNAL pid

In this syntax, pid is the process ID, and SIGNAL is one of the following:

HUP—Stop the server

USR1 or WINCH—Graceful restart; which signal to use depends on the underlying operating system

SIGHUP—Restart

If you make some changes to the configuration files and you want them to take effect, you must signal Apache that the configuration has changed. You can do this by stopping and starting the server or by sending a restart signal. This tells Apache to reread its configuration.

A normal restart can result in a momentary pause in service. A graceful restart takes a different approach: Each thread or process serving a client continues processing the current request, but when it finishes, it is killed and replaced by a new thread or process with the new configuration. This allows seamless operation of the web server with no downtime.

On Windows, you can signal Apache using the httpd.exe executable. Some commands are listed here:

httpd.exe -k restart—Tells Apache to restart

httpd.exe -k graceful—Tells Apache to do a graceful restart

httpd.exe -k stop—Tells Apache to stop

You can access shortcuts to these commands in the Start menu entries that the Apache installer created. If you installed Apache as a service, you can start or stop Apache by using the Windows service interface: In Control Panel, select Administrative Tasks and then click the Services icon.

Apache Control Script

Although it is possible to control Apache on Linux/UNIX using the httpd binary, it is recommended that you use the apachectl tool. The apachectl support program wraps common functionality in an easy-to-use script. To use apachectl, type the following:

# /usr/local/apache2/bin/apachectl command

In this syntax, command can be stop, start, restart, or graceful. You can also edit the contents of the apachectl script to add extra command-line options. Some OS distributions provide you with additional scripts to control Apache; check the documentation included with your distribution.

Starting Apache for the First Time

Before you start Apache, verify that the minimal set of information is present in the Apache configuration file, httpd.conf. The following sections describe the basic information needed to configure Apache and to start the server.

Check Your Configuration File

You can edit the Apache httpd.conf file with your favorite text editor. In Linux/UNIX and Mac OS X, this probably means vi or emacs. In Windows, you can use Notepad or WordPad. You must remember to save the configuration file in plaintext, which is the only format Apache understands.

You might need to change just two parameters to enable you to start Apache for the first time: the name of the server and the address and port to which it is listening. The name of the server is the one Apache will use when it needs to refer to itself (for example, when redirecting requests).

Apache can usually figure out its server name from the IP address of the machine, but not always. If the server does not have a valid DNS (domain name service) entry, you might need to specify one of the IP addresses of the machine. If the server is not connected to a network (you might want to test Apache on a standalone machine), you can use the value 127.0.0.1, which is the loopback address. The default port value is 80. You might need to change this value if a server is already running in the machine at port 80 or if you do not have administrator permissions—on Linux/UNIX and Mac OS X systems, only the root user can bind to privileged ports (those with port numbers lower than 1024).

You can change both the listening address and the port values with the Listen directive. The Listen directive takes either a port number or an IP address and a port, separated by a colon. If you specify only the port, Apache listens on that port at all available IP addresses in the machine. If you provide an additional IP address, Apache listens at only that address and port combination. For example, Listen 80 tells Apache to listen for requests at all IP addresses on port 80. Listen 10.0.0.1:443 tells Apache to listen at only 10.0.0.1 on port 443.

The ServerName directive enables you to define the name the server will report in any self-referencing URLs. The directive accepts a DNS name and an optional port, separated by a colon. Make sure that ServerName has a valid value. Otherwise, the server will not function properly; for example, it will issue incorrect redirects.

On Linux/UNIX and Mac OS X platforms, you can use the User and Group directives to specify which user and group IDs the server will run as. The nobody user is a good choice for most platforms. However, there are problems in the HP-UX platform with this user ID, so you must create and use a different user ID, such as www.

Starting Apache

To start Apache on Linux/UNIX, change to the directory containing the apachectl script and execute the following command:

# /usr/local/apache2/bin/apachectl start

Mac OS X users can type the following at the prompt:

# /usr/sbin/httpd

To manually start Apache on Windows, click the Start link in the Control Apache Server section, within the Apache HTTP Server 2.2 program group in the Start menu. If you installed Apache as a service, you must start the Apache service instead.

If everything goes well, you can access Apache using a browser. A default installation page displays, such as the one shown in Figure 3.4. If you cannot start the web server or an error page appears instead, consult the “Troubleshooting” section that follows. Make sure that you are accessing Apache in one of the ports specified in the Listen directive—usually port 80 or 8080.

image

Figure 3.4 Apache has been installed.

Troubleshooting

The following subsections describe several common problems that you might encounter the first time you start Apache.

Already an Existing Web Server

If a server is already running on the machine and is listening to the same IP address and port combination, Apache cannot start successfully. You will get an entry in the error log file indicating that Apache cannot bind to the port:

[crit] (48)Address already in use: make_sock: could not bind...[alert] no
listening sockets available, shutting down

To solve this problem, you need to stop the running server or change the Apache configuration to listen on a different port.

No Permission to Bind to Port

You will get an error if you do not have administrator permissions and you try to bind to a privileged port (between 0 and 1024):

[crit] (13)Permission denied: make_sock: could not bind to address 10.0.0.2:80
[alert] no listening sockets available, shutting down

To solve this problem, you must either log on as the administrator before starting Apache or change the port number; 8080 is a commonly used nonprivileged port.

Access Denied

You might not be able to start Apache if you do not have permission to read the configuration files or to write to the log files. You will get an error similar to the following:

(13)Permission denied: httpd: could not open error log file

This problem can arise if the user who built and installed Apache is different from the user trying to run it.

Wrong Group Settings

You can configure Apache to run under a certain username and group. Apache has default values for the running server username and group. Sometimes the default value is not valid and you will get an error containing setgid: unable to set group id.

To solve this problem on Linux/UNIX and Mac OS X, you must change the value of the Group directive in the configuration file to a valid value. Check the /etc/groups file for existing groups.

Summary

This chapter explained different ways of getting an Apache web server installed and running on your Linux/UNIX, Mac OS X, or Windows machine. It covered both binary and source installation and explained the basic build-time options. In addition, you learned the location of the server configuration files and the syntax of the commands used to modify your Apache configuration. You learned about the two main log files: access_log and error_log. You also saw how to start and stop the server using the Apache control scripts or the Apache server binary on Linux/UNIX, Mac OS X, and Windows platforms.

Q&A

Q. How can I start a clean build?

A. If you need to build a new Apache from source and do not want the result of earlier builds to affect the new one, it is always a good idea to run the make clean command. Doing so takes care of cleaning up any existing binaries, intermediate object files, and so on.

Q. Why are per-directory configuration files useful?

A. Although per-directory configuration files have an effect on server performance, they can be useful for delegated administration. Because per-directory configuration files are read every time a request is made, there is no need to restart the server when a change is made to the configuration.

You can allow users of your website to make configuration changes on their own without granting them administrator privileges. In this way, they can password-protect sections of their home pages, for example.

Q. What do you mean by a valid ServerName directive?

A. The DNS system associates IP addresses with domain names. The value of ServerName is returned when the server generates a URL. If you are using a certain domain name, you must make sure that it is included in your DNS system and will be available to clients visiting your site.

Workshop

The workshop is designed to help you review what you’ve learned and begin putting your knowledge into practice.

Quiz

1. How can you specify the location where you want to install Apache?

2. What is the main difference between the <Location> and <Directory> sections?

3. What is the difference between a restart and a graceful restart?

Answers

1. Linux/UNIX users can use the --prefix option of the configure script. If an existing installation is present at that location, the configuration files are preserved, but the binaries are replaced. On Windows, this location is set in the Installation Wizard.

2. Directory sections refer to filesystem objects; Location sections refer to elements in the address bar of the web page.

3. During a normal restart, the server is stopped and then started, causing some requests to be lost. A graceful restart allows Apache children to continue to serve their current requests until they can be replaced with children running the new configuration.

Activities

1. Practice the various types of server shutdown and restart procedures.

2. Make some configuration changes, such as different port assignments and ServerName changes.