Monday, March 23, 2015

FastCGI Settings

FastCGI Settings


Applies To: Windows Server 2012, Windows Server 2012 R2


Web servers use the FastCGI (Fast Common Gateway Interface) protocol to communicate with external applications. The FastCGI protocol defines how information is passed between the web server and an external FastCGI application. When a request is sent, the web server starts a FastCGI application to process the request, and generate dynamic responses to send back to the client.
FastCGI applications handle HTTP requests and each one consists of a collection of process pools. Within the pools, each process handles requests one at a time and, upon completion, returns to the pool until it receives another request.
Use the FastCGI Settings page to configure process pool settings for the Fast CGI applications on your Web server. You can add FastCGI applications for use by sites on your web server, and specify configuration properties such as the processing variables and maximum requests for these applications.
Related scenarios
In this document

UI Elements for FastCGI Settings

The following tables describe the UI elements that are available on the feature page and in the Actions pane.

Feature Page Elements

 

Element NameDescription
Full Path
Lists the physical path of the process executable to use in the process pool.
Arguments
Lists the arguments to pass to the selected FastCGI application at start time. If several FastCGI process pools exist for a single process executable, these arguments can be used to identify a FastCGI process pool.
Max. Instances
Lists the maximum number of FastCGI processes to allow in the application process pool for the selected FastCGI application. This number also represents the maximum number of concurrent requests that the FastCGI application can handle. The default value is 4.
For the Windows® 7 and Windows Server® 2008 R2 operating systems only, setting theMaxInstances property to 0 lets IIS automatically select the optimal number of FastCGI processes for the current execution environment.
Instance Max. Requests
Lists the maximum number of requests that the selected FastCGI application processes before recycling. The default value is 200.

Actions Pane Elements

 

Element NameDescription
Add Application
Opens the Add FastCGI Application dialog box that lets you configure general and processing properties.
Edit
Opens the Edit FastCGI Application dialog box that lets you change the general and processing properties for the selected application.
Remove
Removes the selected FastCGI application.

Add or Edit FastCGI Application Dialog Box

Use the Add FastCGI Application and Edit FastCGI Application dialog boxes to add or edit general and processing properties for an application.
For more information about FastCGI applications, see the FastCGI Settings Page.

 

Element NameDescription
Full Path
Specifies the physical path of the process executable to use in the FastCGI application pool.
Click to find the physical location of your FastCGI process executable file.
Arguments
Specifies the command-line arguments that are passed to the FastCGI process. These arguments can be used to uniquely identify a FastCGI process pool in cases where several FastCGI process pools exist for a single process executable.
Environment Variables
Specifies the collection of environment variables that are set for the process executable program. This property is optional. Click the Browse () button to open theEnvironmentVariables Collection Editor, from which you can add and remove members and define property values. For more information, see the Environment Variables Editor Dialog Box UI Help page.
Instance MaxRequests
Controls the FastCGI process-recycling behavior. Specifies the maximum number of requests that a FastCGI application is allowed to handle before the process is recycled. The default value is 200.
Max Instances
Specifies the maximum number of FastCGI processes to allow in the application process pool for the selected FastCGI application. This number also represents the maximum number of concurrent requests that the FastCGI application can handle. The default value is 4.
For the Windows® 7 and Windows Server® 2008 R2 operating systems only, setting the MaxInstances property to 0 lets IIS automatically select the optimal number of FastCGI processes for the current execution environment.
Monitor changes to file
This property applies to the Windows 7 and Windows Server® 2008 R2 operating systems only.
Specifies the path of a file. Changes to this file trigger recycling of FastCGI processes. You can specify the absolute path and also the path that is relative to the location of the FastCGI executable.
Standard error mode
This property applies to the Windows 7 and Windows Server® 2008 R2 operating systems only.
Specifies how to handle errors that are reported by a FastCGI process. The following options exist:
  • ReturnStderrIn500 — This module sets the status code to 500 and sends what is received on STDERR as a response. This is the default value and has the same behavior as IIS 8.
  • ReturnGeneric500 — This module sets the status code to 500, but will return a generic 500. This configuration is useful if you want to enable detailed error logging for a server but do not want to return these errors to users.
  • IgnoreAndReturn200 — This module sends what is received on STDOUT as a response with the status code 200. The text on the STDERR stream is ignored. This property is useful if you want to use debug statements for tracking purposes.
  • TerminateProcess — This module terminates the FastCGI process and returns the generic 500 error message.
ActivityTimeout
Specifies the maximum length of time that a FastCGI process for the application is allowed to run without communicating with IIS before the process times out. This timeout can be used to detect and shut down hung processes. The default value is 30(seconds).
FlushNamedpipe
Specifies whether the named pipe is flushed at the end of each request and before shutting down the application. If the named pipe is flushed, the value is set to true. The default value is false. This property applies only when the named-pipe protocol is used.
Protocol
Specifies the protocol to use to communicate with the FastCGI process. The possible values for the Protocol property are 0 (NamedPipe) or 1 (TCP). The default value is 0 (NamedPipe).
Idle Timeout
Specifies the time interval that a FastCGI process for the application can remain idle, after which an idle process ends. The default value is 300 (seconds).
Queue Length
Specifies the maximum number of requests that are queued for the FastCGI application pool. When the queue is full, subsequent requests return the HTTP error code 503 (Service Unavailable) to clients. This error code indicates that the application is too busy. The default value is 1000.
Rapid Fails PerMinute
Specifies the number of FastCGI process failures allowed in a minute before the FastCGI handler takes the process offline. The default value is 10.
Request Timeout
Specifies the maximum time allowed for a request to the application. If a FastCGI process takes longer than the specified time on a single request, it is terminated. The default value is 90 seconds.

Environment Variables Collection Editor

Environment variables are strings that contain information such as file name or path. These variables control the behavior of various programs on a computer.
When you add or edit a FastCGI application, you can define values for the application properties. For example, you can define values for environment variables. These environment variables are set by the FastCGI module during the start time of a FastCGI process. The environment variables are set for the FastCGI process only and are not seen by any other processes that are running on that computer. Click the Browse () button when you set the EnvironmentVariables property to open the EnvironmentVariables Collection Editor dialog box. This lets you specify optional environment variables that are set in the FastCGI executable. If any of the supplied variable names collide with existing server variables, the ones specified in this property are used.

 

Element NameDescription
Members
Contains a list of the members in the environment variables collection. Each member consists of a name-value pair. To add a member, type a name and value in the Name Properties pane, and then click Add. To remove a member, highlight the member in the list, and then click Remove. To change the order in which these members are stored in the ApplicationHost.config file, use the Up and Down arrows. Because all name-value pairs are unique, the order will not affect functionality.
Name
Type a unique name for the member.
Value
Type a value for the member. This information is stored with the corresponding name in the ApplicationHost.config file.
Sort icons
Use these icons to sort the name-value collection pairs by category, property pages, or alphabetically.

Overview

The FastCGI <application> element contains the configurations settings for a specific FastCGI process pool definition. When FastCGI is used, IIS will use the value in the maxInstances attribute of the <application> element to determine the number of FastCGI processes that can run in the process pool.
The simplest FastCGI application pool definition requires that you include the fullPath attribute in an <application> element to define the binaries to use for the specific application framework. For the PHP framework, set the fullPath attribute to the directory and file name of the PHP binaries.
Other attributes are optional and let you configure a number of other parameters. These include the maximum number of requests that a FastCGI worker process can process before it is recycled, the maximum number of worker processes for an application pool, and the maximum amount of time a FastCGI request can take.
Note: In order to process requests by using FastCGI, you must first map the path and the specific file name extensions to the FastCGI module and specify the target FastCGI process as the script processor.

NEW IN IIS 7.5

IIS 7.5 added the following attributes to the <fastCgi>/<application> element:
  • The monitorChangesTo attribute lets you specify a file to monitor for changes that might affect the behavior of a FastCGI application. For example, if you use the PHP framework, you can configure IIS to monitor the PHP.INI file for changes by specifying the full path to the file in the monitorChangesTo attribute.
  • The stderrMode attribute specifies how IIS 7 will handle errors that are returned from a FastCGI application through the STDERR stream.
  • The signalBeforeTerminateSeconds attribute lets you configure IIS to wait for a specified period of time after IIS signals a FastCGI application that it needs to shut down. This lets a FastCGI application clean up any settings before IIS terminates the process.

Compatibility

VersionNotes
IIS 8.5The <application> element was not modified in IIS 8.5.
IIS 8.0The default value of the maxInstances attribute was changed from 4 to 0 in IIS 8.0.
IIS 7.5The <application> element of the <fastCgi> collection was updated in IIS 7.5 with the monitorChangesTostderrMode, and signalBeforeTerminateSeconds attributes.
IIS 7.0The <application> element of the <fastCgi> collection was introduced in IIS 7.0.
IIS 6.0N/A

Setup

To use the FastCGI environment, you must first install the CGI role service. The installation process registers the Common Gateway Interface (CGI) role service in both the <globalModules> and<modules> elements. You do not need to perform any other steps to install the FastCGI environment.

WINDOWS SERVER 2012 OR WINDOWS SERVER 2012 R2

  1. On the taskbar, click Server Manager.
  2. In Server Manager, click the Manage menu, and then click Add Roles and Features.
  3. In the Add Roles and Features wizard, click Next. Select the installation type and click Next. Select the destination server and click Next.
  4. On the Server Roles page, expand Web Server (IIS), expand Web Server, expandApplication Development, and then select CGI. Click Next.
    .
  5. On the Select Features page, click Next.
  6. On the Confirm installation selections page, click Install.
  7. On the Results page, click Close.

WINDOWS 8 OR WINDOWS 8.1

  1. On the Start screen, move the pointer all the way to the lower left corner, right-click theStart button, and then click Control Panel.
  2. In Control Panel, click Programs and Features, and then click Turn Windows features on or off.
  3. Expand Internet Information Services, expand World Wide Web Services, expandApplication Development Features, and then select CGI.
  4. Click OK.
  5. Click Close.

WINDOWS SERVER 2008 OR WINDOWS SERVER 2008 R2

  1. On the taskbar, click Start, point to Administrative Tools, and then click Server Manager.
  2. In the Server Manager hierarchy pane, expand Roles, and then click Web Server (IIS).
  3. In the Web Server (IIS) pane, scroll to the Role Services section, and then click Add Role Services.
  4. On the Select Role Services page of the Add Role Services Wizard, select CGI, and then clickNext.
  5. On the Confirm Installation Selections page, click Install.
  6. On the Results page, click Close.

WINDOWS VISTA OR WINDOWS 7

  1. On the taskbar, click Start, and then click Control Panel.
  2. In Control Panel, click Programs and Features, and then click Turn Windows Features on or off.
  3. Expand Internet Information Services, then select CGI, and then click OK.

How To

Note for IIS 7.0 users: Some of the steps in this section may require that you install the Microsoft Administration Pack for IIS 7.0, which includes a user interface for FastCGI settings. To install the Microsoft Administration Pack for IIS 7.0, see the following URL:

HOW TO CREATE A GLOBAL FASTCGI HANDLER MAPPING FOR PHP

Note: Before the following procedure will work, you must have already installed binaries that will execute the file path or file name extension that you specify. This example uses a PHP implementation available from www.php.net.
  1. Open Internet Information Services (IIS) Manager:
    • If you are using Windows Server 2012 or Windows Server 2012 R2:
      • On the taskbar, click Server Manager, click Tools, and then click Internet Information Services (IIS) Manager.
    • If you are using Windows 8 or Windows 8.1:
      • Hold down the Windows key, press the letter X, and then click Control Panel.
      • Click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
    • If you are using Windows Server 2008 or Windows Server 2008 R2:
      • On the taskbar, click Start, point to Administrative Tools, and then clickInternet Information Services (IIS) Manager.
    • If you are using Windows Vista or Windows 7:
      • On the taskbar, click Start, and then click Control Panel.
      • Double-click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
  2. In the Connections pane, click the server name for which you want to configure FastCGI handler mappings.
  3. In the Home pane, double-click Handler Mappings.
  4. In the Actions pane, click Add Module Mapping...
  5. Enter the following information:
    • Enter the file name extension in the Request path text box. For example, "*.php".
    • Click FastCgiModule in the Module drop-down list.
    • Enter the path to the scripting engine in the Executable box. For example, "C:\PHP\php-cgi.exe".
    • Enter a unique name for the handler mapping in the Name box. For example, "PHP-FastCGI".

  6. Click Request Restrictions...
    • Select Invoke handler only if request is mapped to.
    • Click to select the File or Folder option.
    • Click OK.
  7. Click OK.
  8. If you are prompted to create a FastCGI application in the Add Module Mapping dialog box, click Yes.
    Note: This will automatically create a FastCGI application for the handler mapping that you just created. If you choose No, you can manually create a FastCGI application later.

HOW TO CREATE A FASTCGI APPLICATION FOR PHP

Note: Before the following procedure will work, you must have already installed binaries that will execute the file path or file name extension that you specify. This example uses a PHP implementation available from www.php.net.
Note: Adding a FastCGI application does not automatically add a handler mapping. To do so, you would need to follow the steps in the How to create a FastCGI handler mapping for PHP section of this topic.
  1. Open Internet Information Services (IIS) Manager:
    • If you are using Windows Server 2012 or Windows Server 2012 R2:
      • On the taskbar, click Server Manager, click Tools, and then click Internet Information Services (IIS) Manager.
    • If you are using Windows 8 or Windows 8.1:
      • Hold down the Windows key, press the letter X, and then click Control Panel.
      • Click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
    • If you are using Windows Server 2008 or Windows Server 2008 R2:
      • On the taskbar, click Start, point to Administrative Tools, and then clickInternet Information Services (IIS) Manager.
    • If you are using Windows Vista or Windows 7:
      • On the taskbar, click Start, and then click Control Panel.
      • Double-click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
  2. In the Connections pane, click the server name for which you want to configure FastCGI settings.
  3. In the Home pane, double-click FastCGI Settings.
  4. In the Actions pane, click Add Application...
  5. In the Add FastCGI Application dialog box, enter the following information:
    • Enter the path to the scripting engine in the Full Path box. For example, "C:\PHP\php-cgi.exe".
    • Enter the maximum number of requests for the FastCGI application.
      Note
      : For PHP, this number must be lesser or equal to the maximum number of requests for PHP environment, which is configured by using the PHP_FCGI_MAX_REQUESTS environment variable.

  6. Click the ellipsis (...) next to EnvironmentVariables to open the environment variables collection editor.
  7. In the EnvironmentVariables Collection Editor dialog box, click Add.
  8. Enter the following information, and then click OK.
    • Enter "PHP_FCGI_MAX_REQUESTS" for the Name.
    • Enter "10000" for the Value.

  9. Click OK to close the Add FastCGI Application dialog box.

HOW ADD A FASTCGI ENVIRONMENT VARIABLE FOR PHP

Note: Before the following procedure will work, you must have already installed binaries that will execute the file path or file name extension that you specify. This example uses a PHP implementation available from www.php.net.
  1. Open Internet Information Services (IIS) Manager:
    • If you are using Windows Server 2012 or Windows Server 2012 R2:
      • On the taskbar, click Server Manager, click Tools, and then click Internet Information Services (IIS) Manager.
    • If you are using Windows 8 or Windows 8.1:
      • Hold down the Windows key, press the letter X, and then click Control Panel.
      • Click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
    • If you are using Windows Server 2008 or Windows Server 2008 R2:
      • On the taskbar, click Start, point to Administrative Tools, and then clickInternet Information Services (IIS) Manager.
    • If you are using Windows Vista or Windows 7:
      • On the taskbar, click Start, and then click Control Panel.
      • Double-click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
  2. In the Connections pane, click the server name for which you want to configure FastCGI settings.
  3. In the Home pane, double-click FastCGI Settings.
  4. Highlight the PHP application in the FastCGI Settings pane, then click Edit... in the Actionspane
  5. In the Edit FastCGI Application dialog box, click the ellipsis (...) next toEnvironmentVariables to open the environment variables collection editor.
  6. In the EnvironmentVariables Collection Editor dialog box, click Add.
  7. Enter the following information, and then click OK.
    • Enter "PHP_FCGI_MAX_REQUESTS" for the Name.
    • Enter "10000" for the Value.

  8. Click OK to close the Add FastCGI Application dialog box.

Configuration

You can configure the <application> element at the server level in the ApplicationHost.config file or at the site level, application level, or directory level in a Web.config file.

ATTRIBUTES

AttributeDescription
activityTimeoutOptional uint attribute.

Specifies the maximum time, in seconds, that a FastCGI process can take to process. Acceptable values are in the range from 10 through 3600.

The default value in IIS 7.0 is 30; the default for IIS 7.5 is 70.
argumentsOptional string attribute.

Specifies the command-line arguments for the FastCGI process.
flushNamedPipeOptional Boolean attribute.

Specifies whether named pipes are flushed at the end of each request. This property applies only when the named-pipe protocol is used.

The default value is false.
fullPathRequired string attribute.

Specifies the full path of the FastCGI process.
idleTimeoutOptional uint attribute.

Specifies the maximum amount of time, in seconds, that a FastCGI process can be idle before the process is shut down. Acceptable values are in the range from 10 through 604800.

The default value is 300.
instanceMaxRequestsOptional uint attribute.

Specifies the maximum number of requests that can be processed by each FastCGI worker process before it is recycled. Acceptable values are in the range from 1 through 10000000.

The default value is 200.
maxInstancesOptional uint attribute.

Specifies the maximum number of FastCGI worker processes that can be started in an application pool. Acceptable values are in the range from 0 through 10000.

The default value is 0.
monitorChangesToOption string attribute.

Specifies the full path to a file to monitor for changes. When changes to the file are detected, IIS will terminate and restart the FastCGI application that is specified in the fullPathattribute.

Note: This attribute was added in IIS 7.5.

There is no default value.
protocolOptional enum attribute.

Specifies the transport mechanism that is used to communicate with the FastCGI process.

The protocol attribute can be one of the following possible values:
ValueDescription
NamedPipeThe named-pipe protocol will be used to communicate with the FastCGI worker process.
TcpThe TCP protocol will be used to communicate with the FastCGI worker process.
The default value is NamedPipe.
queueLengthOptional uint attribute.

Specifies the maximum number of requests that can be queued for a FastCGI process pool. Acceptable values are in the range from 1 through 10000000.

The default value is 1000.
requestTimeoutOptional uint attribute.

Specifies the maximum time, in seconds, that a FastCGI process request can take. Acceptable values are in the range from 10 through 604800.

The default value is 90.
signalBeforeTerminateSecondsOptional uint attribute.

Specifies the amount of time that IIS will wait after IIS signals a FastCGI application that it needs to shut down. When this attribute is set to a value greater than zero, IIS will create an inheritable event and pass the handle value for the event to a FastCGI process as value of _FCGI_SHUTDOWN_EVENT_environment variable.

Note: This attribute was added in IIS 7.5.

The default value is 0.
stderrModeOptional enum attribute.

Specifies the behavior that IIS will use for errors that FastCGI applications return in the STDERR stream.

Note: This attribute was added in IIS 7.5.

The stderrMode attribute can be one of the following possible values:
ValueDescription
ReturnStdErrIn500Specifies that IIS will return an HTTP 500 error and display the content in the STDERR stream to the client. (Note: This matches the IIS 7.0 behavior.)

The numeric value is 0.
ReturnGeneric500Specifies that IIS will return a generic HTTP 500 error to the client. This prevents the content in the STDERR stream from being returned to the client.

The numeric value is 1.
IgnoreAndReturn200Specifies that IIS will ignore the text in the STDERR stream and return an HTTP 200 error, and return the content of the STDOUT stream to the client.

The numeric value is 2.
TerminateProcessSpecifies that IIS will terminate the FastCGI process and return a generic HTTP 500 error to the client.

The numeric value is 3.
The default value is ReturnStdErrIn500.

CHILD ELEMENTS

ElementDescription
environmentVariablesOptional element.

Specifies a collection of environment variables that IIS will pass to the FastCGI application.

CONFIGURATION SAMPLE

The following configuration sample adds a FastCGI application for PHP, sets theinstanceMaxRequests attribute to "10000," and sets the PHP_FCGI_MAX_REQUESTS environment variable to "10000."
<fastCgi>
   <application fullPath="C:\PHP\php-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: For PHP, the value of the PHP_FCGI_MAX_REQUESTS environment variable must be a number that is lesser or equal to the value specified by the instanceMaxRequests attribute of the FastCGI <application> element.
The following configuration sample adds a global FastCGI handler mapping for PHP files to IIS and specifies the full path to a FastCGI executable file.
<configuration>
   <location path="" overrideMode="Allow">
      <system.webServer>
         <handlers>
            <add name="PHP-FastCGI"
                path="*.php"
                verb="GET,HEAD,POST"
                modules="FastCgiModule"
                scriptProcessor="C:\PHP\php-cgi.exe"
                resourceType="Either"
                requireAccess="Script" />
         </handlers>
      </system.webServer>
   </location>
</configuration>
Note: FastCGI settings can be configured per-site through the use of specially formatted FastCGI application and by handler mappings, which are distinguished by matching the fullPath andarguments attributes for an <application> element with the corresponding scriptProcessorattribute in the <handlers> mapping for the FastCGI process. To create these mappings, the settings in the fullPath and arguments attributes for an <application> element must be added to the scriptProcessor attribute in the <handlers> mapping for the FastCGI process and separated by the pipe "|" character. This combination serves as a unique key for each FastCGI application. For example, the following configuration excerpt shows two example <fastCgi>/<application> elements and their corresponding <handlers> mappings for two site-specific PHP handlers:
<fastCgi>
   <application fullPath="C:\PHP\php-cgi.exe"
      arguments="-d open_basedir=C:\Websites\Website1" />
   <application fullPath="C:\PHP\php-cgi.exe"
      arguments="-d open_basedir=C:\Websites\Website2" />
</fastCgi>

<system.webServer>
   <handlers accessPolicy="Read, Script"> 
      <add name="PHP via FastCGI 1"
         path="*.php"
         verb="*"
         modules="FastCgiModule"
         scriptProcessor="C:\PHP\php-cgi.exe|-d open_basedir=C:\Websites\Website1"
         resourceType="Unspecified"
         requireAccess="Script" />
      <add name="PHP via FastCGI 2"
         path="*.php"
         verb="*"
         modules="FastCgiModule"
         scriptProcessor="C:\PHP\php-cgi.exe|-d open_basedir=C:\Websites\Website2"
         resourceType="Unspecified"
         requireAccess="Script" />
</handlers>
</system.webServer>
Additional information and examples about how to configure FastCGI settings for PHP are discussed in the following walkthrough:

Sample Code

The following code samples add a FastCGI application for PHP, set the instanceMaxRequestsattribute to "10000," and set the PHP_FCGI_MAX_REQUESTS environment variable to "10000." Each sample also adds a global FastCGI handler mapping for PHP files to IIS and specifies the full path to a FastCGI executable file.

APPCMD.EXE

appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:\PHP\php-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:\PHP\php-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:\PHP\php-cgi.exe',resourceType='Either',requireAccess='Script']" /commit:apphost
Note: You must be sure to set the commit parameter to apphost when you use AppCmd.exe to configure these settings. This commits the configuration settings to the appropriate location section in the ApplicationHost.config file.

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();

         ConfigurationSection fastCgiSection = config.GetSection("system.webServer/fastCgi");
         ConfigurationElementCollection fastCgiCollection = fastCgiSection.GetCollection();
         ConfigurationElement applicationElement = fastCgiCollection.CreateElement("application");
         applicationElement["fullPath"] = @"C:\PHP\php-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;

         ConfigurationElementCollection environmentVariablesCollection = applicationElement.GetCollection("environmentVariables");
         ConfigurationElement environmentVariableElement = environmentVariablesCollection.CreateElement("environmentVariable");
         environmentVariableElement["name"] = @"PHP_FCGI_MAX_REQUESTS";
         environmentVariableElement["value"] = @"10000";
         environmentVariablesCollection.Add(environmentVariableElement);
         fastCgiCollection.Add(applicationElement);

         ConfigurationSection handlersSection = config.GetSection("system.webServer/handlers");
         ConfigurationElementCollection handlersCollection = handlersSection.GetCollection();
         ConfigurationElement addElement = handlersCollection.CreateElement("add");
         addElement["name"] = @"PHP-FastCGI";
         addElement["path"] = @"*.php";
         addElement["verb"] = @"GET,HEAD,POST";
         addElement["modules"] = @"FastCgiModule";
         addElement["scriptProcessor"] = @"C:\PHP\php-cgi.exe";
         addElement["resourceType"] = @"Either";
         addElement["requireAccess"] = @"Script";
         handlersCollection.AddAt(0, addElement);

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration

      Dim fastCgiSection As ConfigurationSection = config.GetSection("system.webServer/fastCgi")
      Dim fastCgiCollection As ConfigurationElementCollection = fastCgiSection.GetCollection
      Dim applicationElement As ConfigurationElement = fastCgiCollection.CreateElement("application")
      applicationElement("fullPath") = "C:\PHP\php-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

      Dim environmentVariablesCollection As ConfigurationElementCollection = applicationElement.GetCollection("environmentVariables")
      Dim environmentVariableElement As ConfigurationElement = environmentVariablesCollection.CreateElement("environmentVariable")
      environmentVariableElement("name") = "PHP_FCGI_MAX_REQUESTS"
      environmentVariableElement("value") = "10000"
      environmentVariablesCollection.Add(environmentVariableElement)
      fastCgiCollection.Add(applicationElement)

      Dim handlersSection As ConfigurationSection = config.GetSection("system.webServer/handlers")
      Dim handlersCollection As ConfigurationElementCollection = handlersSection.GetCollection
      Dim addElement As ConfigurationElement = handlersCollection.CreateElement("add")
      addElement("name") = "PHP-FastCGI"
      addElement("path") = "*.php"
      addElement("verb") = "GET,HEAD,POST"
      addElement("modules") = "FastCgiModule"
      addElement("scriptProcessor") = "C:\PHP\php-cgi.exe"
      addElement("resourceType") = "Either"
      addElement("requireAccess") = "Script"
      handlersCollection.AddAt(0, addElement)

      serverManager.CommitChanges()
   End Sub

End Module

No comments:

Post a Comment