直接從 ZIP 套件在 Azure App Service 中執行您的應用程式Run your app in Azure App Service directly from a ZIP package

Azure App Service中,您可以直接從部署 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.

App Service 中的所有其他部署方法都有共同的功能:您的檔案會部署到應用程式中的 D:\home\site\wwwroot , (或 /home/site/wwwroot for Linux 應用程式) 。All other deployment methods in App Service have something in common: your files are deployed to D:\home\site\wwwroot in your app (or /home/site/wwwroot for Linux apps). 由於您的應用程式在執行時間使用相同的目錄,因此部署可能會因為檔案鎖定衝突而失敗,而且應用程式的行為無法預期,因為有些檔案尚未更新。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 Resource Manager 部署的效能。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 會新增其他巢狀目錄,其不適用於 App Service。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.html、index.php 和 app.js。This directory should contain the entry file to your web app, such as index.html, index.php, and app.js. 也可以包含套件管理檔案,像是 project.json、composer.json、package.json、bower.json 和 requirements.txt。It can also contain package management files like project.json, composer.json, package.json, bower.json, and requirements.txt.

除非您想要 App Service 為您執行部署自動化,否則請執行所有的組建工作 (例如、、 npm bower gulpcomposerpip) ,並確定您擁有執行應用程式所需的所有檔案。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 publishFor 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

在 App Service 中執行封裝的最簡單方式,是使用 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. 如果您以不同的方式上傳 ZIP 封裝 (例如 FTP) ,您需要手動建立 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 已設定,App Service 會將上傳的套件掛接為唯讀 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) 的私人儲存體容器,讓 App Service 執行時間安全地存取套件。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.windows.net/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 儲存體,您需要重新開機應用程式,以便將更新的封裝載入 App Service 中。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.
  • ZIP 檔案最多可達1GBThe ZIP file can be at most 1GB
  • 這項功能與 本機快取不相容。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