Per-site PHP configuration with IIS FastCGI

Posted: Jul 12, 2008  

Average Rating

Tags
FastCGI
PHP

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

Read the complete post here

Comments

Page view counter