Monday, July 15, 2013

Installing PHP

Having installed a web server, the next order of business is to install PHP to our system. This post includes step by step instructions on how to install PHP on a Windows 7 system. If you haven't installed a web server yet, then see the previous post.

According to the PHP website, there are two methods that can be used for installing PHP: manual installation or using the installer. We will use the manual method since it is the preferred method for installing PHP.

We would like to install the latest version of PHP, but it requires us to have "Visual C++ Redistributable for Visual Studio 2012" installed; so download it from this link and install. Make sure to select the file "VSU3\vcredist_x86.exe", otherwise you will get a missing dll error anytime you try to run "php.exe". After that, visit PHP for Windows download page and download the build of PHP called "VC11 x86 Non Thread Safe". The direct link to this file is here. We note that there is also a 64-bit version of PHP, but we will avoid it for the time being because it is still labeled as "experimental".

We extract the zip file we downloaded to a convenient location, such as "C:\PHP". It is not recommended to use a folder which contains spaces in its path (such as "C:\Program Files\PHP"). Next, locate the file "php.ini-production" under "C:\PHP" and rename it to "php.ini". It is a good idea to create a backup of this file before we rename.

Out of the box, the "php.ini" file (which used to be "php.ini-production") looks like this:

We need to make a few changes in this document. Open "php.ini" in a text editor.

  • Search for the phrase "log_errors =" (it is found on line 499). The value for this variable should be "On"; if it is anything else, change it. It should look like below:
  • Now, search for "error_log =" (line 584). Remove the semicolon and set this varible to "c:\inetpub\logs\php_errors.log". After editing, make sure this line reads:
  • Next, search for the phrase "doc_root =" (line 719). Since we are using IIS, we will set this variable to "c:\inetpub\wwwroot". After editing, it should look like this:
  • A few lines below the doc_root variable is the extension_dir variable (line 730), set this variable to "c:\PHP\ext" (be sure to change the path accordingly if you used a different path for PHP). It should look like:
  • Next, search for the phrase "cgi.force_redirect =" (line 747) and set that value to 0. It should look as follows:
  • Go to line 767 or search for the phrase "cgi.fix_pathinfo=" and remove the semicolon. It should be look like:
  • Search for the line that contains ";fastcgi.impersonate = 1" (line 775) and remove the semicolon so that it looks like:
  • Finally, find the line that contains the phrase "fastcgi.logging =" (line 779) and remove the semicolon. Make sure it looks like:

These are all the changes that we need to do to "php.ini". At this point, we need to configure IIS 7 to work with PHP.

  • Go to the Start menu, type "inetmgr" into the search box and run the program. You should get a screen that looks like below:
  • First, open the "Default Document" feature":
  • On the right-hand menu, choose "Add...":
  • Type "index.php" into the field and click "OK":
  • Click the back arrow to go back to the main window. Select "Handler Mappings":
  • On the right-hand menu, choose the "Add Module Mapping" option:
  • In the dialogue that follows, enter "*.php" to the Request path field; "FastCgiModule" to the Module field; "c:\PHP\php-cgi.exe" to the Executable field and "PHP_via_FastCGI" to the Name field. It should look like below:
  • Next, click "Request Restrictions" and check the box next to "Invoke handler only if request is mapped to:" and choose "File or folder" option below. Make sure it looks like:
  • Click "OK" to close all dialogues. Close the IIS Manager as well.

Installation of PHP is now complete. Traditionally, the first program to run on a new programming language is the Hello World Program and that is what we are going to write now. Go to the start menu, type "notepad" in the search box, right click the Notepad application and choose "Run as administrator". Copy and paste the following code from the PHP website:

  <title>PHP Test</title>
   <?php echo '<p>Hello World</p>'; ?> 

Save this file as "hello.php" and place it in the folder "c:\inetpub\wwwroot\". Now, open your web browser and go to "http://localhost/hello.php". If you did everything right, then we should see a blank page with the words "Hello World" on upper left corner:

No comments :

Post a Comment