FastCGI <fastCgi>

  • 概要
  • 互換性
  • セットアップ
  • 方法
  • 構成
  • サンプル コード

※本ページに挿入されている画像をクリックすると、画像全体が別ウィンドウで表示されます。

概要

<fastCgi> 要素を使用すると、FastCGI アプリケーションにアプリケーション プール サポートを追加できます。インターネット インフォメーション サービス (IIS) 7.0 には、FastCGI コンポーネントが格納されています。このコンポーネントにより、PHP アプリケーション フレームワークおよびその他の Web アプリケーション フレームワークを IIS 7.0 で実行する上で、より高い信頼性とパフォーマンスが実現します。

<fastCgi> 要素には <application> 要素のコレクションが格納されており、各要素によって FastCGI アプリケーション プール定義が作成されます。最も単純な構成では、特定のアプリケーション フレームワークに対して使用するバイナリを定義するために、<application> 要素の中に fullPath 属性を組み込む必要があります。PHP フレームワークの場合、fullPath 属性は PHP バイナリのディレクトリとファイル名に設定します。

その他の属性はオプションです。これらのオプションにより、FastCGI ワーカー プロセスがリサイクルされるまでに処理できる要求の最大数、1 つのアプリケーション プールに対するワーカー プロセスの最大数、および FastCGI 要求が費やすことができる最大時間など、さまざまなパラメーターを構成できます。

: FastCGI を使用して要求を処理するためには、まずパスと特定のファイル名拡張子を FastCGI モジュールにマップし、ターゲットの FastCGI プロセスをスクリプト プロセッサーとして指定する必要があります。

互換性

  IIS 7.0 IIS 6.0
注意 <fastCgi> は IIS 7.0 で新たに導入された要素です。 なし

セットアップ

FastCGI 環境を使用するには、まず CGI 役割サービスをインストールする必要があります。インストール プロセスにより、<globalModules> および <modules> の両方の要素で CGI (Common Gateway Interface) 役割サービスが登録されます。FastCGI 環境をインストールするために他の手順を実行する必要はありません。

Windows Server 2008

  1. タスク バーで [スタート] ボタンをクリックし、[管理ツール] をポイントして [サーバー マネージャー] をクリックします。

  2. [サーバー マネージャー] ウィンドウのツリー表示で、[役割] を展開して [Web サーバー (IIS)] をクリックします。

  3. [Web サーバー (IIS)] ウィンドウで、[役割サービス] セクションまでスクロールして [役割サービスの追加] をクリックします。

  4. 役割サービスの追加ウィザードの [役割サービスの選択] ページで、[CGI] を選択して、[次へ] をクリックします。

    拡大

  5. [インストール オプションの確認] ページで [インストール] をクリックします。

  6. [結果] ページで [閉じる] をクリックします。

Windows Vista

  1. タスク バーで [スタート] ボタンをクリックし、[コントロール パネル] をクリックします。

  2. コントロール パネルで、[プログラムと機能][Windows の機能の有効化または無効化] の順にクリックします。

  3. [Internet Information Services] を展開し、[CGI] を選択して [OK] をクリックします。

    拡大

方法

: このセクションの手順の一部では、FastCGI 設定用のユーザー インターフェイスが含まれている Microsoft Administration Pack for IIS 7.0 のインストールを求められる場合があります。Microsoft Administration Pack for IIS 7.0 をインストールするには、次の URL を参照してください。

http://learn.iis.net/page.aspx/415/

PHP 用のグローバル FastCGI ハンドラー マッピングを作成する方法

: このセクションの手順の操作を行うには、指定するファイル名またはファイル名拡張子を実行するバイナリを既にインストールしている必要があります。この例では、www.php.net (英語) で入手できる PHP 実装を使用します。

  1. タスク バーで [スタート] ボタンをクリックし、[管理ツール] をポイントして [インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。

  2. [接続] ウィンドウで、FastCGI ハンドラー マッピングを構成するサーバー名をクリックします。

  3. [ホーム] ウィンドウで [ハンドラー マッピング] をダブルクリックします。

    拡大

  4. [操作] ウィンドウで [モジュール マップの追加] をクリックします。

    拡大

  5. 次の情報を入力し、[OK] をクリックします。

    • [要求パス] テキスト ボックスにファイル名拡張子を入力します。例 : 「*.php」
    • [モジュール] ドロップダウン リストで [FastCgiModule] をクリックします。
    • [実行可能ファイル] ボックスに、スクリプト エンジンへのパスを入力します。例 : 「C:\PHP\php-cgi.exe」
    • [名前] ボックスに、ハンドラー マッピングの一意の名前を入力します。例 : 「PHP-FastCGI」

    拡大

  6. [モジュール マップの追加] ダイアログ ボックスで、FastCGI アプリケーションを作成するかどうかを確認するプロンプトが表示されたら、[はい] をクリックします。

    拡大

    : これによって、作成したハンドラー マッピングに対する FastCGI アプリケーションが自動的に作成されます。[いいえ] をクリックすると、Fast CGI アプリケーションを後で手動で作成できます。

PHP 用の FastCGI アプリケーションを作成する方法

: このセクションの手順の操作を行うには、指定するファイル パスまたはファイル名拡張子を実行するバイナリを既にインストールしている必要があります。この例では、www.php.net (英語) で入手できる PHP 実装を使用します。

: FastCGI アプリケーションを追加しても、ハンドラー マッピングは自動的に追加されません。ハンドラー マッピングを追加するには、このトピックの 「PHP 用のグローバル FastCGI ハンドラー マッピングを作成する方法」の手順に従ってください。

  1. タスク バーで [スタート] ボタンをクリックし、[管理ツール] をポイントして [インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。

  2. [接続] ウィンドウで、FastCGI 設定を構成するサーバー名をクリックします。

  3. [ホーム] ウィンドウで [FastCGI Settings] をダブルクリックします。

    拡大

  4. [操作] ウィンドウで [アプリケーションの追加] をクリックします。

    拡大

  5. [Add FastCGI Application] ダイアログ ボックスで、次の情報を入力します。

    • [完全なパス] ボックスに、スクリプト エンジンへのパスを入力します。例 : 「C:\PHP\php-cgi.exe」
    • FastCGI アプリケーションに対する要求の最大数を入力します。**
      注** : PHP の場合、この数字は、PHP 環境に対する要求の最大数以下に設定する必要があります。PHP 環境に対する要求の最大数は、PHP_FCGI_MAX_REQUESTS 環境変数を使用して構成されます。

    拡大

  6. [EnvironmentVariables] の横にある省略記号をクリックして、環境変数コレクション エディターを開きます。

  7. [EnvironmentVariables Collection Editor] ダイアログ ボックスで、[追加] をクリックします。

    拡大

  8. 次の情報を入力し、[OK] をクリックします。

    • [名前] に「PHP_FCGI_MAX_REQUESTS」と入力します。
    • [値] に「10000」と入力します。

    拡大

  9. [OK] をクリックして、[Add FastCGI Application] ダイアログ ボックスを閉じます。

PHP 用の FastCGI 環境変数を追加する方法

: このセクションの手順の操作を行うには、指定するファイル パスまたはファイル名拡張子を実行するバイナリを既にインストールしている必要があります。この例では、www.php.net (英語) で入手できる PHP 実装を使用します。

  1. タスク バーで [スタート] ボタンをクリックし、[管理ツール] をポイントして [インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。

  2. [接続] ウィンドウで、FastCGI 設定を構成するサーバー名をクリックします。

  3. [ホーム] ウィンドウで [FastCGI Settings] をダブルクリックします。

    拡大

  4. [FastCGI Settings] ウィンドウで PHP アプリケーションを強調表示し、[操作] ウィンドウで [編集] をクリックします。

    拡大

  5. [Edit FastCGI Application] ダイアログ ボックスで、[EnvironmentVariables] の横にある省略記号をクリックして、環境変数コレクション エディターを開きます。

    拡大

  6. [EnvironmentVariables Collection Editor] ダイアログ ボックスで、[追加] をクリックします。

    拡大

  7. 次の情報を入力し、[OK] をクリックします。

    • [名前] に「PHP_FCGI_MAX_REQUESTS」と入力します。
    • [値] に「10000」と入力します。

    拡大

  8. [OK] をクリックして、[Add FastCGI Application] ダイアログ ボックスを閉じます。

構成

サーバー レベルでの <fastCgi> 要素の構成は ApplicationHost.config ファイルで、サイト レベル、アプリケーション レベル、またはディレクトリ レベルでのこの要素の構成は Web.config ファイルで、それぞれ設定できます。

属性

なし。

子要素

要素 説明
application オプションの要素。

FastCGI プロセス プール定義を、FastCGI プロセス プール定義のコレクションに追加します。

構成サンプル

次の構成サンプルでは、PHP 用の FastCGI アプリケーションを追加し、instanceMaxRequests 属性を "10000" に、PHP_FCGI_MAX_REQUESTS 環境変数を "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>

: PHP の場合、PHP_FCGI_MAX_REQUESTS 環境変数の値は、FastCGI <application> 要素の instanceMaxRequests 属性によって指定される値以下に設定する必要があります。

次の構成サンプルでは、PHP ファイル用のグローバル FastCGI ハンドラー マッピングを IIS に追加し、FastCGI 実行可能ファイルへの完全なパスを指定しています。

<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="Unspecified"
                requireAccess="Script" />

         </handlers>
      </system.webServer>
   </location>
</configuration>

サンプル コード

次のコード サンプルでは、PHP 用の FastCGI アプリケーションを追加し、instanceMaxRequests 属性を "10000"に、PHP_FCGI_MAX_REQUESTS 環境変数を "10000" に設定しています。また、どちらのサンプルでも、PHP ファイル用のグローバル FastCGI ハンドラー マッピングを IIS に追加し、FastCGI 実行可能ファイルへの完全なパスを指定しています。

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='Unspecified',requireAccess='Script']" /commit:apphost

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"] = @"Unspecified";
         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") = "Unspecified"
      addElement("requireAccess") = "Script"
      handlersCollection.AddAt(0, addElement)

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";

var fastCgiSection = adminManager.GetAdminSection("system.webServer/fastCgi", "MACHINE/WEBROOT/APPHOST");
var fastCgiCollection = fastCgiSection.Collection;
var applicationElement = 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;

var environmentVariablesCollection = applicationElement.ChildElements.Item("environmentVariables").Collection;
var environmentVariableElement = environmentVariablesCollection.CreateNewElement("environmentVariable");
environmentVariableElement.Properties.Item("name").Value = "PHP_FCGI_MAX_REQUESTS";
environmentVariableElement.Properties.Item("value").Value = "10000";
environmentVariablesCollection.AddElement(environmentVariableElement);
fastCgiCollection.AddElement(applicationElement);

var handlersSection = adminManager.GetAdminSection("system.webServer/handlers", "MACHINE/WEBROOT/APPHOST");
var handlersCollection = handlersSection.Collection;
var addElement = 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 = "Unspecified";
addElement.Properties.Item("requireAccess").Value = "Script";
handlersCollection.AddElement(addElement, 0);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"

Set fastCgiSection = adminManager.GetAdminSection("system.webServer/fastCgi", "MACHINE/WEBROOT/APPHOST")
Set fastCgiCollection = fastCgiSection.Collection
Set applicationElement = 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

Set environmentVariablesCollection = applicationElement.ChildElements.Item("environmentVariables").Collection
Set environmentVariableElement = environmentVariablesCollection.CreateNewElement("environmentVariable")
environmentVariableElement.Properties.Item("name").Value = "PHP_FCGI_MAX_REQUESTS"
environmentVariableElement.Properties.Item("value").Value = "10000"
environmentVariablesCollection.AddElement(environmentVariableElement)
Call fastCgiCollection.AddElement(applicationElement)

Set handlersSection = adminManager.GetAdminSection("system.webServer/handlers", "MACHINE/WEBROOT/APPHOST")
Set handlersCollection = handlersSection.Collection
Set addElement = 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 = "Unspecified"
addElement.Properties.Item("requireAccess").Value = "Script"
Call handlersCollection.AddElement(addElement, 0)

adminManager.CommitChanges()