在Azure Cloud Service的Zend Framework 1.12.x应用程序中设置文档根目录 [英] Set document root in Zend Framework 1.12.x application in Azure Cloud Service

查看:118
本文介绍了在Azure Cloud Service的Zend Framework 1.12.x应用程序中设置文档根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于我关于如何为 -x-application-in-azure-web-sites> Azure网站,我想知道如何使用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 &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>

现在我已经尝试直接为网站设置physicalDirectoryPath

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

,并且我尝试使用VirtualApplication,但两者似乎都无法正常工作.

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

有帮助吗?

解决方案

在Blog条目之后,我找到了一个可行的解决方案.我将对其进行总结,以快速了解如何做到这一点:

链接:更改您的Azure网站根文件夹

您必须在 ServiceDefinition.csdef 文件中添加启动任务

<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中移动该站点的physicalPath.这也必须进入您的Web角色中的/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运行时在构建机器上是否比在云主机上新:

  • 在两台计算机上打开Powershell
  • 看看[System.Environment]::Version,或者如果您使用的是Powershell 2.0或更高版本,则可以查看$PSVersionTable变量

如果不这样做,将会遇到的问题-您可能将无法使用任何Azure程序集,例如Microsoft.WindowsAzure.ServiceRuntime.更改网站的物理路径取决于了解网站的名称,而该名称仅在该程序集中(动态)可用.

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.

I've used the Azure Powershell Tools to create a package of my application and succesfully uploaded it to the cloud. But setting document root is not working.

My ServiceDefinition.csdef looks like this:

<?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

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

and I've tried to use a VirtualApplication, but both don't seem to work.

<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

You have to add da startup task in your ServiceDefinition.csdef file

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

This instructs each instance on startup to execute 'changeroot.cmd'. This file hast to go into the /bin directory of your web role instance directory.

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. This also has to go into your /bin path in your web role.

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. 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:

  • open a powershell on both machines
  • take a look at [System.Environment]::Version or if you're using powershell 2.0 or newer, you can look at the $PSVersionTable variable

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. Changing the Physical Path of a web site depends on knowing the web site name and this is only (dynamically) available in that assembly.

这篇关于在Azure Cloud Service的Zend Framework 1.12.x应用程序中设置文档根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆