4.2. Installing and Configuring Apache

All recent versions of Mac OS X come with Apache already installed. Most Linux distributions include Apache. However, you might want to install Apache yourself to install a newer version or to install with different options. Windows doesn't come with Apache installed. You must install it yourself.

To check whether Apache is already installed, type a URL, such as localhost, into a browser address window, as described in the section "Testing Your Web Server," earlier in this chapter. If Apache isn't already installed, an error message displays.

4.2.1. Obtaining Apache

Apache is an open source Web server that you can download for free.

4.2.1.1. Selecting a version of Apache

Apache is currently available in three versions: Apache 1.3, Apache 2.0, and Apache 2.2. All three versions are supported and upgraded. The PHP software runs with all three versions, but some other software related to PHP might have problems with Apache 2.0 or 2.2. On Windows, Apache 2.0 and 2.2 aren't supported on Windows 9x installations; they require Windows NT, 2000, XP, or Vista.

Apache 2.0 changed considerably from Apache 1.3; Apache 2.2 changed from Apache 2.0. Some third-party modules might not work correctly on all three versions. Third-party modules that run on 1.3 won't work correctly with Apache 2.0, and modules that work on Apache 2.0 might not work correctly with Apache 2.2. Therefore, only modules that have been modified for Apache 2.0 or 2.2 can run on Apache 2.0 or 2.2.

On the PHP Web site, the recommended setup at present is to use PHP 4.3.0 or later with the most recent version of Apache 2.0. Check the Web page for the current status of PHP with Apache versions at

www.php.net/manual/en/install.windows.apache2.php

At the time of this writing, the current releases are Apache 2.2.4, 2.0.59, and 1.3.37.

Try to install the most current release of the Apache version you choose 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.0 or 2.2.

4.2.1.2. Downloading from the Apache Web site

Apache for all operating systems is available on the official Apache Web site. You can download source code to compile on your operating system. Compiling and installing source code isn't difficult on Linux and Mac, but it requires expert knowledge and software on Windows.

Binary files — compiled, ready-to-run files that just need to be copied to the correct location — are available for Windows.

To obtain Apache from the Apache Web site, go to http://httpd.apache.org. Scroll down to the section for the Apache version you want to download and click Download. A download page with links to download the current versions displays.

4.2.1.3. Obtaining Apache for Windows

The Windows binary file is available with an installer, which will install, configure, and start Apache. On the Apache Web site download page, find the section for the Apache version you want. Click the link for the Win32 Binary (MSI Installer) to download the installer file.

NOTE

Although Win32 source code is also available to download in a Zip file, compiling and installing Apache from source code is difficult and should be attempted only by advanced users. It requires advanced knowledge and special software.

4.2.1.4. Obtaining Apache for Linux

Most recent versions of Linux include Apache. If you need to install Apache or upgrade to a more recent version, most Linux distributions provide software on their Web site that you can download and install on your specific Linux system. In addition, most Linux systems provide a utility specifically for downloading and installing software. For instance, Fedora provides the yum utility that downloads and installs software from the Fedora Web site. See the documentation for your Linux distribution for information on how to download and install software on your Linux distribution.

In a few cases, you might need to install Apache manually. The software provided by the Web site might not be the most recent or might not be configured to your needs. To install manually, you need to download the source code from the Apache Web site at http://httpd.apache.org.

You can easily compile and install Apache from the source code. This process isn't as technical and daunting as it sounds. Instructions for installing Apache from source code are provided in the "Installing Apache from source code on Linux or Mac" section, later in this chapter.

4.2.1.5. Obtaining Apache for Mac

Apache comes already installed on most recent versions of Mac OS X. If you test Apache by typing localhost in your browser address window and it doesn't display a Web page, it's probably installed but not started. To find out how to start Apache, see the section "Installing Apache on Mac," later in this chapter.

If you need to install Apache because it's not installed or an old version is installed, download the source files from the Apache Web site to compile and install on your Mac. Instructions for installing Apache from the source code are provided in the "Installing Apache from source code on Linux and Mac" section, later in this chapter.

4.2.1.6. Obtaining all-in-one installation kits

You can obtain some kits that contain and install PHP, MySQL, and Apache in one procedure. These kits can greatly simplify the installation process. However, the software provided might not include the features and extensions that you need.

XAMPP is a popular all-in-one installation kit that contains Apache, PHP, and MySQL. XAMPP has stable versions available for Windows and for several versions of Linux. In addition, versions of XAMPP are available for Mac and Solaris, but these versions are currently new and aren't as well tested and developed. XAMPP is available at www.apachefriends.org/en/xampp.html. Instructions for installing your software using XAMPP are provided in Chapter 5 in this minibook.

WAMP5 is a popular installation kit for Windows that provides recent versions of Apache 2.2, PHP 5, and MySQL 5. It also installs phpMyAdmin, a utility for managing your MySQL databases. The WAMP5 Web site states that it is compatible with Vista. WAMP5 doesn't run on Windows 98/Me. WAMP5 is available at www.en.wampserver.com.

MAMP is an installation kit for Mac that installs Apache, PHP, and MySQL for Mac OS X. This free package installs a local server environment on your Mac PowerBook or iMac. MAMP was created primarily as a PHP development environment for your local computer and should not be used as a production server for the Internet. You can obtain MAMP at www.mamp.info.

4.2.1.7. Verifying a downloaded file

The Apache Web site provides methods to verify the software after you download it, as a security precaution to make sure that the file hasn't been altered by bad guys. You can use the MD5 method or the PGP method for verifying the file. This book provides instructions for the MD5 method.

Basically, the same process is used to verify the file for PHP, MySQL, and Apache. You can find instructions for verifying the downloaded file in Chapter 2 of this minibook. On the Apache Web site, click the MD5 link to see the MD5 signature discussed in the instructions.

4.2.2. Installing Apache

The following subsections describe installing Apache on Windows, Mac, and Linux.

4.2.2.1. Installing Apache on Windows

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

You can't install Apache with the following directions if IIS (Internet Information Services) is already running on port 80. If IIS is running, you will find the IIS console at StartControl PanelAdministrative ToolsInternet Services Manager. If you don't find this menu item, IIS isn't installed. If IIS is already running, you must shut it down before installing Apache or install Apache on a different port.

To install Apache, follow these steps:

  1. Double-click the file you downloaded.

    The file is named apache_, followed by the version number and win32-x86-no_ssl.msi. For instance, apache_2.0.59-win32-x86-no_ssl.msi.

    Note: In Vista, you might need to right-click the file and choose Run as Administrator.

    The Apache installation wizard begins, and a welcome screen appears.

  2. Click Next.

    The license agreement is displayed.

  3. Select I Accept the Terms in the License Agreement 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.

  4. Click Next.

    A screen is displayed asking for information.

  5. 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 and plan to access it only from the 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 and plan to access it only from the machine where it's installed, you can enter localhost.

    • E-Mail Address: Type the e-mail address where you want to receive e-mail messages about the Web server, such as [email protected].

    • 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 to run Apache as a service.

    The Installation Type screen is displayed.

  6. Select an installation type and then click Next.

    In most cases, you should select Complete. Only advanced users who understand Apache well should select Custom. If you select Custom, the screens will be somewhat different than the screens described below. A screen showing where Apache will be installed is displayed.

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

    You see the default installation directory for Apache, usually C:Program FilesApache 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. The screen that appears says the wizard is ready to install Apache.

  8. 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. When the installation is complete, a screen appears, saying that the wizard has successfully completed the installation.

  9. 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/Vista, it is installed by default 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 section "Starting and stopping Apache," later in this chapter, for more information.

4.2.2.2. Installing Apache on a Mac

Apache is installed on all recent versions of Mac OS X, but it might not be started. To start Apache, choose Apple MenuSystem PreferencesSharing. On the File and Web panel, find the section for Web sharing. Click the Start button to turn Web sharing On, which starts the Apache Web server.

If you need to install Apache yourself for some reason, you can install Apache from source code, as described in the next section.

4.2.2.3. Installing Apache from source code on Linux and Mac

You can install Apache on Linux, Unix, and Mac from source code. You download the source code and compile it. To install Apache from source code, follow these steps:

  1. Change to the directory where you downloaded the file.

    The downloaded file is named apache-, followed by the version name and tar.gz. This file is called a tarball because it contains many files compressed by a program called tar.

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

    gnutar -xzf / httpd-2.0.59.tar.gz          (Mac)
    
    gunzip -c httpd-2.0.59.tar.gz | tar -xf -  (Linux)

    After unpacking the tarball, you see a directory called httpd_2.0.59. 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_2.0.59).

  4. Type the configure command.

    The configure command consists of ./configure followed by all the necessary options. To use Apache with PHP as a module, use the appropriate configure command as follows: For Linux or Unix, use

    ./configure --enable-so

    For Mac, use

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

    You can use other options if you want. One of the more important installation options you might 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 available options by typing the following line:

    ./configure --help

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

  5. Type make to build the Apache server.

    The make command might take a few minutes to run. It displays messages while it's running, with occasional pauses for a process to finish running.

  6. Type the following command to install Apache:

    For Linux or Unix, type

    make install

    For Mac, type

    sudo make install.

  7. Start the Apache Web server.

    See the next section 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.

4.2.3. Starting and stopping Apache

You might need to start Apache when you install it. Or, you might not. It might already be started. However, whenever you change your Apache or PHP configuration settings, you need to restart Apache before the new settings go into effect.

4.2.3.1. Starting and stopping Apache on Windows

When you install Apache on Windows NT, 2000, XP, or Vista, it's automatically installed as a service and started. It's ready to use. On Windows 95, 98, and Me, you have to start Apache manually, using the menu.

When you install Apache, it creates menu items for stopping and starting it. To find this menu, choose StartProgramsApache HTTP ServerControl Apache Server. The menu has the following items:

  • Start: Used to start Apache when it isn't 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's running. If you click this item when Apache isn't running, you see an error message saying that Apache isn't running.

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

4.2.3.2. Starting Apache on Linux, Unix, and Mac

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

The apachectl script starts the Apache server, which then runs in the background, listening for HTTP requests. By default, the compiled Apache server is named httpd and is stored in the same directory as the apachectl script, unless you changed the name or location during installation. 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 following command:

/usr/local/apache/bin/apachectl start          (Linux/Unix)
sudo /usr/local/apache/bin/apachectl start     (Mac)

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 location, you might 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.

4.2.3.3. Restarting Apache on Linux, Unix, and Mac

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 the "Starting Apache on Linux, Unix, and Mac" section. 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       (Linux)
sudo /usr/local/apache2/bin/apachectl restart  (Mac)

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.


4.2.3.4. Stopping Apache on Linux, Unix, and Mac

To stop Apache, use the following command:

/usr/local/apache/bin/apachectl stop
sudo /usr/local/apache/bin/apachectl stop

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

ps -A

The output from ps shouldn't include any httpd processes.

4.2.4. Getting information from Apache

Sometimes you want to know information about your Apache installation, such as the installed version. You can get this information from Apache.

4.2.4.1. Getting Apache information on Windows

You can get information from Apache by opening a Command Prompt window (StartProgramsAccessoriesCommand Prompt), changing to the bin directory in the directory where Apache is installed (such as, cd C:Program FilesApache GroupApache2in), 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

4.2.4.2. Getting Apache information on Linux, Unix, and Mac

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 where the httpd server resides and typing one of the following:

httpd -v
./httpd -v

You can find out what modules are installed with Apache by typing

httpd -l

To see all the options that are available, type

httpd -h

4.2.5. 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 directory where Apache is installed.

4.2.5.1. Changing settings

Apache behaves according to commands, called directives, in the configuration file (which is a plain text file). You can change some of Apache's behavior by editing the configuration file and restarting Apache so that it reads the new directives.

In most cases, the default settings in the configuration file allow Apache to start and run on your system. However, you might need to change the settings in some cases, such as the following:

  • Installing PHP: If you install PHP, you need to configure Apache to recognize PHP programs. How to change the Apache configuration for PHP is described in Chapter 2 of this minibook.

  • 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 httpd.conf file. On Windows, you can access this file through the menu at StartProgramsApache HTTPD ServerConfigure Apache ServerEdit the Apache httpd.conf File. When you click this menu item, the httpd.conf file opens in Notepad.

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

When adding or changing filenames and paths, 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"

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.


4.2.5.2. 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 filename and path 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

4.2.5.3. Changing the port number

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

Listen 80

With Apache 2.0 and 2.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

NOTE

Always restart Apache after you change any directives.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.232.128