Vous êtes sur la page 1sur 6

IS2955 – Web Engineering

University of Pittsburgh
PHP and Server-Side Technologies

Installing Apache and PHP on Windows XP.


Purpose: To learn how to install the Apache 2.2 web server and PHP 5 scripting
language on a Windows XP machine. It is not meant as a tutorial for building a
production-quality web server, nor is it meant as a comprehensive guide for configuring
either Apache or PHP. Please consult the documentation on the Apache and PHP
websites for more information.

Audience: This brief tutorial is meant for beginners who would like to use their PC as a
local web server and development/testing platform.

1. Getting Started
First, download the Apache 2.2 Windows installer from the Apache website:
• Go to http://httpd.apache.org/download.cgi
• Scroll down to the middle of the page and look for the “Win32 Binary without
crypto (no mod_ssl)” MSI Installer. Right-click on the link and save the MSI
installer to your computer.

Second, download the PHP 5 Windows Installer from the PHP.net website;
• Go to http://www.php.net/downloads.php
• As of this writing, the current version of PHP is 5.2.6. Under the Windows
Binaries section, click on the “PHP 5.2.6 installer” link.
• A list of download mirrors will appear. Scroll to the bottom of the page. Under
United States, click on the mirror link next to Yahoo! Inc (most likely, it will be
highlighted in yellow.)
• Save the file to your computer when prompted.

We now have installers for both the Apache web server and PHP, so we’re ready to begin
installation.

2. Installing the Apache web server


Let’s install our web server before we begin tackling PHP.
• Locate the Apache MSI installer you downloaded and double-click on it.
• Click Next
• Accept the terms of the license agreement and click Next.
• The Read Me First screen introduces Apache, provides some useful links, and
details and a number of technical disclaimers. When you’re ready to move on,
click Next.
• The Server Information screen asks you to verify a number of settings about
your machine. Apache assumes you are making your web server accessible to the
outside world, and fills in all of the fields for you accordingly. Because we’re the
setting up the web server only for personal use, let’s change the values of the
fields to those similar in the figure below:

o Network domain: Change this to “localhost”


o Server Name: Remove any part of the name that looks like a URL
o Email Address: You can type in your real email address, or make
something up (e.g., admin@localhost)
o Finally, keep selected the Recommended installation setting – All Users,
Port 80, install as a Service. Apache will automatically start up when you
boot your computer and run on the standard HTTP port.

• Click Next. Make sure the Typical setup type is selected, and click Next.
• You may accept Apache’s default installation folder, or change it to something
more convenient (e.g., “C:\Apache\”). After choosing a folder, click Next.
• Click Install.

When the installation is complete, you should see a icon in the system tray of your
taskbar (near the clock.) A green triangle means Apache is running; a red square means
it has stopped. Open a web browser and go to http://localhost. If you see “It Works!”,
your server is up and running.

Advanced Note: Apache will not run if you have another web server (e.g., Microsoft IIS)
running on your computer listening for connections on port 80. Stop the other web
server(s) and start Apache again…it should work.

3. Installing PHP
Now that we have our web server installed and running, let’s install PHP.
• Locate the PHP MSI installer you downloaded and double-click on it.
• Click Next
• Accept the terms of the license agreement and click Next.
• You may accept PHP’s default installation folder, or change it to something more
convenient (e.g., “C:\PHP\”). After choosing a folder, click Next.
• The Web Server Setup screen asks you to choose the web server you wish to
setup to run PHP. Choose “Apache 2.2.x Module” (see below) and click Next.

• The Apache Configuration Directory screen asks you to locate the folder
containing Apache's configuration file, httpd.conf. Browse to the folder where
you installed Apache and click on the “conf” folder (see figure below.)
• Click OK, then click Next.
• The Choose Items to Install allows you to install additional modules (or
“extensions”) into PHP. We would like to install PHP’s MySQL module.
o Expand the Extensions node (click on the plus sign.)
o Locate the MySQL extension (extensions are listed in alphabetical order.)
o Click on the red X icon, then click on the Will be installed on your local
hard drive option. You should now see something like this:
• Click Next, then click Install.

If the PHP installer cannot locate Apache’s configuration file, you will get an error
message (but PHP will still install successfully.) The end of the next section will help
you manually configure Apache to run PHP.

4. Configuring Apache to run PHP


Even if you received no error messages during the PHP install, we still want to make one
minor adjustment to our Apache configuration to make running PHP a bit easier. If you
did receive an error, this section will help you manually configure Apache to run PHP.

• Locate the Apache configuration file (httpd.conf):


o Go to the folder in which you installed Apache
o Open the “conf” folder, then open httpd.conf.

This file can look rather intimidating, but most of it is made up of comments to help you
configure your web server. The minor adjustment we would like to make is to allow
Apache to treat index.php files as default files for any directory (so people don’t have to
explicitly type “index.php” in their web browsers.)

Look for these three lines of text in the file:

<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

What the above snippet does is tell Apache to serve the “index.html” file under a
directory (if it exists) whenever a client asks for that directory. After “index.html”, add a
space and type “index.php”. Save the file and close it.

Finally, left-click on the icon in your system tray, choose Apache 2.2, and select
Restart. Apache now runs with your changes taken into account.

4.1 Manually Configuring Apache to run PHP

If you received an error in the PHP installation process, following these steps to manually
configure Apache:

• Open the httpd.conf file (see Section 4 above.)


• Scroll down to the very end of the file
• Add the following lines:
# Configuration settings for PHP
LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"
PHPIniDir "C:/Program Files/PHP/"

The first line is simply a comment to explain what the proceeding lines are for. The
second line tells Apache were its PHP module is located on your PC, so it can load the
module into memory at startup. The third line tells Apache where PHP’s main
configuration settings (php.ini) are located.

We have to modify one additional file, and then we’re done. To help Apache funnel PHP
requests over to the PHP module correctly, we have to tell Apache which files we want
the PHP module to handle. We do this based on MIME-types and file extensions.

• In the same folder as httpd.conf is a file named mime.types.


• Open mime.types with Notepad or Wordpad.
• Scroll down to the 2 following lines and add a blank line between them:

application/pgp-signature asc sig


application/pics-rules prf

• On the empty line, add the following text (noted here in italics):

application/pgp-signature asc sig


application/x-httpd-php php
application/pics-rules prf

• Save the file, and restart Apache (see steps in Section 4.)

5. Testing your Configuration – Hello, World


Now that we’ve set up Apache to work with PHP, let’s test our configuration.

• Go to the folder in which you installed Apache.


• Open the “htdocs” folder. This is the “root” folder for all the scripts, HTML
documents, and other resources that are publicly available over the Web.
• Rename the index.html file (e.g., index_old.html.)
• Create a new file named index.php inside the “htdocs” folder.
• Open index.php, type the following lines, and save the file:

<?php
echo("Hello, World!");
?>

• Open your web browser and go to http://localhost. If you see “Hello, World!”,
then Apache is serving PHP files successfully.

Vous aimerez peut-être aussi