How To Add A FastCGI Environment Variable For PHP

This article discusses all the steps you need to add a FastCGI environment variable for PHP in IIS web server.

The steps below would work only if you ensure that you have installed in your device after downloading the binaries which will conduct the execution of the file path or the extension of the file name that you have specified.

The implementation of the PHP we have used in the example below is as specified in the,” www. php.net'” platform.

Step 1: Open Internet Information Services (IIS) Manager

For those who are making use of either the 2012 Windows Server or the 2012 R2 Windows Server, then you should select the Server Manager option you see within the taskbar, click on the tools option which appears and choose the option tagged “Internet

Information Services (IIS) Manager

For the users of Windows 8.1 or the Windows 8, press the Windows key down, click on the letter X, and then select the Control Panel option you see. Select the Administrative Tools, and then click twice on the Internet Information Services (IIS) Manager.

For those making use of the 2008 R2 Windows Server or the 2008 Windows Server, click on the Start feature you see on the taskbar, hover your mouse over the Administrative Tools option to bring forth some options. Then select the Internet

Information Services (IIS) Manager.

For the users of Windows 7 or the Windows Vista, follow the Start feature you see on the taskbar and select the Control Panel option. Click twice on the Administrative Tools, and then click twice again on the Internet Information Services (IIS) Manager.

Step 2: Choose Server Name

In the pane tagged “Connections”, choose the name of the server you wish to configure the settings of the FastCGI to:

Step 3: Choose Setting

Then in the panel tagged “Home”, click twice on the FastCGI Settings option.

How To Add A FastCGI Environment Variable For PHP

Step 4: Edit PHP

You should highlight the PHP application found on the panel of the FastCGI Settings and then the click on the“Edit” option under the panel for “Action.

How To Add A FastCGI Environment Variable For PHP

Step 5: Open Environment Variables

In the dialog box for the Edit FastCGI Application, click on the ellipsis which stands next to the EnvironmentVariables in order to make the collection editor for the environment variables open.

How To Add A FastCGI Environment Variable For PHP

Step 6: Click On

Add

In the dialog box which appears for the EnvironmentVariables Collection Editor, click on the “add” feature.

How To Add A FastCGI Environment Variable For PHP

Step 7: Enter Information

The information "PHP_FCGI_MAX_REQUESTS" should be entered to represent the name, and then for the Value, “10000” should be entered.

How To Add A FastCGI Environment Variable For PHP

Step 8: Close Application

As soon as you’ve done that, click on the “OK” option to close the dialog box of the Add FastCGI Application.

Configuration

The element of the FastCGI can be configured at the level of the server in the file of the"ApplicationHost.config”, or at the level of the site, the level of the application or then in a “Web.config” file at the level of the directory.

Child Elements

Application:It is an element which is optional and adds to the set of definitions of the FastCGI process pool, a certain definition of a FastCGI process pool.

Configuration Sample

The configuration sample below, incoporates FastCGI application for PHP, sets the instanceMaxRequests attribute to "10000," and sets the PHP_FCGI_MAX_REQUESTS environment variable to "10000.

<fastCgi>
<application fullPath="C:PHPphp-cgi.exe"
arguments=""
maxInstances="4"
idleTimeout="300"
activityTimeout="30"
requestTimeout="90"
instanceMaxRequests="10000"
protocol="NamedPipe"
flushNamedPipe="false">
<environmentVariables>
<environmentVariable
name="PHP_FCGI_MAX_REQUESTS"
value="10000" />
</environmentVariables>
</application>
</fastCgi>

Note that the environment variable of the PHP_FCGI_MAX_REQUESTS used for a PHP should be a number either equal to or less than the value which has been specified by the FastCGI <application> element’s attribute "instanceMaxRequests" .

Also, each of the sample adds a wide FastCGI handler mapping used for PHP type files to IIS. It also shows the complete path to the FastCGI file which is executable.

<configuration>
<location path=""overrideMode="Allow">
<system.webServer>
<handlers>
<add name="PHP-FastCGI"
path="*.php"
verb="GET,HEAD,POST"
modules="FastCgiModule"
scriptProcessor="C:PHPphp-cgi.exe"
resourceType="Either"
requireAccess="Script" />
</handlers>
</system.webServer>
</location>
</configuration>

You should note that you can configure the settings of the FastCGI per-site by using a FastCGI application which has been formatted specially and also by using mappings of handlers which are differentiated from others by matching the “arguments” and “fullPath” attributes for an element of an <application> alongside the corresponding attribute, “scriptProcessor"for the process of the FastCGI in the <handlers> mapping.

The attribute of the scriptProcessor in the <handlers> mapping for the process of the FastCGI should be merged to the arguments and fullPath attributes’ settings for an element of an<application> and then separated by the use of a pipe character “ | ” in order to make these mappings.

The combination stands as a unique type of key for every one of the FastCGI application. An example is shown below:

<fastCgi>
<application fullPath="C:PHPphp-cgi.exe"
arguments="-d open_basedir=C:WebsitesWebsite1" />
<application fullPath="C:PHPphp-cgi.exe"
arguments="-d open_basedir=C:WebsitesWebsite2" />
</fastCgi>
 
<system.webServer>
<handlers accessPolicy="Read, Script">
<add name="PHP via FastCGI 1"
path="*.php"
verb="*"
modules="FastCgiModule"
scriptProcessor="C:PHPphp-cgi.exe|-d open_basedir=C:WebsitesWebsite1"
resourceType="Unspecified"
requireAccess="Script" />
<add name="PHP via FastCGI 2"
path="*.php"
verb="*"
modules="FastCgiModule"
scriptProcessor="C:PHPphp-cgi.exe|-d open_basedir=C:WebsitesWebsite2"
resourceType="Unspecified"
requireAccess="Script" />
</handlers>
</system.webServer>

Sample Code

The sample below, used in configuring for a PHP, a FastCGI application, setting to a "10000” attribute, the instanceMaxRequestsand also setting to "10000", the environment variable for the PHP_FCGI_MAX_REQUESTS. Also, each of the sample adds a wide FastCGI handler mapping used for PHP type files to IIS. It also shows the complete path to the FastCGI file which is executable.

AppCmd.exe

appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:PHPphp-cgi.exe',arguments='',maxInstances='4',idleTimeout='300',activityTimeout='30',requestTimeout='90',instanceMaxRequests='10000',protocol='NamedPipe',flushNamedPipe='False']" /commit:apphost
 
appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:PHPphp-cgi.exe'].environmentVariables.[name='PHP_FCGI_MAX_REQUESTS',value='10000']" /commit:apphost
 
appcmd.exe set config -section:system.webServer/handlers /+"[name='PHP-FastCGI',path='*.php',verb='GET,HEAD,POST',modules='FastCgiModule',scriptProcessor='C:PHPphp-cgi.exe',resourceType='Either',requireAccess='Script']" /commit:apphost

You should note that when you are making use of the AppCmd.exe in configuring these listed settings, you must ensure the commit parameter is set to “apphost”. Doing this links these settings for configurations to the correct location part in the ApplicationHost.config file.

The C#

using System;
usingSystem.Text;
usingMicrosoft.Web.Administration;
 
internalstaticclassSample
{
privatestaticvoidMain()
{
using (ServerManagerserverManager = newServerManager())
          {
             Configuration config = serverManager.GetApplicationHostConfiguration();
 
ConfigurationSectionfastCgiSection = config.GetSection("system.webServer/fastCgi");
ConfigurationElementCollectionfastCgiCollection = fastCgiSection.GetCollection();
ConfigurationElementapplicationElement = fastCgiCollection.CreateElement("application");
applicationElement["fullPath"] = @"C:PHPphp-cgi.exe";
applicationElement["arguments"] = @"";
applicationElement["maxInstances"] = 4;
applicationElement["idleTimeout"] = 300;
applicationElement["activityTimeout"] = 30;
applicationElement["requestTimeout"] = 90;
applicationElement["instanceMaxRequests"] = 10000;
applicationElement["protocol"] = @"NamedPipe";
applicationElement["flushNamedPipe"] = false;
 
ConfigurationElementCollectionenvironmentVariablesCollection = applicationElement.GetCollection("environmentVariables");
ConfigurationElementenvironmentVariableElement = environmentVariablesCollection.CreateElement("environmentVariable");
environmentVariableElement["name"] = @"PHP_FCGI_MAX_REQUESTS";
environmentVariableElement["value"] = @"10000";
environmentVariablesCollection.Add(environmentVariableElement);
fastCgiCollection.Add(applicationElement);
 
ConfigurationSectionhandlersSection = config.GetSection("system.webServer/handlers");
ConfigurationElementCollectionhandlersCollection = handlersSection.GetCollection();
ConfigurationElementaddElement = handlersCollection.CreateElement("add");
addElement["name"] = @"PHP-FastCGI";
addElement["path"] = @"*.php";
addElement["verb"] = @"GET,HEAD,POST";
addElement["modules"] = @"FastCgiModule";
addElement["scriptProcessor"] = @"C:PHPphp-cgi.exe";
addElement["resourceType"] = @"Either";
addElement["requireAccess"] = @"Script";
handlersCollection.AddAt(0, addElement);
 
serverManager.CommitChanges();
          }
   }
}

The VB.NET

Imports System
ImportsSystem.Text
ImportsMicrosoft.Web.Administration
 
Module Sample
 
Sub Main()
DimserverManagerAsServerManager = NewServerManager
DimconfigAs Configuration = serverManager.GetApplicationHostConfiguration
 
DimfastCgiSectionAsConfigurationSection = config.GetSection("system.webServer/fastCgi")
DimfastCgiCollectionAsConfigurationElementCollection = fastCgiSection.GetCollection
DimapplicationElementAsConfigurationElement = fastCgiCollection.CreateElement("application")
applicationElement("fullPath") = "C:PHPphp-cgi.exe"
applicationElement("arguments") = ""
applicationElement("maxInstances") = 4
applicationElement("idleTimeout") = 300
applicationElement("activityTimeout") = 30
applicationElement("requestTimeout") = 90
applicationElement("instanceMaxRequests") = 10000
applicationElement("protocol") = "NamedPipe"
applicationElement("flushNamedPipe") = False
 
DimenvironmentVariablesCollectionAsConfigurationElementCollection = applicationElement.GetCollection("environmentVariables")
DimenvironmentVariableElementAsConfigurationElement = environmentVariablesCollection.CreateElement("environmentVariable")
environmentVariableElement("name") = "PHP_FCGI_MAX_REQUESTS"
environmentVariableElement("value") = "10000"
environmentVariablesCollection.Add(environmentVariableElement)
fastCgiCollection.Add(applicationElement)
 
DimhandlersSectionAsConfigurationSection = config.GetSection("system.webServer/handlers")
DimhandlersCollectionAsConfigurationElementCollection = handlersSection.GetCollection
DimaddElementAsConfigurationElement = handlersCollection.CreateElement("add")
addElement("name") = "PHP-FastCGI"
addElement("path") = "*.php"
addElement("verb") = "GET,HEAD,POST"
addElement("modules") = "FastCgiModule"
addElement("scriptProcessor") = "C:PHPphp-cgi.exe"
addElement("resourceType") = "Either"
addElement("requireAccess") = "Script"
handlersCollection.AddAt(0, addElement)
 
serverManager.CommitChanges()
EndSub
 
EndModule

The JavaScript

varadminManager = newActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
 
varfastCgiSection = adminManager.GetAdminSection("system.webServer/fastCgi", "MACHINE/WEBROOT/APPHOST");
varfastCgiCollection = fastCgiSection.Collection;
varapplicationElement = fastCgiCollection.CreateNewElement("application");
applicationElement.Properties.Item("fullPath").Value = "C:\PHP\php-cgi.exe";
applicationElement.Properties.Item("arguments").Value = "";
applicationElement.Properties.Item("maxInstances").Value = 4;
applicationElement.Properties.Item("idleTimeout").Value = 300;
applicationElement.Properties.Item("activityTimeout").Value = 30;
applicationElement.Properties.Item("requestTimeout").Value = 90;
applicationElement.Properties.Item("instanceMaxRequests").Value = 10000;
applicationElement.Properties.Item("protocol").Value = "NamedPipe";
applicationElement.Properties.Item("queueLength").Value = 1000;
applicationElement.Properties.Item("flushNamedPipe").Value = false;
applicationElement.Properties.Item("rapidFailsPerMinute").Value = 10;
 
varenvironmentVariablesCollection = applicationElement.ChildElements.Item("environmentVariables").Collection;
varenvironmentVariableElement = environmentVariablesCollection.CreateNewElement("environmentVariable");
environmentVariableElement.Properties.Item("name").Value = "PHP_FCGI_MAX_REQUESTS";
environmentVariableElement.Properties.Item("value").Value = "10000";
environmentVariablesCollection.AddElement(environmentVariableElement);
fastCgiCollection.AddElement(applicationElement);
 
varhandlersSection = adminManager.GetAdminSection("system.webServer/handlers", "MACHINE/WEBROOT/APPHOST");
varhandlersCollection = handlersSection.Collection;
varaddElement = handlersCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "PHP-FastCGI";
addElement.Properties.Item("path").Value = "*.php";
addElement.Properties.Item("verb").Value = "GET,HEAD,POST";
addElement.Properties.Item("modules").Value = "FastCgiModule";
addElement.Properties.Item("scriptProcessor").Value = "C:\PHP\php-cgi.exe";
addElement.Properties.Item("resourceType").Value = "Either";
addElement.Properties.Item("requireAccess").Value = "Script";
handlersCollection.AddElement(addElement, 0);
 
adminManager.CommitChanges();

The VBScript

SetadminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
 
SetfastCgiSection = adminManager.GetAdminSection("system.webServer/fastCgi", "MACHINE/WEBROOT/APPHOST")
SetfastCgiCollection = fastCgiSection.Collection
SetapplicationElement = fastCgiCollection.CreateNewElement("application")
applicationElement.Properties.Item("fullPath").Value = "C:PHPphp-cgi.exe"
applicationElement.Properties.Item("arguments").Value = ""
applicationElement.Properties.Item("maxInstances").Value = 4
applicationElement.Properties.Item("idleTimeout").Value = 300
applicationElement.Properties.Item("activityTimeout").Value = 30
applicationElement.Properties.Item("requestTimeout").Value = 90
applicationElement.Properties.Item("instanceMaxRequests").Value = 10000
applicationElement.Properties.Item("protocol").Value = "NamedPipe"
applicationElement.Properties.Item("queueLength").Value = 1000
applicationElement.Properties.Item("flushNamedPipe").Value = false
applicationElement.Properties.Item("rapidFailsPerMinute").Value = 10
 
SetenvironmentVariablesCollection = applicationElement.ChildElements.Item("environmentVariables").Collection
SetenvironmentVariableElement = environmentVariablesCollection.CreateNewElement("environmentVariable")
environmentVariableElement.Properties.Item("name").Value = "PHP_FCGI_MAX_REQUESTS"
environmentVariableElement.Properties.Item("value").Value = "10000"
environmentVariablesCollection.AddElement(environmentVariableElement)
CallfastCgiCollection.AddElement(applicationElement)
 
SethandlersSection = adminManager.GetAdminSection("system.webServer/handlers", "MACHINE/WEBROOT/APPHOST")
SethandlersCollection = handlersSection.Collection
SetaddElement = handlersCollection.CreateNewElement("add")
addElement.Properties.Item("name").Value = "PHP-FastCGI"
addElement.Properties.Item("path").Value = "*.php"
addElement.Properties.Item("verb").Value = "GET,HEAD,POST"
addElement.Properties.Item("modules").Value = "FastCgiModule"
addElement.Properties.Item("scriptProcessor").Value = "C:PHPphp-cgi.exe"
addElement.Properties.Item("resourceType").Value = "Either"
addElement.Properties.Item("requireAccess").Value = "Script"
CallhandlersCollection.AddElement(addElement, 0)
 
adminManager.CommitChanges()

Check out these top 3 Best web hosting services

Was this article helpful?