Per-site PHP configuration with IIS FastCGI

There have been a few questions on IIS.NET PHP forum regarding enabling per-site PHP configuration. This is a common requirement when running PHP applications in shared hosting environment, because each PHP application may require a different set of PHP settings. Shared hosting providers often want to provide their customers with an option of controlling PHP configuration if necessary.

Until recently, it was thought that per-site PHP configuration was only possible when running PHP on Apache in *nix based OS. However, with FastCGI module it is possible to enable this for PHP applications hosted on IIS 6.0 and IIS 7.0.

The development team at GoDaddy.com has researched and validated several options for enabling per-site PHP configuration on Windows. Based on their findings, we have updated the PHP shared hosting guide with instructions on how to allow per-site php.ini files when running PHP via FastCGI on IIS 7.0. In this post I will explain how to accomplish the same on IIS 6.0 by using FastCGI extension.

Assume we have two web sites in IIS 6.0 – website1.com and website2.com. We want each web site to use its own version of php.ini file. Let’s assume that php.ini for website1.com is located in C:\Inetpub\website1.com folder and php.ini for website2.com is located in C:\Inetpub\website2.com folder.

First step is to create script mappings and FastCgi configuration sections for each web site. For that you can use the helper script fcgiconfig.js located in %WINDIR%\system32\inetsrv\ folder. Execute the following commands to create PHP script mappings for website1.com and website2.com. Make sure you put the correct path to PHP executable instead of <php_path> and correct site ID instead of <site_id>.

cscript fcgiconfig.js -add -section:"PHP website1.com" -extension:php -path:<php_path> -site:<site_id>

cscript fcgiconfig.js -add -section:"PHP website2.com" -extension:php -path:<php_path> -site:<site_id>

After executing these commands open the fcgiext.ini file located in %WINDIR%\system32\inetsrv. It should contain the following sections:

[Types]
php:169297538=PHP website1.com ; The actual site id will be different for your site
php:273357939=PHP website2.com ; The actual site id will be different for your site

[PHP website1.com]
ExePath=C:\php523nts\php-cgi.exe

[PHP website2.com]
ExePath=C:\php523nts\php-cgi.exe

Now [PHP website1.com] and [PHP website2.com] can be used to specify some site specific FastCGI configuration settings. We are going to use these sections to specify the path to php.ini file for each of the web sites.

When PHP process starts it determines the location of configuration php.ini file by using various settings. The PHP documentation provides detailed description of the PHP start up process. Note that one of the places where PHP process searches for php.ini location is the PHPRC environment variable. If PHP process finds a php.ini file in the path specified in this environment variable then it will use it, otherwise it will revert to default location of php.ini. We will configure FastCGI to set this environment variable to point to site specific php.ini file:

cscript fcgiconfig.js -set -section:"PHP website1.com" -EnvironmentVars:PHPRC:C:\Inetpub\website1.com

cscript fcgiconfig.js -set -section:"PHP website2.com" -EnvironmentVars:PHPRC:C:\Inetpub\website1.com

Now, if you look into fcgiext.ini file, you will see the configuration sections updated:

[PHP website1.com]
ExePath=C:\php523nts\php-cgi.exe
EnvironmentVars=PHPRC:C:\Inetpub\website1.com 

[PHP website2.com]
ExePath=C:\php523nts\php-cgi.exe
EnvironmentVars=PHPRC:C:\Inetpub\website2.com

After completing these steps, you can easily verify that PHP loads its configuration from site specific location:

  1. Copy php.ini into C:\Inetpub\website1.com
  2. Create a phpinfo.php file in C:\Inetpub\website1.com
  3. Place this code inside of phpinfo.php: <?php phpinfo(); ?>
  4. Open web browser and make a request to http://website1.com/phpinfo.php

The output of phpinfo.php file will show the location from where php.ini file was loaded:

PerSitePHP

3,111 views

ruslany on July 12th 2008 in FastCGI, PHP

PoorFairAverageGoodExcellent (1 votes, average: 5.00 out of 5)

11 Responses to “Per-site PHP configuration with IIS FastCGI”

  1. Gravatar ImageAmit Shah responded on 16 Jul 2008 at 5:52 am #

    How could we apply rewrite rule on IIS?

    For more specific I want to apply rewrite rule for zend framework using IIS.

  2. Gravatar ImagePablo Weyne responded on 16 Jul 2008 at 6:39 am #

    The security of the server cannot be affected?

  3. Gravatar Imageruslany responded on 16 Jul 2008 at 10:10 am #

    Amit,

    It depends on what version of IIS you are using. If you work with IIS 6.0 then you can use either ISAPI_Rewrite or IONICS Rewrite Filter .

    If you work with IIS 7.0 then you can use the Microsoft URL rewrite module .

  4. Gravatar Imageruslany responded on 16 Jul 2008 at 10:18 am #

    Pablo,

    As usual, in shared hosting environment you would need to ensure that account that is used to run php-cgi.exe has minimal set of privileges. It is recommended that you turn on fastCGI impersonation for PHP. That way php-cgi processes for each web site will be running in a security context of impersonated user for that site. This article explains how to configure fastcgi on IIS 6.0 and turn on impersonation.

  5. Gravatar ImageAmit Shah responded on 16 Jul 2008 at 10:19 pm #

    Hi Ruslany,

    Thanks for reply. Im using IIS 5.1. I have installed ISAPI_Rewrite on my system. Now rewrite rule is working fine. but I have rewrite rule problem with zend framework. Is there any other settings I have to apply?

    Im using following rule for zend framework.
    #RewriteRule ^/amit/(?!index\.php|(?:\.(?:js|ico|gif|jpg|png|css))).*$ /amit/index.php

    My local URL is http://localhost:8081/amit/

  6. Gravatar ImagePablo Weyne responded on 17 Jul 2008 at 11:16 am #

    Thank You! Very good!

  7. Gravatar Imageruslany responded on 17 Jul 2008 at 11:55 am #

    Amit,

    The only thing I can think of is that it may be possible that Zend Framework relies on the REQUEST_URI server variable to contain the original un-rewritten URL. This server variable is not available on IIS 5.1. ISAPI_Rewrite sets another server variable “HTTP_X_REWRITE_URL” that contains the original URL.

    Typically, if PHP application relies on REQUEST_URI, then it is fairly easy to fix it for IIS with ISAPI_Rewrite by adding this code:

    if (isset($_SERVER[ 'HTTP_X_REWRITE_URL' ]))
    $_SERVER[ 'REQUEST_URI' ] = $_SERVER[ 'HTTP_X_REWRITE_URL' ];

  8. Gravatar Images.r. responded on 18 Aug 2008 at 5:27 pm #

    actually %WINDIR%\system32\inetpub\ is not right path
    Should be %WINDIR%\system32\inetsrv\
    I didn’t find inetpub folder in the system32 dir

  9. Gravatar Imageruslany responded on 18 Aug 2008 at 7:05 pm #

    s.r., you are correct. That was a typo – the correct path is %WINDIR%\system32\inetsrv\. Thanks for pointing that out – I’ve fixed the text now.

  10. Gravatar Imageshee responded on 08 Mar 2009 at 2:12 pm #

    Thanks for sharing.

  11. Gravatar ImageKimio Tanaka responded on 12 Mar 2009 at 4:16 am #

    Thank you for the other day.

    At last, I succeeded in Per-site PHP configuration with IIS7 FastCGI
    using GUI.

    http://iis.museum-in-cloud.com/wordpress/index.php/archives/87

Trackback URI | Comments RSS

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Recently Published Articles