关于如何设置Azure Web Site的文档根目录的其他问题,我想知道如何对Cloud Service执行相同的操作。
我使用Azure Powershell工具创建了我的应用程序包,并成功将其上传到云端。但是设置文档根目录不起作用。
我的ServiceDefinition.csdef如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="myCloudService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="myWebRole" vmsize="ExtraSmall">
<Imports />
<Startup>
<Task commandLine="setup_web.cmd > log.txt" executionContext="elevated">
<Environment>
<Variable name="EMULATED">
<RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
</Variable>
<Variable name="RUNTIMEVERSIONPRIMARYKEY" value="5.3.17" />
<Variable name="RUNTIMEID" value="PHP" />
<Variable name="RUNTIMEURL" value="http://az413943.vo.msecnd.net/php/5.3.17.exe" />
</Environment>
</Task>
</Startup>
<Endpoints>
<InputEndpoint name="Endpoint1" protocol="http" port="80" />
</Endpoints>
<Sites>
<Site name="Web">
<Bindings>
<Binding name="Endpoint1" endpointName="Endpoint1" />
</Bindings>
</Site>
</Sites>
</WebRole>
</ServiceDefinition>
现在我已经尝试直接为网站设置physicalDirectoryPath
<Sites>
<Site name="Web" physicalDirectory="../htdocs">
...
</Site>
</Sites>
我尝试使用VirtualApplication,但两者似乎都不起作用。
<Sites>
<Site name="Web">
<VirtualApplication name="MyWeb" physicalDirectory="../htdocs" />
...
</Site>
</Sites>
任何帮助?
答案 0 :(得分:1)
在博客条目之后,我发现了一个有效的解决方案。我总结一下,快速了解如何做到这一点:
链接:Change your azure websites root folder
您必须在 ServiceDefinition.csdef 文件中添加da启动任务
<Startup>
<Task commandLine="changeroot.cmd" executionContext="elevated" taskType="background" />
</Startup>
这指示启动时每个实例执行'changeroot.cmd'。该文件必须进入Web角色实例目录的 / bin 目录。
这包含以下代码:
@echo off
cd "%~dp0"
icacls %RoleRoot%\approot /grant "Everyone":F /T
powershell.exe Set-ExecutionPolicy Unrestricted
powershell.exe .\changeroot.ps1
ECHO Changeroot Run. >> ..\startup-tasks-log.txt
这将执行powershell changeroot.ps1 脚本,具有提升的用户权限,一旦创建,将在IIS中移动该站点的物理路径。这也必须进入您的网络角色的 / bin 路径。
它包含以下代码:
$siteName = "Web"
$serverIP = "127.0.0.1"
$newPath = "htdocs" ## your document root
$pathset = $false
$trycount = 0
##loop until physical path has changed
while($pathset -eq $false) {
$trycount += 1
##if the role id can be determined
if([Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment]::CurrentRoleInstance.Id -ne $null)
{
$fullName = [Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment]::CurrentRoleInstance.Id + "_" + $siteName
$op = "Changeroot: Site Full Name: $fullName`r`n"
Write-Output $op | Out-File -Encoding Ascii ..\startup-tasks-log.txt
##init server manager
$serverManager = [Microsoft.Web.Administration.ServerManager]::OpenRemote($serverIP)
if($serverManager -ne $null)
{
$op = "Changeroot: Site Manager Setup`r`n"
Write-Output $op | Out-File -Encoding Ascii ..\startup-tasks-log.txt
##load site
$site = $serverManager.Sites | where { $_.Name -eq $fullName }
if($site -ne $null)
{
$op = "Changeroot: Site loaded ($fullName)`r`n"
Write-Output $op | Out-File -Encoding Ascii ..\startup-tasks-log.txt
##change physical path
$rootApp = $site.Applications | where { $_.Path -eq "/" }
$rootVdir = $rootApp.VirtualDirectories | where { $_.Path -eq "/" }
$dir = $rootVdir.PhysicalPath.EndsWith('\')
if($dir -eq $true) {
$rootVdir.PhysicalPath += $newPath + "\"
} else {
$rootVdir.PhysicalPath += "\" + $newPath + "\"
}
$serverManager.CommitChanges()
$op = "Root changed for $fullName (after $trycount tries)`r`n"
Write-Output $op | Out-File -Encoding Ascii ..\startup-tasks-log.txt
$pathset = $true
exit
}
}
} else {
startup-tasks-log.txt
}
# Restart the loop in 5 seconds
Start-Sleep -Seconds 5
}
这最终对我有用。
有关更详细的说明,请点击链接。
额外的预防措施:
确保使用具有匹配(或较小).net框架作为云主机的构建计算机构建程序包(或使用publish-azureserviceproject
进行部署)。如果在Windows 8.1计算机上进行部署,则可能需要确保将OSFamily设置为4.为了验证您的.net运行时在构建计算机上是否比在云主机上更新:
[System.Environment]::Version
或者如果您使用的是PowerShell 2.0或更新版本,则可以查看$PSVersionTable
变量如果不这样做,您将遇到的问题 - 您可能无法使用任何Azure程序集,如Microsoft.WindowsAzure.ServiceRuntime。更改网站的物理路径取决于了解网站名称,并且该组件中仅(动态)可用。