Installing a Web Server - Getting Started with PHP & MySQL - PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies (2013)

PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies (2013)

Book I: Getting Started with PHP & MySQL

Chapter 2: Installing a Web Server

In This Chapter

arrow.png Testing for a web server

arrow.png Obtaining Apache

arrow.png Installing Apache on Windows, Linux, and Mac

arrow.png Using Apache

arrow.png Configuring Apache

You might have the idea that this chapter is all about the web server Apache. Well, you’re right. In this chapter, you download and install Apache. If you’ll be using a hosted website or a company website and placing your files on someone else’s server, then you don’t need to install a web server at all.

The chapter focuses on httpd from Apache because it’s free and the most popular web server used on the Internet. Other web servers are available. Microsoft has Internet Information Services (IIS) and also includes a development web server with its Visual Studio development application. Another popular web server is called nginx. Apache and its wide support across different types of computers is so popular that we focus solely on Apache in this book.

Windows doesn’t come with Apache installed. You must install it yourself. Most Linux distributions include Apache or have it easily available through their package management software. All recent versions of Mac OS X come with Apache already installed. However, you might want to install Apache yourself for a newer version or to install with different options.

This chapter guides you in finding out if Apache is already installed on your computer; finding, downloading, and installing the software; starting and stopping Apache; getting information about the installation; and configuring Apache so that it behaves as you need it to.

Testing Your Web Server

You can test whether a web server is installed on your computer by viewing a web page in your browser. Open your browser and type http://localhost in the browser address bar. If your web server is installed and running, a web page displays. For instance, the Apache Welcome screen displays the following text:

If you can see this, it means that the installation of the

Apache web server software on this system was successful. You

may now add content to this directory and replace this page.

warning_bomb.eps You can’t test your web server by choosing File⇒Open or Open File in your browser. This method of viewing a web page file doesn’t go through the web server. You must type the URL into your browser’s address bar to test the server.

If no web server is running on your machine, an error message is displayed, such as one of the following:

Unable to connect

The page cannot be displayed

Even if you have no web server running, a web server might be installed on your computer but not started. If so, you need only start the web server. For instance, Apache is installed on all recent Mac computers, but it might need to be started. See the instructions for obtaining and installing Apache later in this chapter.

Obtaining Apache

Apache is an open source web server that you can download for free. The sections that follow give you the preliminary info you need — based on the operating system you’re using — to decide how to begin selection and installation of your web server software. Be aware, also, that an all-in-one installation kit might work for your purposes. We provide information on that option as well in this section.

Selecting a version of Apache

Apache is currently available in three versions: Apache 2.0, Apache 2.2, and Apache 2.4. All 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.4. On Windows, Apache 2.4 is currently not available.

Like any software, Apache evolves as new versions come out. Some third-party modules might not work correctly on all three versions. Because PHP is a module, you should check the web page for the current status of PHP with Apache versions at

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.

Downloading from the Apache website

Apache for all operating systems is available on the official Apache website. 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 website, go to Scroll down to the section for the Apache version you want to download click the appropriate link for the version you want to download. A download page with links to download the current versions displays.

Obtaining Apache for Windows

The Windows binary file is available with an installer, which will install, configure, and start Apache. On the Apache website 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.

warning_bomb.eps 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.

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 either on their website that you can download or through their package management software that will 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 website. See the documentation for your Linux distribution for information on how to download and install software on your Linux system.

In a few cases, you might need to install Apache manually. The software provided by the website 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 website at

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.

Obtaining Apache for Mac

Apache comes already installed on most recent versions of Mac OS X. If you test Apache by typing http://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 a Mac, later in this chapter.

If you need to install Apache because it isn’t installed or an old version is installed, download the source files from the Apache website 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.

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. XAMPP is available at Instructions for installing your software using XAMPP are provided in Chapter 5 in this minibook.

WAMPServer is a popular installation kit for Windows that provides recent versions of Apache, PHP, and MySQL. It also installs phpMyAdmin, a utility for managing your MySQL databases. WAMPServer is available at

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. You can obtain MAMP at

Verifying a downloaded file

The Apache website 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 3 of this minibook. On the Apache website, click the MD5 link to see the MD5 signature discussed in the instructions.

Installing Apache

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

Installing Apache on Windows

You can install Apache on almost any version of Windows.

You can’t install Apache with the following directions if Internet Information Services (IIS) is already running on port 80. If IIS is running, you will find the IIS console at Start⇒Control Panel⇒Administrative Tools⇒Internet 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 after you’re sure that IIS isn’t running, 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, httpd-2.2.22-win32-x86-no_ssl.msi.

Note: 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 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 or 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

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 in the following text. 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 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. 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 later versions of Microsoft Windows, it is installed by default as a service that automatically starts when your computer starts. If you install it on an older version of Windows, such as Windows 95/98/Me, then 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.

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 Menu⇒System Preferences⇒Sharing. On the Service pane, find the section for web sharing. Click the check box 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.

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 typically named httpd-, 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:

For Linux:

gunzip -c httpd-2.2.22.tar.gz | tar -xf –

For Mac:

gnutar -xzf httpd-2.2.22.tar.gz

After unpacking the tarball, you see a directory called httpd_2.2.22. This directory contains several subdirectories and many files. Note that the version number will be different by the time you read this.

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

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

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 website (for example, or localhost) into a browser to test Apache.

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

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.

Starting and stopping Apache on Windows

When you install Apache on Windows, it’s usually automatically installed as a service and started. It’s ready to use. However, 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 Start⇒Programs⇒Apache HTTP Server⇒Control Apache Server. The menu has the following items:

check Start: This option starts 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.

check Stop: Stops 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.

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

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:

check For Linux:

/usr/local/apache/bin/apachectl start

check For Mac:

sudo /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 location, you might need to edit apachectl to use the correct path. Openapachectl and then search for the following line:


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


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.

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:

check For Linux:

/usr/local/apache2/bin/apachectl restart

check For Mac:

sudo /usr/local/apache2/bin/apachectl restart

tip.eps 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 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.

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 by using the applicable procedure that follows.

Getting Apache information on Windows

You can get information from Apache by opening a Command Prompt window (Start⇒Programs⇒Accessories⇒Command Prompt), changing to the bin directory in the directory where Apache is installed (such as cd C:\Program Files\Apache Group\Apache2\bin), 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

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

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. Keep reading for details on how to configure Apache so that it starts without a hitch.

remember.eps Always restart Apache after you change any directives.

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:

check 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 3 of this minibook.

check Changing your Document Root: Apache looks for web page files in a specific directory and its subdirectories, called your Document Root. You can change the location of your Document Root. Read the next section for instructions.

check Changing the port on which Apache listens: By default, Apache listens for file requests on port 80. You can configure Apache to listen on a different port. See the upcoming Changing the port number section for details on how to do that.

To change any settings, edit the httpd.conf file using a text editor. 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 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 website.

remember.eps Here are some conventions to consider when you’re changing Apache settings:

check Filenames and paths: When adding or changing filenames and paths, use forward slashes, even when the directory is on Windows. Apache can figure it out.

check Path names: You don’t need to put path names in quotes, unless they include special characters.

check 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:


warning_bomb.eps 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 Document Root

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

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

warning_bomb.eps 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