Creating a new IIS 7.5 Fast CGI web site for your PHP project

by Klaus Graefensteiner 22. April 2010 03:54

Introduction

This is the sixth article of a series of blog post called the WIMPinator Chronicles that describe how to setup a PHP development environment for Windows 7 and IIS 7.5.

So far we covered how to install a Wordpress blog and all its dependencies using the WPI (Web Platform Installer). Then we added additional features and extensions to the PHP deployment on Windows for IIS. I explained how to get PEAR setup and how to download and deploy the PHPUnit unit testing framework using PEAR. The last two posts described how to install Eclipse PDT and make it work with Subversion source control using Subclipse. There is one big piece missing so far and this is the testing of your PHP project on your IIS test web server. The next two blog posts will describe how to create an IIS 7.5 Fast CGI web site from scratch and how to use ANT build tasks to deploy your PHP project from Eclipse to your IIS test server during a custom build step.

In this part I am going to first explain how to create an IIS 7.5 Fast CGI PHP web site on IIS 7.5 from scratch.

The Series

  1. Getting a Wordpress blog installed in a jiffy on Windows 7 using the Web Platform Installer
  2. Adding additional features to PHP
  3. Installing PEAR and PHPUnit
  4. Installing Eclipse PDT
  5. Configuring Eclipse PDT to work with Subversion source control
  6. Creating a new IIS 7.5 fast CGI web site
  7. Configuring Eclipse to work with Ant build tasks
  8. Setting up XDebug with Eclipse and IIS 7.5
  9. Setting up the Zend Debugger with Eclipse and IIS 7.5
  10. Configuring Ruby and Watir
  11. Moving a Wordpress blog from GoDaddy shared hosting to my local debugging system.
  12. NEW: Setting up the CodeIgnitor MVC framework with PDT and IIS 7.5
  13. NEW: Configuring IIS 7.5 Rewrite rules to exclude index.php from the URL
  14. NEW: Configuring SSL on IIS 7.5

References

This blog post is based on, sometimes out-of-date, articles that I found on the InterWeb. Here are the links for these:

http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/

http://learn.iis.net/page.aspx/728/best-practices-for-php-on-the-microsoft-web-platform/

Creating a new web application

First open Internet Information Services (IIS) Manager. On Windows 7 just run Start and enter IIS. You should see something like this:

image

Figure 1: Open IIS Manager. Start –> Run –> IIS

Once IIS Manager is open connect to your local web server.

image_2 

Figure 2: Connect to your local web server

Browse to your Default Web site node in your web sites connections tree on the left.

image_4

Figure 3: Select the Default Web Site

Using Explorer create a new folder that will be the root of your new web application. I created the folder under c:\inetpub\wwwroot. I called it CIM225.

image_8

Figure 4: Create new folder

Back in the IIS Manager right mouse click and select the Add Application… menu option.

image_6

Figure 5: Add Application…

This will open the Add Application dialog. Provide the Alias name of the application and the physical path. This is the folder that we just created.

image_10

Figure 6: Configure the path and the alias name of the new application

The new web application has been added to the Default Web Site.

image_12

Figure 7: CIM225 is a new web application

Enabling FastCGI

If PHP has been installed on Windows 7 using the Web Platform Installer, then PHP and IIS should be configured correctly out of the box for fast CGI. In any case I recommend to verify the settings. Here is how you do it.

First make sure that all required IIS modules are installed. Go do the Control Panel and click on the Programs node.

image_14

Figure 8: Open the Control Panel and then the Programs page

Then click on “Turn Windows features on or off” link. Now navigate to the IIS Settings->World Wide Web Services node and verify that CGI is installed.

image_16

Figure 9: Navigate to the IIS Settings and verify that CGI is installed

Now verify the configuration of the Handler Mappings in IIS to make sure that the *.php file extension is handled using FastCGI. Double click the Handler Mappings icon in the IIS Manager.

image_44

Figure 10: Double click the Handler Mappings Icon

In the Handler Mappings list find the PHP_via_FastCGI entry. If it is already there compare its settings with the ones that follow. If you can’t find it, then add it. Use the provided values below.

image_46

Figure 11: Verify the PHP_via_FastCGI entry for *.php

Verify the settings in the Edit Module Mapping dialog.

image_48

Figure 12: Verify the settings in the Edit Module Mapping dialog

The values must be:

Request path: *.php
Module: FastCgiModule
Executable: "C:\[Path to your PHP installation]\php-cgi.exe"
Name: PHP via FastCGI

Editing the PHP.INI file

Now its time to verify your php.ini file. Open the global php.ini file and verify the following configuration:

Note: Before you can change the php.ini file you must stop IIS. Run a command prompt as Administrator and stop IIS with the iisreset /stop command. You also might have to run the editor like notepad or notepad2 as Administrator in order to save the changes back to the document. A normal user normally doesn’t have access to the PHP folder. Don’t forget to enable IIS again later using iisreset /start.

image_24

Figure 13. Stop IIS as Administrator

In the PHP.ini file search for the following line and uncomment it, if necessary:

Set cgi.force_redirect = 0.

image_20

Figure 14: Before

 image_26

Figure 15: After

Set fastcgi.impersonate = 1. FastCGI under IIS supports the ability to impersonate security tokens of the calling client. This allows IIS to define the security context that the request runs under.

image_28

Figure 16: Before

image_30

Figure 17: After

Set cgi.fix_pathinfo=1. cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's previous behavior was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not care what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting this to 1 will cause PHP CGI to fix its paths to conform to the spec

image_32

Figure 18: Before

image_34

Figure 19: After

Set open_basedir to point to a folder or network path where the content of the web site(s) is located.

image_36

Figure 20: Use this setting on a production server to limit file access

This settings doesn’t seem to make sense on my dev machine, but should be used on a production server.

Set extension_dir to point to a location where PHP extensions reside. Typically, for PHP 5.2.X that would be set as extension_dir = "./ext"


image_38

Figure 21: Set the extension directory

Enable the required PHP extension by un-commenting corresponding lines, for example:

extension=php_mssql.dll
extension=php_mysql.dll

image_40

Figure 22: Enable necessary extensions

Now we are done with the php.ini configuration that is specific to running PHP as FastCGI under IIS 7.5. Next we look at the file access security settings.

Setting the Windows 7 file access permissions

The last important thing here is to assign the correct permissions to the web application folder. By default on Windows 7 the users don’t have write permissions for the application folder. In the next blog post of this series we are going to copy the current php project from the local Eclipse workspace straight into the root folder of the web application that we just created. For this we need write permissions for the user the Eclipse build runs under. In my case this user is the desktop user.

Here is how you do it:

Select the application folder CIM225 and right mouse click and select Properties. Then go to the Security tab.

image_160

Figure 23: Go to the Security tab in the web application folder properties

Press the Edit… button.

image_162

Figure 24: Edit the file access permissions

Then push Add and enter the user that Eclipse runs under. In my case it is the logged in user.

image_164

Figure 25: Add the Eclipse “Build” user

Then give this user Full Control permissions by clicking the Allow Full Control check box.

image_166

Figure 26: Full control to the web application folder

Testing the web application settings

Now let’s verify that everything is configured correctly.

After turning IIS back on run php –info in the command prompt and re-direct the output into a file. Look in the file for any errors.

image_42

Figure 27: Run php -info

In addition use a php-info.php file that calls phpinfo() function to return a similar result as nicely formatted html when requested by a browser.

Test that the handler mapping works correctly by creating a phpinfo.php file and copying it into the C:\inetpub\wwwroot folder and/or in your application folder. The PHP file contains the following code:
<?php phpinfo(); ?>

image_50

Figure 28:  Create a phpinfo.php file and copy it into the default sites root folder

Open a browser and navigate to http://localhost/phpinfo.php. If everything was setup correctly, then you will see the standard PHP information page:

image_52

Figure 29: PHPInfo() requested from Default Website root.

image_54

Figure 30: PHPInfo() requested from web application root 

Ausblick

Now we are setup for the next few blog posts that are all about testing and debugging the Eclipse PDT PHP project using Ant and debug extensions.

Stay tuned!

Tags: , , ,

WIMPinator Chronicles | Tips & Tricks | Php | IIS

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as developer in Test at Rockwell Automation and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Administration

About

Powered by:
BlogEngine.Net
Version: 1.5.0.7

License:
Creative Commons License

Copyright:
© Copyright 2009, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2009 by Klaus Graefensteiner

Rendertime:
Page rendered at 9/3/2010 12:31:38 AM (PST Pacific Standard Time UTC DST -7)