Apache PHP Tutorial 2
Apache PHP Tutorial 2
University of Pittsburgh
PHP and Server-Side Technologies
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.
• 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.
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.)
<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.
If you received an error in the PHP installation process, following these steps to manually
configure Apache:
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.
• On the empty line, add the following text (noted here in italics):
• Save the file, and restart Apache (see steps in Section 4.)
<?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.