在 IIS7 中,每个网站是否可以拥有自定义环境变量?

在 IIS7 中,每个网站是否可以拥有自定义环境变量?

我正在为公司内联网设置 IIS7.5。该服务器将托管“测试”和“生产”站点。我想要为每个站点自定义的环境变量是“PERL5LIB”。

我希望此 Web 服务器包含 Perl CGI(不是 FastCGI)环境。(ActiveState Perl v5.16,使用 PerlIs.dll 和 PerlEx30.dll)。我希望此 Perl CGI 环境同时具有“测试”和“生产”模块,这样在访问“测试”站点时可以加载测试模块。同样,在访问“生产”站点时也会加载生产模块。为每个站点设置 PERL5LIB 是关键。

Apache 将使用与站点 URL 关联的 SetEnv 指令执行此操作。

答案1

当然可以,您可以使用两个不同的应用程序池,在不同的用户帐户下运行它们并设置基于用户的环境变量。

以下 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 中执行相同的步骤。

相关内容