在 Azure App 服務 中建立 PHP Web 應用程式

Azure App Service 提供可高度調整且自我修補的虛擬主機服務。 本快速入門教學課程示範如何將 PHP 應用程式部署至 Windows 上的 Azure App 服務。

您可以在 Cloud Shell 中使用 Azure CLI 建立 Web 應用程式,並使用 Git 將範例 PHP 程式代碼部署至 Web 應用程式。

Sample app running in Azure

您可以使用 Mac、Windows 或 Linux 計算機遵循這裡的步驟。 安裝必要條件之後,大約需要五分鐘的時間才能完成這些步驟。

如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

必要條件

若要完成本快速入門:

在本機下載範例

  1. 在終端機視窗中,執行下列命令。 它會將範例應用程式複製到本機計算機,並流覽至包含範例程式代碼的目錄。

    git clone https://github.com/Azure-Samples/php-docs-hello-world
    cd php-docs-hello-world
    
  2. 請確定預設分支為 main

    git branch -m main
    

    提示

    App Service 不需要變更分支名稱。 不過,由於許多存放庫將預設分支變更為 main,本快速入門也會示範如何從 main部署存放庫。

在本機執行應用程式

  1. 在本機執行應用程式,以便在您將應用程式部署至 Azure 時,查看其外觀。 開啟終端機視窗,並使用 php 命令啟動內建 PHP 網頁伺服器。

    php -S localhost:8080
    
  2. 開啟網頁瀏覽器,然後瀏覽至 位於 http://localhost:8080的範例應用程式。

    您會看到頁面所顯示範例應用程式的 Hello World! 訊息。

    Sample app running locally

  3. 在您的終端機視窗中,按 Ctrl+C 結束網頁伺服器。

Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。 您可以使用 Cloud Shell 預先安裝的命令,執行本文提到的程式碼,而不必在本機環境上安裝任何工具。

要啟動 Azure Cloud Shell:

選項 範例/連結
選取程式碼或命令區塊右上角的 [試試看]。 選取 [試試看] 並不會自動將程式碼或命令複製到 Cloud Shell 中。 Screenshot that shows an example of Try It for Azure Cloud Shell.
請前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 Button to launch Azure Cloud Shell.
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 Screenshot that shows the Cloud Shell button in the Azure portal

若要使用 Azure Cloud Shell:

  1. 啟動 Cloud Shell。

  2. 選取程式碼區塊 (或命令區塊) 上的 [複製] 按鈕以複製程式碼或命令。

  3. 透過在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼或命令貼到 Cloud Shell 工作階段中。

  4. 選取 Enter 鍵執行程式碼或命令。

設定部署使用者

FTP 和本機 Git 可以使用部署使用者部署至 Azure Web 應用程式。 設定部署用戶之後,即可將其用於所有 Azure 部署。 您的帳戶層級部署使用者名稱和密碼與您的 Azure 訂用帳戶認證不同。

若要設定部署使用者,請在 Azure Cloud Shell 中執行 az webapp deployment user set 命令。 將使用者>名稱和<密碼>取代<為部署使用者名稱和密碼。

  • 用戶名稱在 Azure 內必須是唯一的,而且對於本機 Git 推送,不得包含 『@』 符號。
  • 密碼長度必須至少為八個字元,且下列三個元素有兩個:字母、數位和符號。
az webapp deployment user set --user-name <username> --password <password>

JSON 輸出會將密碼顯示為 null。 如果您收到 'Conflict'. Details: 409 錯誤,請變更用戶名稱。 如果您收到 'Bad Request'. Details: 400 錯誤,請使用更強的密碼。

記錄您的使用者名稱和密碼,以用來部署 Web 應用程式。

建立資源群組

資源群組是一個邏輯容器,其中會部署和管理 Azure 資源,例如 Web 應用程式、資料庫和記憶體帳戶。 例如,您可以選擇稍後在一個簡單的步驟中刪除整個資源群組。

在 Cloud Shell 中,使用 az group create 命令建立資源群組。 下列範例會在西歐位置建立名為 myResourceGroup 的資源群組。 若要查看免費層中 App Service 的所有支援位置,請執行 az appservice list-locations --sku FREE 命令。

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

您通常會在附近的區域中建立您的資源群組和資源。

當命令完成時,JSON 輸出會顯示資源群組屬性。

建立 Azure App Service 方案

在 Cloud Shell 中,使用 az appservice plan create 命令建立 App Service 方案。

下列範例會在免費定價層中建立名為 myAppServicePlanApp Service 方案:

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

建立 App Service 方案之後,Azure CLI 會顯示類似下列範例的資訊:

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

建立 Web 應用程式

  1. 在 Cloud Shell 中,使用 az webapp create 命令在 myAppServicePlan App Service 方案中建立 Web 應用程式。

    在下列範例中,將 取代 <app-name> 為全域唯一的應用程式名稱(有效字元為 a-z0-9-。 執行時間設定為 PHP|7.4。 若要檢視所有支援的執行時間,請執行 az webapp list-runtimes

    az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app-name> --runtime 'PHP|8.1' --deployment-local-git
    

    建立 Web 應用程式後,Azure CLI 會顯示類似下列範例的輸出:

     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 部署。

    注意

    Git 遠端的網址會顯示在屬性中 deploymentLocalGitUrl , 格式為 https://<username>@<app-name>.scm.azurewebsites.net/<app-name>.git。 請稍後視需要儲存此 URL。

  2. 流覽至新建立的 Web 應用程式。 將 app-name> 取代<為您在上一個步驟中建立的唯一應用程式名稱。

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

    以下是新 Web 應用程式的外觀:

    Empty web app page

從 Git 推送至 Azure

  1. 由於您要部署 main 分支,因此您必須將App Service 應用程式的預設部署分支設定為 main (請參閱 變更部署分支)。 在 Cloud Shell 中,使用 az webapp config appsettings set 命令設定DEPLOYMENT_BRANCH應用程式設定。

    az webapp config appsettings set --name <app-name> --resource-group myResourceGroup --settings DEPLOYMENT_BRANCH='main'
    
  2. 回到本機終端視窗,將 Azure 遠端新增至本機 Git 存放庫。 將 deploymentLocalGitUrl-from-create-step> 取代<為您從建立 Web 應用程式儲存的 Git 遠端 URL。

    git remote add azure <deploymentLocalGitUrl-from-create-step>
    
  3. 使用下列命令推送至 Azure 遠端以部署您的應用程式。 當 Git 認證管理員提示您輸入認證時,請務必輸入您在設定部署使用者建立的認證,而不是您用來登入 Azure 入口網站 的認證。

    git push azure main
    

    執行此命令可能會需要花費幾分鐘。 執行時會顯示類似下列範例的資訊:

  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
  

瀏覽至應用程式

使用網頁瀏覽器流覽至已部署的應用程式。

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

PHP 範例程式代碼正在 Azure App 服務 Web 應用程式中執行。

Sample app running in Azure

恭喜! 您已將第一個 PHP 應用程式部署至 App Service。

在本機更新並重新部署程序代碼

  1. 使用本機文字編輯器,在 PHP 應用程式中開啟 index.php 檔案,然後對 旁 echo字串內的文字進行小變更:

    echo "Hello Azure!";
    
  2. 在本機終端機視窗中,認可 Git 中的變更,然後將程式代碼變更推送至 Azure。

    git commit -am "updated output"
    git push azure main
    
  3. 部署完成後,返回在瀏覽至應用程式步驟期間開啟的瀏覽器視窗,然後重新整理頁面。

    Updated sample app running in Azure

管理新的 Azure 應用程式

  1. 移至 Azure 入口網站 來管理您所建立的 Web 應用程式。 搜尋並選取 [應用程式服務]。

    Search for App Services, Azure portal, create PHP web app

  2. 選取 Azure 應用程式的名稱。

    Portal navigation to Azure app

    將會顯示 Web 應用程式的 [概觀 ] 頁面。 在這裡,您可以執行基本管理工作,例如 流覽停止重新啟動刪除

    App Service page in Azure portal

    Web 應用程式功能表提供不同的選項來設定您的應用程式。

清除資源

在上述步驟中,您已建立資源群組中的 Azure 資源。 如果您未來不需要這些資源,請在 Cloud Shell 中執行下列命令來刪除資源群組:

az group delete --name myResourceGroup

此命令可能需要一分鐘的時間才能執行。

Azure App Service 提供可高度調整且自我修補的虛擬主機服務。 本快速入門說明如何將 PHP 應用程式部署至 Linux 上的 Azure App 服務。

Screenshot of the sample app running in Azure.

您可以使用 Mac、Windows 或 Linux 計算機遵循這裡的步驟。 安裝必要條件之後,大約需要五分鐘的時間才能完成這些步驟。

若要完成本快速入門,您需要:

1 - 取得範例存放庫

您可以在 Cloud Shell 中使用 Azure CLI 建立 Web 應用程式,並使用 Git 將範例 PHP 程式代碼部署至 Web 應用程式。

  1. 在終端機視窗中,執行下列命令,將範例應用程式複製到本機計算機,並流覽至專案根目錄。

    git clone https://github.com/Azure-Samples/php-docs-hello-world
    cd php-docs-hello-world
    
  2. 若要在本機執行應用程式,請使用 php 命令啟動內建 PHP 網頁伺服器。

    php -S localhost:8080
    
  3. 流覽至網頁瀏覽器中的範例應用程式 http://localhost:8080

    Screenshot of the sample app running locally.

  4. 在您的終端機視窗中,按 Ctrl+C 結束網頁伺服器。

2 - 將應用程式程式代碼部署至 Azure

Azure CLI 有一個命令 az webapp up ,可建立必要的資源,並在單一步驟中部署您的應用程式。

在終端機中,使用 az webapp up 命令在您的本機資料夾中部署程式代碼:

az webapp up --runtime "PHP:8.2" --os-type=linux
  • az如果無法辨識命令,請確定您已安裝 Azure CLI
  • --runtime "PHP:8.2" 變數會使用 PHP 8.2 版建立 Web 應用程式。
  • --os-type=linux變數會在 Linux 上的 App Service 上建立 Web 應用程式。
  • 您可以選擇性地使用 自變數 --name <app-name>來指定名稱。 如果您沒有提供名稱,則會自動產生名稱。
  • 您可以選擇性地包含自變數 --location <location-name> ,其中 <location_name> 是可用的 Azure 區域。 您可以執行 az account list-locations 命令來擷取 Azure 帳戶允許的區域清單。
  • 如果您看到錯誤「無法自動偵測應用程式的運行時間堆疊」,請確定您是在程式代碼目錄中執行 命令(請參閱 針對 az webapp up 的自動偵測問題進行疑難解答)。

此命令需要幾分鐘的時間才能完成。 執行時,它會提供建立資源群組、App Service 方案和應用程式資源、設定記錄,以及執行 ZIP 部署的相關訊息。 接著會提供訊息:「您可以在 azure 上的應用程式 URL http://< app-name.azurewebsites.net> 啟動應用程式。

The webapp '<app-name>' doesn't exist
Creating Resource group '<group-name>' ...
Resource group creation complete
Creating AppServicePlan '<app-service-plan-name>' ...
Creating webapp '<app-name>' ...
Configuring default logging for the app, if not already enabled
Creating zip with contents of dir /home/msangapu/myPhpApp ...
Getting scm site credentials for zip deployment
Starting zip deployment. This operation can take a while to complete ...
Deployment endpoint responded with status code 202
You can launch the app at http://<app-name>.azurewebsites.net
{
  "URL": "http://<app-name>.azurewebsites.net",
  "appserviceplan": "<app-service-plan-name>",
  "location": "centralus",
  "name": "<app-name>",
  "os": "linux",
  "resourcegroup": "<group-name>",
  "runtime_version": "php|8.2",
  "runtime_version_detected": "0.0",
  "sku": "FREE",
  "src_path": "//home//msangapu//myPhpApp"
}

注意

az webapp up 命令會執行下列動作:

在網頁瀏覽器中瀏覽至已部署的應用程式,網址 http://<app-name>.azurewebsites.net為 。

PHP 範例程式代碼正在 Azure App 服務 中執行。

Screenshot of the sample app running in Azure, showing 'Hello World!'.

恭喜! 您已使用 Azure 入口網站,將第一個 PHP 應用程式部署至 App Service。

3 - 更新並重新部署應用程式

  1. 使用本機文字編輯器,在 PHP 應用程式中開啟 index.php 檔案,然後對 旁 echo字串內的文字進行小變更:

    echo "Hello Azure!";
    
  2. 儲存您的變更,然後使用 az webapp up 命令重新部署應用程式,並搭配下列自變數:

    az webapp up --runtime "PHP:8.2" --os-type=linux
    
  3. 部署完成後,返回在瀏覽至應用程式步驟期間開啟的瀏覽器視窗,然後重新整理頁面。

    Screenshot of the updated sample app running in Azure.

4 - 管理新的 Azure 應用程式

  1. 移至 Azure 入口網站 以管理您建立的 Web 應用程式。 搜尋並選取 [應用程式服務]。

    Screenshot of the Azure portal with 'app services' typed in the search text box. In the results, the App Services option under Services is highlighted.

  2. 選取 Azure 應用程式的名稱。

    Screenshot of the App Services list in Azure. The name of the demo app service is highlighted.

    應該會顯示 Web 應用程式的 [概觀 ] 頁面。 在這裡,您可以執行基本管理工作,例如 流覽停止重新啟動刪除

    Screenshot of the App Service overview page in Azure portal. In the action bar, the Browse, Stop, Swap (disabled), Restart, and Delete button group is highlighted.

    Web 應用程式功能表提供不同的選項來設定您的應用程式。

5 - 清除資源

當您完成範例應用程式時,您可以從 Azure 移除應用程式的所有資源。 其可協助您避免額外的費用,並讓您的 Azure 訂用帳戶保持不完全。 拿掉資源群組也會移除資源群組中的所有資源,而且是移除應用程式所有 Azure 資源最快的方式。

使用 az group delete 命令刪除 資源群組。

az group delete --name myResourceGroup

此命令需要一分鐘的時間才能執行。

下一步