Demystifying The Code

PHP and IIS: Running PHP under Fast CGI and Url Rewriting

Introduction

You can run PHP (as well as other CGI apps) on IIS7 with high performance and reliability thanks to FastCGI.  CGI is a protocol that defines how web servers launch an executable resulting from a request, pass it arguments and return the dynamic response.  You have been able to host CGI applications in IIS in the past, however there have been challenges prior to FastCGI.  As it turns out, the challenge is that there are certain modules (in PHP, for instance) that are not thread safe.  For that reason, IIS traditionally spun up a new process for each request.  This provided the reliability necessary in the face of non-thread-safe modules.  However, the performance suffered due to the cost of spinning up all of these processes.  Enter FastCGI.  It guarantees that each process will handle 1 request at a time (providing the reliability), but it allows for process reuse by maintaining a pool of processes.

While installing PHP on IIS7 wasn’t overly complex in the past (see this article if you want the details) we now have the Web Platform Installer that makes installing FastCGI, PHP and much much more a breeze.  Now, just because you can host PHP on IIS with reliability and high performance, it doesn’t necessarily mean you should.  You need some reasons, right?  Well, in this post, I will illustrate how to install PHP and FastCGI on IIS7 easily and illustrate one of many reasons you will want to: The UrlRewrite Module.  This module allows you to easily create SEO-friendly Uris for your application.  Let’s get to it.

 

PHP Interop Series

This is part of a small series of blog posts and screencasts that I am doing on interoperability between PHP and the Microsoft stack.  My goal is to illustrate some features of the MS stack that PHP developers can take advantage of, as well as to illustrate how it is done.

 

Screencasts

You can see screencasts on YouTube of the topics in this post here:

Set Up a Test Environment (optional)

You can test all of this on Vista, Windows 7 or Windows Server 2008.  We have made it pretty easy for you to to set up a virtual environment for testing.  You can click here to download Windows Server 2008 Evaluation Virtual Hard Drive Images.  Although it says Virtual Server 2005 is required, the VHD works on Virtual PC 2007, as well.  You simply need to download the WS08_RTM_x86_EnterpriseVHD.exe or, if you want smaller downloads the WS08_RTM_x86_Enterprise.part01.exe and all of the subsequent rar files.  You will also need the WS08_ServerCore86_EnterpriseVHD.exe.  You simply uncompress these and register the VHD with Virtual PC or Virtual Server.  That is exactly what I did.

image

Install the Web Platform Installer

  1. Navigate to http://www.microsoft.com/web/downloads/platform.aspx and click the download link to ‘Microsoft Web Platform Installer 2.0 Beta
    image
  2. Click Run (If you get a security warning, click run again)
    image
  3. The Web Platform Installer is now installed.  You will see the UI, where you can choose items to install.  Feel free to close this.  You can re-open it at any time.
    image

 

Install PHP, FastCGI and the URLRewrite Module

  1. Open the Web Platform Installer 2 Beta (if you closed it)
  2. Under ‘Web Platform’, choose ‘Web Server’.  Select the following items to install
    • Static Content
    • URL Rewrite
    • CGI

    image

  3. Under ‘Web Platform’, Choose ‘Frameworks and Runtimes’.  Select the following item to install
    • PHP 5.2.9-2

    image

  4. Click ‘Install’
  5. Click ‘I Accept’ if you accept the license terms and conditions
  6. That is it!  PHP is installed and FastCGI is configured for you.

 

Test a PHP Sample Application

I have a very simple PHP application that you can use to test your install (as well as the rewrites later).  You can download it here.  If you have your own application feel free to use it.  Here are the steps I used to setup my test environment on my Windows Server 2008 VHD:

  1. Create a directory called Demos
  2. Download the sample application from the link above
  3. Copied the TestPHP directory under my new demos directory.  (if you cannot seem to copy in your virtual hard disk, make sure you may need to install the virtual pc additions)
  4. Register the virtual directory in IIS
    • Open IIS
    • Right-Click on your web and choose Add Virtual Directory
      image
    • Set the Alias to ‘TestPHP’ and the Path to C:\Demos\TestPHP
      image
    • Open a browser and navigate to: http://localhost/TestPHP/catalog.php.  You should see something like this:
      image

    To prove to yourself that php is actually running and configured, navigate to http://localhost/TestPHP/contactus.php.  Take a look at the code and you will see the line of code that adds the header ‘Contact Us’:

<?php echo "Contact Us" ?>

You can also navigate to http://localhost/TestPHP/index.php.  It will echo the results of phpinfo().

 

Add some UrlRewrite Rules

Remember that we have already installed the UrlRewrite 1.1 Module with the Web Platform Installer.  Let’s use this module so we can create some friendly URLs for our PHP Application.  We do this by adding rewrite rules.  I’m going to show you a feature that is new to version 1.1 called Rewrite Maps.  It acts just like it sounds.  It allows you to define simple rewrite maps (your basic from –> to mapping).  I would guess that this will be used pretty commonly when creating search-engine-friendly URLs.  Let’s take a look.

  1. Open up IIS and click on your default web site.  Then double-click on Url Rewrite

    image
  2. Now we want to create our map.  Click on the ‘View Rewrite Maps…’ link on the right side.

    image
  3. Click ‘Add Rewrite Map…’

    image
  4. Click ‘Add Mapping Entry…’

    image
  5. Remembering that Virtual Directory name was ‘TestPHP’ and our catalog page was called ‘catalog.php’.  The URL to our page is: http://localhost/TestPHP/catalog.php.  We also had a page called ‘contactus.php’ (Url http://localhost/TestPHP/contactus.php). These are pretty ugly Urls. The extension ‘php’ means nothing to the general public.  Perhaps we want the Urls to look more like http://localhost/catalog and http://localhost/contact.  Let’s add the following 2 rules:

    image

    image

  6. Now we need to add the rewrite rule that uses the map.  Click ‘Add Rules…’

    image
  7. Choose ‘Rule with rewrite map’

    image
  8. Leave the rule action set to ‘Rewrite’.  Under the ‘Choose the rewrite map’ section, choose the map you created earlier.  Mine was named ‘PHPTestMap’.  Click ‘OK’

    image

 

Test It

Now we can type in our friendly URI into the address bar of our browser.  Nice.

image

Conclusion

As you can see, the Web Platform Installer makes configuring PHP to run on IIS a simple task.  You can also see the power of the IIS pipeline.  There are a wide array of modules like the UrlRewrite module that you can take advantage of.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Demystifying The Code