Wednesday, November 9, 2011

How to install PHP on Windows 7 with IIS 7 via FastCGI

In this programming tutorial, we will learn how to install and configure PHP on windows 7 with IIS 7 via FastCGI. After release of php 5.3 version because php5isapi.dll is no longer available in the php releases starting from 5.3. So my recommendation is with the world’s recommendation and that is to use "FastCGI". Please consider following points before installing PHP on Windows 7.

The FastCGI module in IIS enables popular application frameworks that support the FastCGI protocol to be hosted on the IIS Web server in a high performance and reliable way. FastCGI provides a high-performance alternative to the Common Gateway Interface (CGI), which is a standard way of interfacing external applications with Web servers that has been a part of the supported IIS feature set since the first release.

  1. First of all check the system type; either it is 32 bit or 64 bit. X86 means 32 bit. X64 means 64 bit. Download the appropriate php version according to your system type.
  2. If you are using PHP with IIS you should use the VC9 versions of PHP.
  3. A Thread Safe version should be used if you install PHP as an Apache module. The Non Thread Safe version should be used if you install PHP as a CGI binary.
  4. Non-thread-safe build of PHP is recommended for PHP 5.3 version when using IIS. The non-thread-safe builds are available at » PHP for Windows: Binaries and Sources Releases.

VC9 Versions are compiled with the Visual Studio 2008 compiler and have improvements in performance and stability. The VC9 versions require you to have the » Microsoft 2008 C++ Runtime (x86) or the » Microsoft 2008 C++ Runtime (x64) installed. If you have visual studio 2008 install then you don't have to install Microsoft 2008 C++ Runtime.

What is FastCGI?
FastCGI is a standard protocol that allows the CGI executable files for application frameworks to interface with the Web server. It is different to the standard CGI protocol in that FastCGI reuses CGI processes for multiple requests, which provides a significant performance boost as compared to CGI. Support for IIS FastCGI enables IIS to host normal CGI programs like PHP or Ruby on Rails by using the FastCGI protocol, and to offer high performance and stability for production deployment of such application frameworks.
For having the IIS FastCGI support, you must have following things:
  • IIS Web server
  • IIS FastCGI extension
  • CGI program (such as php-cgi.exe)
The Web server dispatches HTTP requests to your application to the FastCGI component, which in turn launches the CGI program executable, and forwards the request for processing. Once the request is finished and the response is returned back to the server and sent to the client, the CGI process is reused for a subsequent request. This helps to avoid the high performance penalty of starting a new process for each request, which results in better performance and scalability in a production environment. Now let’s come to the topic.

Install PHP on Windows 7 with IIS 7 via FastCGI
First of all, definitely you need to have PHP. Go to the PHP download page and get the latest non-thread-safe ZIP archive of windows binaries for Windows. As of writing this, the package is called "PHP 5.3 (5.3.6) Non-thread-safe zip package". Unzip the archive to a folder on your hard disk. I use C:\php5.

In the latest version of php, you will not find any php.ini-recommended file in php directory. php.ini-recommended is replaced by php.ini-development and php.ini-production files. php.ini-production is used for production systems; for development systems you should use php.ini-development.

Open the php.ini-development and save as php.ini in the same folder and then use the following configuration setting.
Uncomment cgi.force_redirect by removing preceding; and then set it as cgi.force_redirect = 0

This directive is required for running under IIS. It is a directory security facility required by many other web servers. However, enabling it under IIS will cause the PHP engine to fail on Windows.
It is the minimum setting you need to change so that PHP can work with IIS.

Additionally, you may want to use
Uncomment cgi.fix_pathinfo by removing preceding; and then set it as cgi.fix_pathinfo = 1
This lets PHP access real path info following the CGI Spec. The IIS FastCGI implementation needs this set.
Uncomment fastcgi.impersonate by removing preceding; and then set it as 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.

Uncomment extension_dir by removing preceding; and then set it as extension_dir=C:\php5\ext.
The extension_dir needs to point to the directory where PHP extensions files are stored. The path can be absolute (i.e. "C:\PHP5\ext") or relative (i.e. ".\ext"). Extensions that are listed lower in the php.ini file need to be located in the extension_dir.

Note: - It is better approach to save php.ini in c:/windows as well.

PHP is now setup on your system. The next step is to choose a web server, and enable it to run PHP. We will go with IIS 7.
Internet Information Services (IIS) 7 is available on Windows Vista, Windows 7, and Windows Server 2008.
You probably heard a lot of bad things about IIS, from various groups. If you were holding off using IIS because of that well now is the time to re-evaluate IIS with version 7. A lot of the problems that plaques IIS were cured way back in IIS 5. Introduced in IIS 7 was a new architecture similar to Apache with its modules. Everything in IIS 7 is a module, an extension that can be removed. Just need a basic HTTP server? No problem! Just remove or deactivate everything but the HTTP service module.

Now let’s install IIS 7 if it is not installed in Windows 7. Go to the Start/Control Panel/Programs/Turn Windows Features on or off and check on the Internet Information Services entry. Activate the World Wide Web Services/Application Development Features/CGI node and also Web Management Tools/IIS Management Console (the latter not shown in the figure).

Installing PHP on Windows 7 - Step 1

Now, start the IIS Management Console; Open up the start menu, press windows+r to bring run dialog, type inetmgr and hit Enter. There, navigate to the Sites/Default Web Site/Handler Mappings node and double-click on the Handler Mappings entry.

Installing PHP on Windows 7 - Step 2

As a result of this, the Actions panel on the right hand side changes. You will now see an option called Add Module Mapping. Clicking on it brings a dialog which you fill out as you can see in the following figure (you may need to adapt the path used to your local system).

Installing PHP on Windows 7 - Step 3

If you do not see the FastCgiModule entry, you probably forgot to check the CGI node when installing IIS. Otherwise, close the Add Module Mapping dialog by pressing OK button. You need to confirm that you want to create a FastCGI application; click Yes.

Installing PHP on Windows 7 - Step 4

If you don't want to save php.ini in c:\windows then restart the IIS else Reboot your pc.
Finally, create a .php page and put it in the root folder (wwwroot) of the IIS site by default C:\Inetpub\wwwroot, e.g. phpinfo.php with a simple phpinfo() call in it like the code given below.


For Detailed information follow these links as reference:

Using FastCGI to Host PHP Applications on IIS 7

Configuring FastCGI to Host PHP Applications (IIS 7)

No comments :

Post a Comment