在 Azure 中创建 PHP Web 应用Create a PHP web app in Azure

Azure 应用服务提供高度可缩放、自修补的 Web 托管服务。Azure App Service provides a highly scalable, self-patching web hosting service. 本快速入门教程演示如何将 PHP 应用部署到 Azure 应用服务。This quickstart tutorial shows how to deploy a PHP app to Azure App Service. 在 CLI 中使用 Azure CLI 创建 Web 应用,并使用 Git 将 PHP 代码示例部署到 Web 应用。You create the web app using the Azure CLI in CLI, 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. 安装先决条件后,大约需要五分钟完成这些步骤。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 trial account before you begin.


若要完成本快速入门教程,需先执行以下操作: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

打开 Web 浏览器并导航到 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.

创建部署用户Create a deployment user

使用 az webapp deployment user set 命令配置部署凭据。Configure deployment credentials with the az webapp deployment user set command. 对 Web 应用进行 FTP 和本地 Git 部署时需要此部署用户。This deployment user is required for FTP and local Git deployment to a web app. 用户名和密码都为帐户级别。The user name and password are account level. 它们与 Azure 订阅凭据不同。They are different from your Azure subscription credentials.

在以下示例中,将 <username><password>(包括括号)替换为新的用户名和密码。In the following example, replace <username> and <password> (including brackets) with a new user name and password. 用户名在 Azure 中必须唯一。The user name must be unique within Azure. 密码长度必须至少为 8 个字符,其中包含以下 3 种元素中的两种:字母、数字、符号。The password must be at least eight characters long, with two of the following three elements: letters, numbers, symbols.

az webapp deployment user set --user-name <username> --password <password>

命令执行完成后你会获得一个 JSON 输出,密码显示为 nullYou should get a JSON output, with the password shown 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.

只需配置此部署用户一次;之后可对所有 Azure 部署使用此用户。You need to configure this deployment user only once; you can use it for all your Azure deployments.


记录用户名和密码。Record the user name and password. 稍后需要使用它们来部署 Web 应用。You need them to deploy the web app later.

创建资源组Create a resource group

资源组是在其中部署和管理 Azure 资源(例如 Web 应用、数据库和存储帐户)的逻辑容器。A resource group is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed.

在 Azure CLI 中,使用 az group create 命令创建资源组。In the Azure CLI, create a resource group with the az group create command. 以下示例在“中国北部” 位置创建名为“myResourceGroup” 的资源组。The following example creates a resource group named myResourceGroup in the China North location. 要查看“免费”层中应用服务支持的所有位置,请运行 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 "China North"

通常在附近的区域中创建资源组和资源。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 应用服务计划Create an Azure App Service plan

使用 az appservice plan create 命令创建应用服务计划。Create an App Service plan with the az appservice plan create command.

应用服务计划指定托管应用的 Web 服务器场的位置、大小和功能。An App Service plan specifies the location, size, and features of the web server farm that hosts your app. 可以在托管多个应用时节省资金,方法是将多个 Web 应用配置为共用单个应用服务计划。You can save money when hosting multiple apps by configuring the web apps to share a single App Service plan.

应用服务计划定义:App Service plans define:

  • 区域(例如:中国东部、中国东部 2、中国北部、中国北部 2)Region (for example: China East, China East 2, China North, China North 2)
  • 实例大小(小、中、大)Instance size (small, medium, or large)
  • 规模计数(1 到 20 个实例)Scale count (1 to 20 instances)
  • SKU(免费、共享、基本、标准、高级)SKU (Free, Shared, Basic, Standard, or Premium)

以下示例在免费 定价层中创建名为 myAppServicePlan 的应用服务计划: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

创建应用服务计划后,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": "China North",
  "hostingEnvironmentProfile": null,
  "id": "/subscriptions/0000-0000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myAppServicePlan",
  "kind": "app",
  "location": "China North",
  "maximumNumberOfWorkers": 1,
  "name": "myAppServicePlan",
  < JSON data removed for brevity. >
  "targetWorkerSizeId": 0,
  "type": "Microsoft.Web/serverfarms",
  "workerTierName": null

创建 Web 应用Create a web app

在本地终端窗口中,使用 az webapp create 命令在 myAppServicePlan 应用服务计划中创建一个 Web 应用。In the local terminal window, create a web app in the myAppServicePlan App Service plan with the az webapp create command.

在以下示例中,将 <app_name> 替换为全局唯一的应用名称(有效字符是 a-z0-9-)。In the following example, replace <app_name> with a globally unique app name (valid characters are a-z, 0-9, and -). 运行时设置为 PHP|7.0The runtime is set to PHP|7.0. 若要查看所有受支持的运行时,请运行 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.0" --deployment-local-git
# PowerShell
az --% webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app_name> --runtime "PHP|7.0" --deployment-local-git

创建 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.chinacloudsites.cn/<app_name>.git'
  "availabilityState": "Normal",
  "clientAffinityEnabled": true,
  "clientCertEnabled": false,
  "cloningInfo": null,
  "containerSize": 0,
  "dailyMemoryTimeQuota": 0,
  "defaultHostName": "<app_name>.chinacloudsites.cn",
  "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.chinacloudsites.net/<app_name>.gitThe URL of the Git remote is shown in the deploymentLocalGitUrl property, with the format https://<username>@<app_name>.scm.chinacloudsites.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>.chinacloudsites.cn

新 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. <deploymentLocalGitUrl-from-create-step> 替换为在创建 Web 应用中保存的 Git 远程 URL。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 prompted for credentials by Git Credential Manager, make sure that 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 'master'.
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.chinacloudsites.cn/<app_name>.git
   cc39b1e..25f1805  master -> master

浏览到应用Browse to the app

使用 Web 浏览器浏览到已部署的应用程序。Browse to the deployed application using your web browser.


PHP 示例代码正在 Azure 应用服务 Web 应用中运行。The PHP sample code is running in an Azure App Service web app.

在 Azure 中运行应用的示例

祝贺你!Congratulations! 现已将第一个 PHP 应用部署到应用服务。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 master

完成部署后,返回到“浏览到应用”步骤期间打开的浏览器窗口,然后刷新页面。 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

转到 Azure 门户管理创建的 Web 应用。Go to the Azure portal to manage the web app you created.

在左侧菜单中单击“应用程序服务” ,然后单击 Azure 应用的名称。From the left menu, click App Services, and then click 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, start, restart, and delete.

Azure 门户中的应用服务页

左侧菜单提供用于配置应用的不同选项。The left menu provides different options for configuring your app.

清理资源Clean up resources

若要清理资源,请运行以下命令:To clean up your resources, run the following command:

az group delete --name myResourceGroup

后续步骤Next steps