
我正在為公司內部網路設定 IIS7.5。該伺服器將託管“測試”和“產品”網站。我想要為每個網站自訂的環境變數是“PERL5LIB”。
我希望這個 Web 伺服器包含 Perl CGI(不是 FastCGI)環境。 (ActiveState Perl v5.16,使用 PerlIs.dll 和 PerlEx30.dll)。我希望這個 Perl CGI 環境同時具有“測試”和“產品”模組,以便在訪問“測試”網站時可以載入測試模組。同樣,當訪問“prod”網站時,將載入 prod 模組。每個站點設定 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 中執行相同的步驟。