Using IIS Manager user interface to create a handler mapping for PHP

In the IIS Manager user interface select the server node in the "Connections" tree view;

In the "Features View" page open the "Handler Mappings" feature;

In the "Actions" pane click "Add Module Mapping...";

In the "Add Module Mapping" dialog enter the following:

Request path: *.php

Module: FastCgiModule

Executable: C:\[Path to PHP installation]\php-cgi.exe

Name: PHP_via_FastCGI

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

Click OK on all the dialogs to save the configuration.

Using command line tool to create a handler mapping for PHP

Use the command shown below to create an IIS FastCGI process pool which will use php-cgi.exe
executable for processing PHP requests. Replace the value of the fullPath parameter with the
absolute file path to the php-cgi.exe file.

This command creates an IIS handler mapping for *.php file extension, which will result in all URLs that
end with .php being handled by FastCGI module.

Note:

At this point the required installation and configuration steps are completed. The remaining
instructions below are optional but highly recommended for achieving optimal functionality
and performance of PHP on IIS.

Impersonation and file system access

It is recommended to enable FastCGI impersonation in PHP when using IIS. This
is controlled by the fastcgi.impersonate directive in php.ini
file. When impersonation is enabled, PHP will perform all the file system operations on behalf of
the user account that has been determined by IIS authentication. This ensures that even if the
same PHP process is shared across different IIS web sites, the PHP scripts in those web sites
will not be able to access each other's files as long as different user accounts are used
for IIS authentication on each web site.

For example IIS 7, in its default configuration, has anonymous authentication enabled with
built-in user account IUSR used as a default identity. This means that in order for IIS to
execute PHP scripts, it is necessary to grant IUSR account read permission on those scripts.
If PHP applications need to perform write operations on certain files or write files into some
folders then IUSR account should have write permission to those.

To determine what user account is used as an anonymous identity in IIS 7 use the following command.
Replace the "Default Web Site" with the name of IIS web site that you use. In the output
XML configuration element look for the userName attribute.

If userName attribute is not present in the anonymousAuthentication element,
or is set to an empty string, then it means that the application pool identity is used as an
anonymous identity for that web site.

To modify the permissions settings on files and folders, use the Windows Explorer
user interface or icacls command.

Example #5 Configuring file access permissions

icacls C:\inetpub\wwwroot\upload /grant IUSR:(OI)(CI)(M)

Set index.php as a default document in IIS

The IIS default documents are used for HTTP requests that do not specify a
document name. With PHP applications, index.php usually
acts as a default document. To add index.php to the list
of IIS default documents, use this command:

FastCGI and PHP Recycling configuration

Configure IIS FastCGI settings for recycling of PHP processes by using the commands shown below.
The FastCGI setting instanceMaxRequests controls how many requests will be
processed by a single php-cgi.exe process before IIS shuts it down.
The PHP environment variable PHP_FCGI_MAX_REQUESTS controls how many
requests a single php-cgi.exe process will handle before it recycles
itself. Make sure that the value specified for FastCGI InstanceMaxRequests
setting is less than or equal to the value specified for PHP_FCGI_MAX_REQUESTS.

FastCGI timeout settings

Increase the timeout settings for FastCGI if it is expected to have long running PHP scripts.
The two settings that control timeouts are activityTimeout and
requestTimeout. Use the commands below to change the timeout settings.
Make sure to replace the value in the fullPath parameter to
contain the absolute path to the php-cgi.exe file.

Changing the Location of php.ini file

PHP searches for php.ini file in
several locations and it is
possible to change the default locations of php.ini
file by using PHPRC environment variable. To instruct PHP
to load the configuration file from a custom location run the command shown below.
The absolute path to the directory with php.ini file should be
specified as a value of PHPRC environment variable.

This might be an incredibly stupid comment, but I thought I would share anyway. If you want to install PHP on Windows Server in one click without all the hassle above, simply go here to the official IIS website:http://php.iis.net

Run the install, boom, PHP works on your IIS/Windows server! No command prompt windows, no messing with settings, everything is pre-configured. I followed all the steps on this page and I could not get PHP working for anything. When I was about to give up, I found that link where the official Microsoft installer made it stupidly easy.

It's a bit silly, but it's worth writing it down, as there will be a day you'll have to configure php on IIS in a Preproduction via VPN with a really pushing customer and only few minutes available... and somehow you WILL forget this.

Php.ini by default has the short code set to off, so if you configure everything and test it out with <? phpinfo() ?> you will a blank screen with the code in the source, which can also indicate that there is something wrong in the configuration and that the script doesn't get sent to the php processor. It's not, it's correct! Just test out with <?php phpinfo(); ?> or turn on the short codes!

For setting the time out above it took me forever to figure out the right combination for double and single quotes (and no quotes) given I had spaces in the absolute path to my php-cgi.exe file. Here is what worked in case you have/had the same problem:%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi ^/[fullPath="'C:\Program Files (x86)\PHP\php-cgi.exe'"].activityTimeout:90 /commit:apphost