简体   繁体   English

在Azure Cloud Service的Zend Framework 1.12.x应用程序中设置文档根目录

[英]Set document root in Zend Framework 1.12.x application in Azure Cloud Service

in relation to my other question on how to set the document root for a Azure Web Site , I would like to know how to do the same with a Cloud Service. 关于我关于如何为Azure网站设置文档根目录的其他问题,我想知道如何使用Cloud Service进行同样的操作。

I've used the Azure Powershell Tools to create a package of my application and succesfully uploaded it to the cloud. 我已经使用Azure Powershell工具创建了一个应用程序包,并将其成功上传到云中。 But setting document root is not working. 但是设置文档根目录不起作用。

My ServiceDefinition.csdef looks like this: 我的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 &gt; 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>

Now I've tried setting the physicalDirectoryPath for the site directly 现在,我尝试直接为网站设置physicalDirectoryPath

<Sites>
  <Site name="Web" physicalDirectory="../htdocs">
    ...
  </Site>
</Sites>

and I've tried to use a VirtualApplication, but both don't seem to work. 并且我尝试使用VirtualApplication,但两者似乎都无法正常工作。

<Sites>
  <Site name="Web">
    <VirtualApplication name="MyWeb" physicalDirectory="../htdocs" />
    ...
  </Site>
</Sites>

Any help? 有什么帮助吗?

Following a Blog entry, I found a solution that works. 在博客条目之后,我找到了一个可行的解决方案。 I'll sum it up to give a quick insight on how to do it: 我将对其进行总结,以快速了解如何执行此操作:

Link: Change your azure websites root folder 链接: 更改您的azure网站根文件夹

You have to add da startup task in your ServiceDefinition.csdef file 您必须在ServiceDefinition.csdef文件中添加启动任务

<Startup>
    <Task commandLine="changeroot.cmd" executionContext="elevated" taskType="background" />
</Startup>

This instructs each instance on startup to execute 'changeroot.cmd'. 这指示启动时每个实例执行“ changeroot.cmd”。 This file hast to go into the /bin directory of your web role instance directory. 该文件不必进入Web角色实例目录的/ bin目录。

This contains the following code: 它包含以下代码:

@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

This will execute the powershell changeroot.ps1 script, with elevated user right, that will move the physicalPath of the site in IIS once it is created. 这将以提升的用户权限执行powershell changeroot.ps1脚本,一旦创建,它将在IIS中移动该站点的physicalPath。 This also has to go into your /bin path in your web role. 这也必须进入Web角色的/ bin路径。

It contains the following code: 它包含以下代码:

$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
}

This finally worked for me. 这终于对我有用。

For a more detailed explanation please follow the link. 有关详细说明,请点击链接。

An added precaution: 附加的预防措施:

Make sure you build your package (or deploy using publish-azureserviceproject ) using a build machine that has a matching (or lesser) .net framework as your cloud host. 确保使用具有匹配(或更小).net框架的构建计算机作为云主机来构建软件包(或使用publish-azureserviceproject进行部署)。 If deploying on a Windows 8.1 machine, you may need to make sure your OSFamily is set to 4. In order to verify that your .net runtimes aren't newer on your build machine than they are on your cloud host: 如果在Windows 8.1机器上进行部署,则可能需要确保OSFamily设置为4。为了验证.net运行时在构建机器上是否比在云主机上新:

  • open a powershell on both machines 在两台机器上打开Powershell
  • take a look at [System.Environment]::Version or if you're using powershell 2.0 or newer, you can look at the $PSVersionTable variable 看一下[System.Environment]::Version或者如果您使用的是Powershell 2.0或更高版本,则可以看一下$PSVersionTable变量

The problems you'll run into if you don't do this - you likely won't be able to use any of the Azure Assemblies such as Microsoft.WindowsAzure.ServiceRuntime. 如果不这样做,就会遇到问题-您可能将无法使用任何Azure程序集,例如Microsoft.WindowsAzure.ServiceRuntime。 Changing the Physical Path of a web site depends on knowing the web site name and this is only (dynamically) available in that assembly. 更改网站的物理路径取决于知道网站的名称,而该名称仅(动态)在该程序集中可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM