Apache PHP Tutorial 2

download Apache PHP Tutorial 2

of 6

Transcript of Apache PHP Tutorial 2

  • 8/14/2019 Apache PHP Tutorial 2

    1/6

    IS2955 Web Engineering

    University of PittsburghPHP 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 configuringeither 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 alocal web server and development/testing platform.

    1. Getting Started

    First, download the Apache 2.2 Windows installer from the Apache website:

    Go tohttp://httpd.apache.org/download.cgi

    Scroll down to the middle of the page and look for the Win32 Binary withoutcrypto (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 tohttp://www.php.net/downloads.php

    As of this writing, the current version of PHP is 5.2.6. Under the WindowsBinaries section, click on the PHP 5.2.6 installer link.

    A list of download mirrors will appear. Scroll to the bottom of the page. UnderUnited 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 were ready to begin

    installation.

    2. Installing the Apache web server

    Lets 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.

    http://httpd.apache.org/download.cgihttp://httpd.apache.org/download.cgihttp://httpd.apache.org/download.cgihttp://www.php.net/downloads.phphttp://www.php.net/downloads.phphttp://www.php.net/downloads.phphttp://www.php.net/downloads.phphttp://httpd.apache.org/download.cgi
  • 8/14/2019 Apache PHP Tutorial 2

    2/6

    The Read Me First screen introduces Apache, provides some useful links, anddetails and a number of technical disclaimers. When youre ready to move on,click Next.

    The Server Information screen asks you to verify a number of settings aboutyour 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 were thesetting up the web server only for personal use, lets change the values of the

    fields to those similar in the figure below:

    o Network domain: Change this to localhosto Server Name: Remove any part of the name that looks like a URLo 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 Apaches default installation folder, or change it to somethingmore 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

    mailto:admin@localhostmailto:admin@localhostmailto:admin@localhostmailto:admin@localhost
  • 8/14/2019 Apache PHP Tutorial 2

    3/6

    it has stopped. Open a web browser and go tohttp://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 againit should work.

    3. Installing PHP

    Now that we have our web server installed and running, lets 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 PHPs default installation folder, or change it to something moreconvenient (e.g., C:\PHP\). After choosing a folder, click Next.

    The Web Server Setup screen asks you to choose the web server you wish tosetup to run PHP. Choose Apache 2.2.x Module (see below) and click Next.

    The Apache Configuration Directory screen asks you to locate the foldercontaining Apache's configuration file, httpd.conf. Browse to the folder where

    you installed Apache and click on the conf folder (see figure below.)

    http://localhost/http://localhost/http://localhost/http://localhost/
  • 8/14/2019 Apache PHP Tutorial 2

    4/6

    Click OK, then click Next.

    The Choose Items to Install allows you to install additional modules (orextensions) into PHP. We would like to install PHPs 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:

  • 8/14/2019 Apache PHP Tutorial 2

    5/6

    Click Next, then click Install.

    If the PHP installer cannot locate Apaches 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

    didreceive 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 youconfigure 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 dont have to

    explicitly type index.php in their web browsers.)

    Look for these three lines of text in the file:

    DirectoryIndex index.html

    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 aspace 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 selectRestart. 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:

  • 8/14/2019 Apache PHP Tutorial 2

    6/6

    # 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 themodule into memory at startup. The third line tells Apache where PHPs main

    configuration settings (php.ini) are located.

    We have to modify one additional file, and then were done. To help Apache funnel PHP

    requests over to the PHP module correctly, we have to tell Apache which files we wantthe PHP module to handle. We do this based on MIME-types and file extensions.

    In the same folder as httpd.confis 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 weve set up Apache to work with PHP, lets 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, HTMLdocuments, 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:

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

    http://localhost/http://localhost/http://localhost/http://localhost/