直接从 ZIP 包运行 Azure 应用服务中的应用Run your app in Azure App Service directly from a ZIP package

Azure 应用服务中,可以直接从部署 ZIP 包文件运行应用。In Azure App Service, you can run your apps directly from a deployment ZIP package file. 本文将介绍如何在应用中启用此功能。This article shows how to enable this functionality in your app.

应用服务中的所有其他部署方法具有一定的共同之处:文件将部署到应用中的 D:\home\site\wwwrootAll other deployment methods in App Service have something in common: your files are deployed to D:\home\site\wwwroot in your app. 由于应用在运行时使用同一个目录,因此部署可能会因文件锁定冲突而失败,并且应用可能会由于某些文件尚未更新而出现不可预测的行为。Since the same directory is used by your app at runtime, it's possible for deployment to fail because of file lock conflicts, and for the app to behave unpredictably because some of the files are not yet updated.

相反,直接从包运行时,包中的文件不会复制到 wwwroot 目录,In contrast, when you run directly from a package, the files in the package are not copied to the wwwroot directory. 而是将 ZIP 包本身直接装载为只读的 wwwroot 目录。Instead, the ZIP package itself gets mounted directly as the read-only wwwroot directory. 从包直接运行可提供多种好处:There are several benefits to running directly from a package:

  • 消除部署与运行时之间的文件锁定冲突。Eliminates file lock conflicts between deployment and runtime.
  • 确保在任意时候只运行完全部署的应用。Ensures only full-deployed apps are running at any time.
  • 可部署到生产应用(需重启)。Can be deployed to a production app (with restart).
  • 改善 Azure 资源管理器部署的性能。Improves the performance of Azure Resource Manager deployments.
  • 可以减少冷启动时间,特别是对于具有大型 npm 包树的 JavaScript 函数。May reduce cold-start times, particularly for JavaScript functions with large npm package trees.

备注

目前仅支持 ZIP 包文件。Currently, only ZIP package files are supported.

创建一个项目 zip 文件Create a project ZIP file

备注

如果以 zip 文件的形式下载文件,请首先提取该文件。If you downloaded the files in a ZIP file, extract the files first. 例如,如果从 GitHub 下载了 zip 文件,则无法按原样部署该文件。For example, if you downloaded a ZIP file from GitHub, you cannot deploy that file as-is. GitHub 可添加额外的嵌套目录,但这不适用于应用服务。GitHub adds additional nested directories, which do not work with App Service.

在本地终端窗口中,导航到应用项目的根目录。In a local terminal window, navigate to the root directory of your app project.

此目录应包含 Web 应用的入口文件,例如 index.htmlindex.phpapp.jsThis directory should contain the entry file to your web app, such as index.html, index.php, and app.js. 它还可能包含包管理文件,如 project.jsoncomposer.jsonpackage.jsonbower.jsonrequirements.txtIt can also contain package management files like project.json, composer.json, package.json, bower.json, and requirements.txt.

除非你希望应用服务为你运行部署自动化,否则请运行所有生成任务(例如,npmbowergulpcomposerpip),并确保你拥有运行应用所需的所有文件。Unless you want App Service to run deployment automation for you, run all the build tasks (for example, npm, bower, gulp, composer, and pip) and make sure that you have all the files you need to run the app. 如果想要直接运行包,则此步骤是必需的。This step is required if you want to run your package directly.

创建一个包含项目所有内容的 zip 文件。Create a ZIP archive of everything in your project. 对于 dotnet 项目,此文件夹是 dotnet publish 命令的输出文件夹。For dotnet projects, this folder is the output folder of the dotnet publish command. 以下命令使用您终端中的默认工具执行操作:The following command uses the default tool in your terminal:

# Bash
zip -r <file-name>.zip .

# PowerShell
Compress-Archive -Path * -DestinationPath <file-name>.zip

启用从包运行的功能Enable running from package

WEBSITE_RUN_FROM_PACKAGE 应用设置启用从包运行的功能。The WEBSITE_RUN_FROM_PACKAGE app setting enables running from a package. 若要指定此设置,请使用 Azure CLI 运行以下命令。To set it, run the following command with Azure CLI.

az webapp config appsettings set --resource-group <group-name> --name <app-name> --settings WEBSITE_RUN_FROM_PACKAGE="1"

WEBSITE_RUN_FROM_PACKAGE="1" 允许从应用本地的包运行应用。WEBSITE_RUN_FROM_PACKAGE="1" lets you run your app from a package local to your app. 也可以从远程包运行You can also run from a remote package.

运行包Run the package

在应用服务中运行包的最简单方法是使用 Azure CLI az webapp deployment source config-zip 命令。The easiest way to run a package in your App Service is with the Azure CLI az webapp deployment source config-zip command. 例如:For example:

az webapp deployment source config-zip --resource-group <group-name> --name <app-name> --src <filename>.zip

由于已指定 WEBSITE_RUN_FROM_PACKAGE 应用设置,因此,此命令不会将包内容解压缩到应用的 D:\home\site\wwwroot 目录。Because the WEBSITE_RUN_FROM_PACKAGE app setting is set, this command doesn't extract the package content to the D:\home\site\wwwroot directory of your app. 而是将 ZIP 文件按原样上传到 D:\home\data\SitePackages,并在同一目录中创建 packagename.txt,其中包含要在运行时加载的 ZIP 包的名称。Instead, it uploads the ZIP file as-is to D:\home\data\SitePackages, and creates a packagename.txt in the same directory, that contains the name of the ZIP package to load at runtime. 如果以不同的方式(例如使用 FTP)上传 ZIP 包,需要手动创建 D:\home\data\SitePackages 目录和 packagename.txt 文件。If you upload your ZIP package in a different way (such as FTP), you need to create the D:\home\data\SitePackages directory and the packagename.txt file manually.

该命令还会重启应用。The command also restarts the app. 由于设置了 WEBSITE_RUN_FROM_PACKAGE,应用服务会将上传的包装载为只读的 wwwroot 目录,并直接从该装载目录运行应用。Because WEBSITE_RUN_FROM_PACKAGE is set, App Service mounts the uploaded package as the read-only wwwroot directory and runs the app directly from that mounted directory.

改为从外部 URL 运行Run from external URL instead

还可以从外部 URL(例如 Azure Blob 存储)运行包。You can also run a package from an external URL, such as Azure Blob Storage. 可以使用 Azure 存储资源管理器将包文件上传到 Blob 存储帐户。You can use the Azure Storage Explorer to upload package files to your Blob storage account. 应通过共享访问签名 (SAS) 使用专用存储容器,使应用服务运行时能够安全访问包。You should use a private storage container with a Shared Access Signature (SAS) to enable the App Service runtime to access the package securely.

将文件上传到 Blob 存储并获取该文件的 SAS URL 后,请将 WEBSITE_RUN_FROM_PACKAGE 应用设置指定为该 URL。Once you upload your file to Blob storage and have an SAS URL for the file, set the WEBSITE_RUN_FROM_PACKAGE app setting to the URL. 以下示例使用 Azure CLI 执行此操作:The following example does it by using Azure CLI:

az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings WEBSITE_RUN_FROM_PACKAGE="https://myblobstorage.blob.core.chinacloudapi.cn/content/SampleCoreMVCApp.zip?st=2018-02-13T09%3A48%3A00Z&se=2044-06-14T09%3A48%3A00Z&sp=rl&sv=2017-04-17&sr=b&sig=bNrVrEFzRHQB17GFJ7boEanetyJ9DGwBSV8OM3Mdh%2FM%3D"

如果将同名的已更新包发布到 Blob 存储,则需要重启应用,以便将更新的包加载到应用服务中。If you publish an updated package with the same name to Blob storage, you need to restart your app so that the updated package is loaded into App Service.

故障排除Troubleshooting

  • 直接从包运行会使 wwwroot 变为只读目录。Running directly from a package makes wwwroot read-only. 如果应用尝试将文件写入此目录,将会收到错误。Your app will receive an error if it tries to write files to this directory.
  • 不支持 TAR 和 GZIP 格式。TAR and GZIP formats are not supported.
  • 此功能与本地缓存不兼容。This feature is not compatible with local cache.
  • 若要提高冷启动性能,请使用本地 Zip 选项 (WEBSITE_RUN_FROM_PACKAGE=1)。For improved cold-start performance, use the local Zip option (WEBSITE_RUN_FROM_PACKAGE=1).

更多资源More resources