在 Azure App Service 中建立 PHP Web 應用程式Create a PHP web app in Azure App Service

Azure App Service 可提供可高度擴充、自我修復的 Web 主控服務。Azure App Service provides a highly scalable, self-patching web hosting service. 本快速入門教學課程會說明如何將 PHP 應用程式部署至 Windows 上的 Azure App Service。This quickstart tutorial shows how to deploy a PHP app to Azure App Service on Windows.

Azure App Service 可提供可高度擴充、自我修復的 Web 主控服務。Azure App Service provides a highly scalable, self-patching web hosting service. 本快速入門教學課程會說明如何將 PHP 應用程式部署至 Linux 上的 Azure App Service。This quickstart tutorial shows how to deploy a PHP app to Azure App Service on Linux.

您將會在 Cloud Shell 中使用 Azure CLI 建立 Web 應用程式,並使用 Git 將範例 PHP 程式碼部署至 Web 應用程式。You create the web app using the Azure CLI in Cloud Shell, and you use Git to deploy sample PHP code to the web app.

在 Azure 中執行的範例應用程式

您可以使用 Mac、Windows 或 Linux 機器,依照此處的步驟操作。You can follow the steps here using a Mac, Windows, or Linux machine. 安裝先決條件後,大約需要 5 分鐘才能完成這些步驟。Once the prerequisites are installed, it takes about five minutes to complete the steps.

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

PrerequisitesPrerequisites

若要完成本快速入門:To complete this quickstart:

將範例下載到本機Download the sample locally

在終端機視窗中,執行下列命令。In a terminal window, run the following commands. 這會將應用程式範例複製到本機電腦,並瀏覽至包含範例程式碼的目錄。This will clone the sample application to your local machine, and navigate to the directory containing the sample code.

git clone https://github.com/Azure-Samples/php-docs-hello-world
cd php-docs-hello-world

在本機執行應用程式Run the app locally

在本機執行應用程式,以便您查看它在部署至 Azure 時的樣貌。Run the application locally so that you see how it should look when you deploy it to Azure. 開啟終端機視窗,並使用 php 命令來啟動內建的 PHP Web 伺服器。Open a terminal window and use the php command to launch the built-in PHP web server.

php -S localhost:8080

開啟網頁瀏覽器,然後巡覽至位於 http://localhost:8080 的範例應用程式。Open a web browser, and navigate to the sample app at http://localhost:8080.

您會看到來自範例應用程式的 Hello World!You see the Hello World! 訊息顯示在網頁中。message from the sample app displayed in the page.

在本機執行的範例應用程式

在終端機視窗中,按 Ctrl+C 結束 web 伺服器。In your terminal window, press Ctrl+C to exit the web server.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。You can use either Bash or PowerShell with Cloud Shell to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文提到的程式碼,而不必在本機環境上安裝任何工具。You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

要啟動 Azure Cloud Shell:To start Azure Cloud Shell:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看]。Select Try It in the upper-right corner of a code block. 選取 [試用] 並不會自動將程式碼複製到 Cloud Shell 中。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的試試看範例
請前往 https://shell.azure.com 或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新視窗中啟動 Cloud ShellLaunch Cloud Shell in a new window
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

若要在 Azure Cloud Shell 中執行本文中的程式碼:To run the code in this article in Azure Cloud Shell:

  1. 啟動 Cloud Shell。Start Cloud Shell.

  2. 選取程式碼區塊上的 [複製] 按鈕,複製程式碼。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼貼到 Cloud Shell 工作階段中。Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. 選取 Enter 鍵執行程式碼。Select Enter to run the code.

設定部署使用者Configure a deployment user

FTP 和本機 Git 可以透過使用「部署使用者」 部署到 Azure Web 應用程式。FTP and local Git can deploy to an Azure web app by using a deployment user. 部署使用者只需設定一次,就能供所有 Azure 部署使用。Once you configure your deployment user, you can use it for all your Azure deployments. 使用者名稱和密碼屬於帳戶等級部署,因此與 Azure 訂用帳戶認證不同。Your account-level deployment username and password are different from your Azure subscription credentials.

若要設定部署使用者,請在 Azure Cloud Shell 中執行 az webapp deployment user set 命令。To configure the deployment user, run the az webapp deployment user set command in Azure Cloud Shell. 以部署使用者的使用者名稱和密碼取代 <username> 和 <password>。Replace <username> and <password> with a deployment user username and password.

  • 使用者名稱在 Azure 服務及本機 Git 推送中都必須是唯一的,且不能包含 ‘@’ 符號。The username must be unique within Azure, and for local Git pushes, must not contain the ‘@’ symbol.
  • 密碼長度必須至少為 8 個字元,包含下列三個元素其中兩個:字母、數字及符號。The password must be at least eight characters long, with two of the following three elements: letters, numbers, and symbols.
az webapp deployment user set --user-name <username> --password <password>

JSON 輸出會將密碼顯示為 nullThe JSON output shows the password as null. 如果您收到 'Conflict'. Details: 409 錯誤,請變更使用者名稱。If you get a 'Conflict'. Details: 409 error, change the username. 如果您收到 'Bad Request'. Details: 400 錯誤,請使用更強的密碼。If you get a 'Bad Request'. Details: 400 error, use a stronger password.

將使用者名稱和密碼記錄下來,在部署 Web 應用程式時還會用到。Record your username and password to use to deploy your web apps.

建立資源群組Create a resource group

資源群組是一個邏輯容器,可在其中部署與管理 Azure 資源 (例如 Web 應用程式、資料庫和儲存體帳戶)。A resource group is a logical container into which Azure resources, such as web apps, databases, and storage accounts, are deployed and managed. 例如,您可以選擇在稍候透過一個簡單的步驟刪除整個資源群組。For example, you can choose to delete the entire resource group in one simple step later.

在 Cloud Shell 中,使用 az group create 命令來建立資源群組。In the Cloud Shell, create a resource group with the az group create command. 下列範例會在「西歐」位置建立名為 myResourceGroup 的資源群組。The following example creates a resource group named myResourceGroup in the West Europe location. 若要查看 免類 層中 App Service 的所有支援位置,請執行 az appservice list-locations --sku FREE 命令。To see all supported locations for App Service in Free tier, run the az appservice list-locations --sku FREE command.

az group create --name myResourceGroup --location "West Europe"

您通常會在附近的區域中建立資源群組和資源。You generally create your resource group and the resources in a region near you.

當命令完成時,JSON 輸出會顯示資源群組屬性。When the command finishes, a JSON output shows you the resource group properties.

建立資源群組Create a resource group

資源群組是一個邏輯容器,可在其中部署與管理 Azure 資源 (例如 Web 應用程式、資料庫和儲存體帳戶)。A resource group is a logical container into which Azure resources, such as web apps, databases, and storage accounts, are deployed and managed. 例如,您可以選擇在稍候透過一個簡單的步驟刪除整個資源群組。For example, you can choose to delete the entire resource group in one simple step later.

在 Cloud Shell 中,使用 az group create 命令來建立資源群組。In the Cloud Shell, create a resource group with the az group create command. 下列範例會在「西歐」位置建立名為 myResourceGroup 的資源群組。The following example creates a resource group named myResourceGroup in the West Europe location. 若要查看 基本 層中 Linux 上之 App Service 的所有支援位置,請執行 az appservice list-locations --sku B1 --linux-workers-enabled 命令。To see all supported locations for App Service on Linux in Basic tier, run the az appservice list-locations --sku B1 --linux-workers-enabled command.

az group create --name myResourceGroup --location "West Europe"

您通常會在附近的區域中建立資源群組和資源。You generally create your resource group and the resources in a region near you.

當命令完成時,JSON 輸出會顯示資源群組屬性。When the command finishes, a JSON output shows you the resource group properties.

建立 Azure App Service 方案Create an Azure App Service plan

在 Cloud Shell 中,使用 az appservice plan create 命令來建立 App Service 方案。In the Cloud Shell, create an App Service plan with the az appservice plan create command.

下列範例會在 免費 定價層中建立名為 myAppServicePlan 的 App Service 方案。The following example creates an App Service plan named myAppServicePlan in the Free pricing tier:

az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku FREE

建立 App Service 方案後,Azure CLI 會顯示類似下列範例的資訊:When the App Service plan has been created, the Azure CLI shows information similar to the following example:

{ 
  "adminSiteName": null,
  "appServicePlanName": "myAppServicePlan",
  "geoRegion": "West Europe",
  "hostingEnvironmentProfile": null,
  "id": "/subscriptions/0000-0000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myAppServicePlan",
  "kind": "app",
  "location": "West Europe",
  "maximumNumberOfWorkers": 1,
  "name": "myAppServicePlan",
  < JSON data removed for brevity. >
  "targetWorkerSizeId": 0,
  "type": "Microsoft.Web/serverfarms",
  "workerTierName": null
} 

建立 Web 應用程式Create a web app

在 Cloud Shell 中,使用 az webapp create 命令,在 myAppServicePlan App Service 方案中建立 Web 應用程式。In the Cloud Shell, create a web app in the myAppServicePlan App Service plan with the az webapp create command.

在下列範,了中,使用全域唯一的應用程式名稱 (有效的字元為 a-z0-9-) 取代 <app-name>In the following example, replace <app-name> with a globally unique app name (valid characters are a-z, 0-9, and -). 執行階段設定為 PHP|7.4The runtime is set to PHP|7.4. 若要查看所有支援的執行階段,請執行 az webapp list-runtimesTo see all supported runtimes, run az webapp list-runtimes.

# Bash
az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app-name> --runtime "PHP|7.4" --deployment-local-git
# PowerShell
az --% webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app-name> --runtime "PHP|7.4" --deployment-local-git

注意

在 PowerShell 3.0 中引進的停止剖析符號 (--%) 會指示 PowerShell 避免將輸入解讀為 PowerShell 命令或運算式。The stop-parsing symbol (--%), introduced in PowerShell 3.0, directs PowerShell to refrain from interpreting input as PowerShell commands or expressions.

建立 Web 應用程式後,Azure CLI 會顯示類似下列範例的輸出:When the web app has been created, the Azure CLI shows output similar to the following example:

Local git is configured with url of 'https://<username>@<app-name>.scm.azurewebsites.net/<app-name>.git'
{
  "availabilityState": "Normal",
  "clientAffinityEnabled": true,
  "clientCertEnabled": false,
  "cloningInfo": null,
  "containerSize": 0,
  "dailyMemoryTimeQuota": 0,
  "defaultHostName": "<app-name>.azurewebsites.net",
  "enabled": true,
  < JSON data removed for brevity. >
}

您已建立空的新 Web 應用程式,其中已啟用 Git 部署。You've created an empty new web app, with git deployment enabled.

注意

Git 遠端的 URL 會顯示在 deploymentLocalGitUrl 屬性中,其格式為 https://<username>@<app-name>.scm.azurewebsites.net/<app-name>.gitThe URL of the Git remote is shown in the deploymentLocalGitUrl property, with the format https://<username>@<app-name>.scm.azurewebsites.net/<app-name>.git. 儲存此 URL,稍後您會用到此資訊。Save this URL as you need it later.

瀏覽至您剛建立的 Web 應用程式。Browse to your newly created web app. 以您在先前步驟中建立的唯一應用程式名稱取代 <app-name>Replace <app-name> with your unique app name created in the prior step.

http://<app-name>.azurewebsites.net

新的 Web 應用程式看起來應該像這樣:Here is what your new web app should look like:

空的 Web 應用程式頁面

從 Git 推送至 AzurePush to Azure from Git

回到本機終端視窗,將 Azure 遠端新增至本機 Git 存放庫。Back in the local terminal window, add an Azure remote to your local Git repository. 以您從 建立 Web 應用程式儲存的 Git 遠端 URL 取代 <deploymentLocalGitUrl-from-create-step>Replace <deploymentLocalGitUrl-from-create-step> with the URL of the Git remote that you saved from Create a web app.

git remote add azure <deploymentLocalGitUrl-from-create-step>

推送到 Azure 遠端,使用下列命令來部署您的應用程式。Push to the Azure remote to deploy your app with the following command. 當 Git 認證管理員提示輸入認證時,請務必輸入您在 設定部署使用者 中建立的認證,而不是您用來登入 Azure 入口網站的認證。When Git Credential Manager prompts you for credentials, make sure you enter the credentials you created in Configure a deployment user, not the credentials you use to sign in to the Azure portal.

git push azure master

此命令可能會花數分鐘執行。This command may take a few minutes to run. 執行上述命令時,會顯示類似下列範例的資訊:While running, it displays information similar to the following example:

Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 352 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Updating branch 'main'.
remote: Updating submodules.
remote: Preparing deployment for commit id '25f18051e9'.
remote: Generating deployment script.
remote: Running deployment command...
remote: Handling Basic Web Site deployment.
remote: Kudu sync from: '/home/site/repository' to: '/home/site/wwwroot'
remote: Copying file: '.gitignore'
remote: Copying file: 'LICENSE'
remote: Copying file: 'README.md'
remote: Copying file: 'index.php'
remote: Ignoring: .git
remote: Finished successfully.
remote: Running post deployment command(s)...
remote: Deployment successful.
To https://<app-name>.scm.azurewebsites.net/<app-name>.git
   cc39b1e..25f1805  main -> main

瀏覽至應用程式Browse to the app

使用 web 瀏覽器瀏覽至已部署的應用程式。Browse to the deployed application using your web browser.

http://<app-name>.azurewebsites.net

PHP 範例程式碼正在 Azure App Service Web 應用程式中執行。The PHP sample code is running in an Azure App Service web app.

在 Azure 中執行的範例應用程式

恭喜!Congratulations! 您已將第一個 PHP 應用程式部署至 App Service。You've deployed your first PHP app to App Service.

在本機更新和重新部署程式碼Update locally and redeploy the code

使用本機文字編輯器,開啟 PHP 應用程式內的 index.php 檔案,並且對 echo 旁邊字串內的文字進行小幅變更:Using a local text editor, open the index.php file within the PHP app, and make a small change to the text within the string next to echo:

echo "Hello Azure!";

在本機終端機視窗中,在 Git 中認可您的變更,然後將程式碼變更推送至 Azure。In the local terminal window, commit your changes in Git, and then push the code changes to Azure.

git commit -am "updated output"
git push azure main

部署完成後,返回在 瀏覽至應用程式 步驟中開啟的瀏覽器視窗,然後重新整理頁面。Once deployment has completed, return to the browser window that opened during the Browse to the app step, and refresh the page.

在 Azure 中執行的已更新範例應用程式

管理新的 Azure 應用程式Manage your new Azure app

  1. 請移至 Azure 入口網站,以管理您所建立的 Web 應用程式。Go to the Azure portal to manage the web app you created. 搜尋並選取 [應用程式服務] 。Search for and select App Services.

    搜尋應用程式服務、Azure 入口網站、建立 PHP Web 應用程式

  2. 選取您 Azure 應用程式的名稱。Select the name of your Azure app.

    入口網站瀏覽至 Azure 應用程式

    您 Web 應用程式的 [概觀] 頁面會隨即顯示。Your web app's Overview page will be displayed. 您可以在這裡執行基本管理工作,像是 [瀏覽] 、[停止] 、[重新啟動] 和 [刪除] 。Here, you can perform basic management tasks like Browse, Stop, Restart, and Delete.

    Azure 入口網站中的 App Service 頁面

    Web 應用程式功能表提供不同的選項來設定您的應用程式。The web app menu provides different options for configuring your app.

清除資源Clean up resources

在前述步驟中,您在資源群組中建立了 Azure 資源。In the preceding steps, you created Azure resources in a resource group. 如果您在未來不需要這些資源,請在 Cloud Shell 中執行下列命令,刪除資源群組:If you don't expect to need these resources in the future, delete the resource group by running the following command in the Cloud Shell:

az group delete --name myResourceGroup

此命令可能會花一分鐘執行。This command may take a minute to run.

後續步驟Next steps