IIS7 では、Web サイトごとにカスタム環境変数を設定できますか?

IIS7 では、Web サイトごとにカスタム環境変数を設定できますか?

会社のイントラネット用に IIS7.5 を設定しています。サーバーは「テスト」サイトと「本番」サイトの両方をホストします。サイトごとにカスタマイズしたい環境変数は「PERL5LIB」です。

この Web サーバーに Perl CGI (FastCGI ではない) 環境を含めるようにします。(ActiveState Perl v5.16、PerlIs.dll および PerlEx30.dll を使用)。この Perl CGI 環境に 'test' モジュールと 'prod' モジュールの両方を含めて、'test' サイトにアクセスするとテスト モジュールが読み込まれるようにします。同様に、'prod' サイトにアクセスすると prod モジュールが読み込まれます。サイトごとに PERL5LIB を設定することが重要です。

Apache は、サイトの URL に関連付けられた SetEnv ディレクティブを使用してこれを実行します。

答え1

もちろん可能です。2 つの異なるアプリケーション プールを使用し、それらを異なるユーザー アカウントで実行し、ユーザー ベースの環境変数を設定します。

次の PowerShell スクリプトは、これを行う方法を示しています。このページでは ASP.NET を使用していますが、Perl でも同じことができるはずです。スクリプトを使用するには、IIS PowerShell スクリプトも有効にする必要があります。

 Import-Module WebAdministration

 Function Prepare([string]$name,[int]$port)
 {
     # create a new directory for the site
     md c:\inetpub\site$name

     # create a new application pool
     New-WebAppPool "pool$name"

     # create a new site using the folder and pool we just created
     New-WebSite -name "site$name" -port $port -physicalpath "c:\inetpub\site$name" -applicationpool "pool$name"

     # Make sure the pool runs as applicationpoolidentity and loads its user profile
     set-webconfigurationproperty -pspath 'machine/webroot/apphost'  -filter "system.applicationhost/applicationpools/add[@name='pool$name']/processmodel" -name "identitytype" -value "applicationpoolidentity"
     set-webconfigurationproperty -pspath 'machine/webroot/apphost'  -filter "system.applicationhost/applicationpools/add[@name='pool$name']/processmodel" -name "loaduserprofile" -value "true"

     # create two pages, one to show the environment variable, the other to set it.
     "<%@ page %><html># <% response.write(system.environment.getenvironmentvariable(`"myvar`")) %> #</html>" | out-file  "c:\inetpub\site$name\default.aspx"
     "<%@ page %><%  system.environment.setenvironmentvariable(`"myvar`", `"i am site $name`", system.environmentvariabletarget.user) %>" | out-file "c:\inetpub\site$name\setenv.aspx"

     # hit the home page, just to get it started
     (new-object net.webclient).DownloadString("http://localhost:$port")
     # set our environment variable
     (new-object net.webclient).DownloadString("http://localhost:$port/setenv.aspx")
     # recycle the pool
     Restart-WebAppPool -Name "Pool$name"
     # wait a little bit to restart
     Start-Sleep -Milliseconds 500
     # hit the home page again to show our variable
     (new-object net.webclient).DownloadString("http://localhost:$port")
 }

 # call the function for two sites
 Prepare A 81
 Prepare B 82   

私はこれを 2012 R2 でのみテストしましたが、2008 R2 でも問題なく実行されるはずです。スクリプトを使用する必要はなく、GUI で同じ手順を実行できます。

関連情報