Tuesday, August 11, 2009

Installing and Configuring Apache

pache is an open-source Web server. A Web server delivers the files on the Web site to the visitor who wants to see the Web pages.

In most cases, you want to get Apache from the Web and install it yourself. Although Apache may already be installed on your computer (because most Linux distributions and Mac OS X include Apache), it’s unlikely to be the latest version. Or Apache may not have been installed with the options you need. Because of this, you’re often better off installing Apache yourself.

Selecting a Version of Apache

Apache is currently available in two versions: Apache 1.3 and Apache 2. Apache 2 is the newer version, released in April 2002. Apache 2 is not sup­ ported on Windows 9x installations; it requires Windows NT/2000/XP.

Many Linux distributions come with Apache 2 installed. However, as of this writing, the PHP Web site cautions against using Apache 2 with PHP in a pro­ duction environment. Check the Web page for the current status of PHP with Apache 2 at www.php.net/manual/en/install.apache2.php#install. apache2.unix.

At this time, the current versions are

Apache 2.0.47

Apache 1.3.28

Try to install the most current Apache version so that your Apache server includes all the latest security and bug fixes. New features are no longer being added to Apache 1.3, but bugs are still being fixed and security issues are being addressed. New versions of Apache 1.3 continue to be released but on a less frequent basis than for Apache 2.

Installing Apache

Apache can be downloaded and installed on your Web server for free. It’s available for almost every operating system, including Windows, Linux, many flavors of Unix, and Mac.

On Linux/Unix

To install Apache on Linux and Unix, you download the source code, compile it, and install it. This sounds daunting but is much easier than it sounds.

Before installing
Before installing Apache, check the following requirements:

Disk space: You may need as much as 50MB of disk space while installing. Apache will probably use 10MB after installation, although the amount varies depending on the options used and modules installed.

C compiler: Your computer has an ANSI-compliant C compiler installed.
GNU C (gcc) is a good choice.

Installing
To install Apache from source files, follow these steps:

1. Point your Web browser to httpd.apache.org, the Apache home
page.

2. Click the From a Mirror link under Download on the left side of
the page.

3. Scroll down to the Mirror section.

A specific mirror is selected for you. If you don’t want to use this mirror, select another. Or if you have problems downloading from this mirror, return to this page and select another.

4. Scroll further down the same page to the section for Apache 2 or for Apache 1.3, whichever you want to install. Locate and highlight the file you want to download.

For instance, at this time, the most recent version of Apache 1.3 for
Linux is apache-1.3.28.tar.gz.

5. Click the latest version to download it.

6. Select the option to save the file.

7. Navigate to where you want to save the source code (for example,
/usr/src). Then click Save.

8. After the download, change to the download directory (for example,
cd-/usr/src).

You see a file named apache-, followed by the version name and tar.gz. This file is called a tarball because it contains many files com­ pressed into the tarball file by a program called tar.

Be sure you’re using an account that has permission to write into
/usr/src, such as root.

9. Verify the downloaded file to be sure it hasn’t been tampered with. To verify the file, follow these steps:

a. Download two files from www.apache.org/dist/httpd/: One file to download is named KEYS. The second file is named with the same file name, including version number, as the source, but the filename ends in .asc.

b. Type one of the following lines, depending on which version of PGP
is installed on your computer:

pgp <KEYS
gpg --import KEYS

Several lines of output are displayed.

c. Type one of the following lines, with the correct version number.

pgp apache-1.3.28.tar.gz.asc
gpg --verify apache-1.3.28.tar.gz.asc

You should see something similar to the following:

Good signature from user “Sander Striker”

This is what you are looking for. Several messages will probably be displayed, but the preceding message is the important one. You might also see a message stating that the relationship between the key and the signer of the key cannot be verified. This is okay.

If you don’t get a message that the signature is good, the file might have been tempered with and may be dangerous. In this case, repeat the process starting with Step 1 and select a different mirror to download from.

10. Unpack the tarball.

The command to unpack the tarball for version 1.3.28 is the following:

gunzip -c apache-1.3.28.tar.gz | tar -xf –

A new directory called apache-1.3.28 is created with several subdirec­
tories containing all the files that you just unpacked from the tarball.

11. Change to the new directory that was created when you unpacked the tarball.

For example, you can use a command like the following:

cd apache-1.3.28

12. Type the configure command.

The configure command consists of ./configure followed by all the necessary options. If you can use all the default options, you can use configure without any options. However, to use Apache with PHP as a module, use the configure command as follows:

./configure --enable-module=so

One of the more important installation options you may want to use is prefix, which sets a different location where you want Apache to be installed. By default, Apache is installed at /usr/local/apache or usr/local/apache2. You can change the installation location with the following line:

./configure -–prefix=/software/apache

You can see a list of all the available options by typing the following line:

./configure -–help

This script may take a while to finish running. As it runs, it displays output. When the script is done, the system prompt is displayed. If configure encounters a problem, it displays a descriptive error message.

13. Type the following command:

make

This command builds the Apache server. It may take several minutes to finish running. As it runs, it displays messages telling you what it’s doing. There may be occasional longer pauses as it completes some action. When it’s finished, it returns to the system prompt. If it has a problem, it displays a descriptive error message.

14. Type the following command:

make install

This command installs the Apache software in the proper locations, based on the configure command you used in Step 11.

15. Start the Apache Web server.

See the following section, “Starting and Stopping Apache,” for details.

16. Type the URL for your Web site (for example, www.mysite.com or
localhost) into a browser to test Apache.

If all goes well, you see the Apache message telling you that Apache is working.

Starting and stopping Apache
A script named apachectl is available to control the server. By default, the script is stored in a subdirectory called bin in the directory where Apache is installed. Some Linux distributions may put it in another directory.

The script requires a keyword. The most common keywords are start, stop, and restart. The general syntax is as follows:

path/apachectl keyword

For example, if Apache was installed in the default directory, type the follow­
ing line to start Apache:

/usr/local/apache/bin/apachectl start

Starting Apache
The apachectl script starts the Apache server, which then runs in the back­ ground, listening for HTTP requests. By default, the compiled Apache server is named httpd and is stored in the same directory as the apachectl script, although you can change the name and location when you install Apache. The
apachectl script serves as an interface to the compiled server, called httpd.

You can run the httpd server directly, but it’s better to use apachectl as an interface. The apachect1 script manages and checks data that httpd commands require. Use the apachectl script to start Apache with the fol­ lowing command:

/usr/local/apache/bin/apachectl start

The apachectl script contains a line that runs httpd. By default, apachectl
looks for httpd in the default location — /usr/local/apache/bin or
/usr/local/apache2/bin. If you installed Apache in a nonstandard loca­ tion, you may need to edit apachectl to use the correct path. Open apachectl and then search for the following line:

HTTPD=’/usr/local/apache2/bin/httpd’

Change the path to the location where you installed httpd. For example, the new line might be this:

HTTPD=’/usr/mystuff/bin/httpd’

After you start Apache, you can check whether Apache is running by looking at the processes on your computer. Type the following command to display a list of the processes that are running:

ps –A

If Apache is running, the list of processes includes some httpd processes.

Getting information from Apache
You can use options with the httpd server to obtain information about Apache. For instance, you can find out what version of Apache is installed by changing to the directory with httpd and typing

httpd -v

Or, probably, ./httpd –v. You can find out what modules are installed with
Apache by typing the following:

httpd -l

To see all the options that are available, type the following:

httpd -h

Restarting Apache
Whenever you change the configuration file, the new directives take effect the next time Apache starts. If Apache is shut down when you make the changes, you can start Apache as described earlier in “Starting Apache.” However, if Apache is running, you can’t use start to restart it. Using start results in an error message saying that Apache is already running. You can use the following command to restart Apache when it’s currently running:

/usr/local/apache2/bin/apachectl restart

Although the restart command usually works, sometimes it doesn’t. If you restart Apache and the new settings don’t seem to be in effect, try stopping Apache and starting it again. Sometimes this solves the problem.

Stopping Apache
To stop Apache, use the following command:

/usr/local/apache/bin/apachectl stop

You can check to see that Apache is stopped by checking the processes that are running on your computer by using the following command:

ps –A

The output from ps should not include any httpd processes.

On Windows

You can install Apache on almost any version of Windows, although Windows
NT/2000/XP are preferred.

Installing
To install Apache, follow these steps:

1. Point your Web browser to httpd.apache.org, the Apache home
page.

2. Click the From a Mirror link under Download on the left side of
the page.

3. Scroll down to the Mirror section.

A specific mirror is selected for you. If you don’t want to use this mirror, select another. Or if you have problems downloading from this mirror, return to this page and select another.

4. Scroll further down the same page to the section for Apache 2 or for Apache 1.3, whichever you want to install. Locate and highlight the line for Win 32 Binary (MSI installer).

For instance, at this time, the most recent version of Apache 1.3 for
Windows is apache_1.3.28.

5. Click the filename to download it.

6. Select the option to save the file.

7. Navigate to where you want to save the installer. This should be a tem­
porary directory, such as a download directory. Then click Save.

After the download is complete, you see a file in the download location containing all the files needed. The file is named apache, followed by the version number and win32-x86-no_src.msi. For the current version, the file is named apache_1.3.28-win32-x86-no_src.msi.

8. Double-click the downloaded file.

The Apache installation wizard begins, and a welcome screen is displayed.

9. Click Next.

The license agreement is displayed.

10. Select I Accept the Terms in the License Agreements and then
click Next.

If you don’t accept the terms, you can’t install the software. A screen of information about Apache is displayed.
11. Click Next.

A screen is displayed asking for information.

12. Enter the requested information and then click Next.

The information requested is

• Domain Name: Type your domain name, such as MyFineCompany.com. If you’re installing Apache for testing pur­ poses and plan only to access it from the same machine where it’s installed, you can enter localhost.

• Server Name: Type the name of the server where you’re installing Apache, such as www.MyFineCompany.com or s1.mycompany.com. If you’re installing Apache for testing pur­ poses and plan only to access it from the same machine where it’s installed, you can enter localhost.

• E-mail Address: Type the e-mail address that you want to receive e-mail message about the Web server, such as WebServer@ MyFineCompany.com.

• Run Mode: Select whether you want Apache to run as a service, starting automatically when the computer boots up, or whether you want to start Apache manually when you want to use it.
In most cases, you want the first choice — to run Apache as a service.

The installation type screen is displayed.

13. Select an installation type and click Next.

In most cases, you should select Complete. Only advanced users who understand Apache well should select Custom.

A screen showing where Apache is to be installed is displayed.

14. Select the directory where you want Apache installed and click Next.

You see the default installation directory for Apache, usually C:\Program Files\Apache Group. If this is okay, click Next. If you want Apache installed in a different directory, click Change and select a different directory, click OK, and click Next.

A screen is displayed that says the wizard is ready to install Apache.

15. Click Install.

If you need to, you can go back and change any of the information you entered before proceeding with the installation.

A screen displays the progress while Apache is being installed. When the installation is complete, a screen is displayed saying that the wizard has successfully completed the installation.

16. Click Finish to exit the installation wizard.

Apache is installed on your computer based on your operating system. If you install it on Windows NT/2000/XP, it is automatically installed as a service that automatically starts when your computer starts. If you install it on Windows 95/98/Me, you need to start it manually or set it up so that it starts

automatically when your computer boots. See the next section, “Starting and stopping Apache,” for more information.

Starting and stopping Apache
When you install Apache on Windows NT/2000/XP, it’s automatically installed as a service and started. It’s ready to use. You can test it by typing your Web site name (or localhost) into your browser window. You see a welcome Web page that reads, “If you can see this, it means that the installation of the Apache Web server software on this system was successful.” On Windows
95/98/Me, you have to start Apache manually, using the menu.

Apache installs menu items for stopping and starting Apache during installa­ tion. You can find this menu at Start➪Programs➪Apache HTTP Server➪ Control Apache Server.

The menu you use to start and stop Apache provides the following menu items:

Start: Used to start Apache when it is not running. If you click this item when Apache is running, you see an error message saying that Apache has already been started.

Stop: Used to stop Apache when it is running. If you click this item when Apache is not running, you see an error message saying that Apache is not running.

Restart: Used to restart Apache when it is running. If you make changes to Apache’s configuration, you need to restart Apache before the changes become effective.

Getting information from Apache
Sometimes you want to know information about your Apache installation, such as the version that’s installed. You can get this information from Apache by opening a command prompt window (Start➪Programs➪Accessories➪ Command Prompt), changing to the directory where Apache is installed
(such as, cd C:\Apache), and accessing Apache with options. For example, to find out which version of Apache is installed, type the following in the command prompt window:

Apache –v

To find out what modules are compiled into Apache, type

Apache –l

You can also start and stop Apache directly, as follows:

Apache -k start
Apache -k stop

You can see all the options available by typing the following:

Apache -h

On Mac

Installing Apache on the Mac is very similar to installing Apache on Unix/Linux. You download the source code and compile it. To install Apache on the Mac, follow these steps:

1. Download the source code, save it in a directory, and change to the directory where the downloaded file is saved.

Follow Steps 1–8 of the directions for Unix/Linux.

You will see a file named httpd, followed by the version name and tar.gz, such as, httpd-1.3.28.tar.gz. This file is the tarball — a single file that contains all the files needed, compressed into one file.

2. Unpack the tarball by using a command similar to the following:

gnutar -xzf /httpd_1.3.28.tar.gz

After unpacking the tarball, you see a directory called httpd_1.3.28. This directory contains several subdirectories and many files.

3. Use a cd command to change to the new directory created when you unpacked the tarball (for example, cd httpd_1.3.28).

4. Type the following command:

./configure --enable-module=most --enable-shared=max

This command may take some time to run.

5. Type the following command to build the Apache server:

make

This command may take a few minutes to run. It displays messages while it is running, with occasional pauses for a process to finish running.

6. Type the following command to install Apache:

sudo make install

7. Start the Apache Web server.

See the section, “Starting and Stopping Apache,” under Unix/Linux for details.

8. Type the URL for your Web site (for example, www.mysite.com or
localhost) into a browser to test Apache.

If all goes well, you see a Web page telling you that Apache is working.

Configuring Apache

When Apache starts, it reads information from a configuration file. If Apache can’t read the configuration file, it can’t start. Unless you tell Apache to use a different configuration file, it looks for the file conf/httpd.conf in the direc­ tory where Apache is installed.

Changing settings

Apache behaves according to commands, called directives, in the configura­ tion file. You can change some of Apache’s behavior by editing the configura­ tion file and restarting Apache so that it reads the new directives.

The configuration file is a text file containing commands called directives. Apache behaves according to the directives in this file. In most cases, the default settings allow Apache to start and run on your system. However, you may need to change the settings in some cases. Some reasons you might want to change the settings are

Installing PHP: If you install PHP, you need to configure Apache to rec­ ognize PHP programs. How to change the Apache configuration for PHP is described in Appendix B.

Changing your Web space: Apache looks for Web page files in a specific directory and its subdirectories, often called your Web space. You can change the location of your Web space.

Changing the port where Apache listens: By default, Apache listens for file requests on port 80. You can configure Apache to listen on a different port.

To change any settings, edit the file httpd.conf. On Windows, you can access this file through the menu at Start➪Programs➪Apache HTTPD Server➪Configure Apache Server➪Edit the Apache httpd.conf File. When you click this menu item, the httpd.conf file is opened in Notepad.

The httpd.conf file has comments (beginning with #) that describe the directives, but you should be sure you understand their function before changing any. All directives are documented on the Apache Web site.

When adding or change file path/names, use forward slashes, even when the directory is on Windows. Apache can figure it out. Also, path/names don’t need to be in quotes unless they include special characters. A colon (:) is a special character; the underscore (_) and hyphen (-) are not. For instance, to indicate a Windows directory, you would use something like the following:

“c:/temp/mydir”

Remember to restart Apache after you change any settings. The settings don’t go into effect until Apache is restarted. Sometimes using the restart command doesn’t work to change the settings. If the new settings don’t seem to be in effect, try stopping the server with stop and then starting it with start.

Changing the location of your Web space

By default, Apache looks for your Web page files in the subdirectory htdocs in the directory where Apache is installed. You can change this with the DocumentRoot directive. Look for the line that begins with DocumentRoot, such as the following:

DocumentRoot “C:/Program Files/Apache Group/Apache/htdocs”

Change the file path/name to the location where you want to store your Web page files. Don’t include a forward slash ( / ) on the end of the directory path. For example, the following might be your new directive:

DocumentRoot /usr/mysrver/Apache2/webpages

Changing the port number

By default, Apache listens on port 80. You might want to change this, for instance, if you are setting up a second Apache server for testing purposes. The port is set by using the Listen directive as follows:

Listen 80

With Apache 2, the Listen directive is required. If no Listen directive is included, Apache 2 won’t start.

You can change the port number as follows:

Listen 8080

Remember to restart Apache after you change any directives.

0 comments: