Getting Started with PHP On Windows

Intro

For the four-or-so years that I’ve been writing code, it’s always been with a Microsoft technology; starting with VBScript, moving to VB.Net, and eventually C#, Silverlight, and WPF. I’ve been meaning to dive in to the cross-platform, open source world of Web Development, and I have explored the various aspects of it at different times, so I’m going to do an end-to-end, cross platform “Address Book” based on web technologies…HTML 5, CSS, and JavaScript for the front end, PHP for server side scripting, and MySQL for my database. However, I will still be using a Microsoft product…IIS as my web server, for the sake of familiarity. This walkthrough will help you get PHP installed on your Windows machine, and IIS configured to work with PHP.

Preparing IIS

PHP is very well documented, and I suggest you browse through the manual at http://www.php.net/manual/en/index.php to get familiar with the language before diving right in to the preparation and installation. The steps I list below are for IIS 7, on Windows Vista SP1, Windows 7, or Windows 8, and are based on the instructions in the manual I linked to above.

  1. Assuming you already have IIS 7 installed ( go do that, if you don’t), we need to enable FastCGI on IIS. To do this, open Control Panel, and select Programs and Features. In the column on the left side, click “Turn Windows features on or off”. In the Windows Features dialog, expand Internet Information Services, then expand World Wide Web Services, Application Development Features, and select the check box next to CGI. You should see something like this…..
  2. Click Ok, and Windows will do the installation.

Installing PHP

The actual process of installing PHP requires a bit of reading, because you have to set up the config file once you’ve downloaded and extracted the correct build of PHP. Here is a link to the full process… http://www.php.net/manual/en/install.windows.manual.php . Once you download and extract PHP, and get your config file set up, we’ll need to tell IIS how to process PHP requests.

Configuring IIS to Process PHP

These last steps will guide you through using the IIS Manager UI to create a handler mapping for PHP, and, again are based on the steps in http://www.php.net/manual/en/install.windows.iis7.php . I recommend you take a look at their manual after doing this simple UI setup, as they have detailed information on how to use the command line tool, and information about impersonation and file system access.

  1. Open Control Panel, select Administrative Tools, and double click IIS Manager.
  2. Select the Server node in the tree view on the left side, and then double click the Handler Mappings icon in the IIS section of the Features View. It should look similar to this…    
  3. In the Actions pane on the right, click “Add Module Mapping…” 
  4. Enter the following information in the “Add Module Mapping” dialog…
  • Request path: *.php
  • Module: FastCgiModule
  • Executable: C:\[Path to PHP installation]\php-cgi.exe
  • Name: PHP_via_FastCGI

5.  Click “Request Restrictions” button and then configure the mapping to invoke handler only if request is mapped to a file or a folder;

6.  Click OK on all the dialogs to save the configuration.

Test

If everything went according to plan, your default website is now configured to process PHP requests. Here’s how we test it…

  1. Open up Notepad and enter the following line of text, without the quotation marks surrounding it…”<?php echo ‘Hello World’; ?> “
  2. Click File, Save As…, and save the file as Test.php ( be sure to set the file type to All Files) in your My Documents folder.
  3. Copy the Test.php file to C:\inetpub\wwwroot.
  4. Finally, open a web browser and browse to localhost/test.php. You should see your Hello World output.

Conclusion / Now What?

This walkthrough covered the very basics of getting PHP installed, and setting up IIS to process PHP requests. It didn’t begin to touch on the actual language, syntax, or usage of PHP, but there is great documentation and plenty of walkthroughs at php.net to get you started on using PHP. My next blogs will be about setting up MySQL ( Spoiler: it was infinitely easier than my first attempts at installing and configuring Microsoft SQL Server ) and connecting to it from PHP, and how to use browser detection to detect mobile devices and load appropriate css sheets.

Leave a Reply

Your email address will not be published. Required fields are marked *