在应用服务中创建 Ruby on Rails 应用Create a Ruby on Rails App in App Service

Linux 上的 Azure 应用服务使用 Linux 操作系统,提供高度可缩放的自修补 Web 托管服务。Azure App Service on Linux provides a highly scalable, self-patching web hosting service using the Linux operating system. 本快速入门教程介绍了如何使用 Azure CLI 将 Ruby on Rails 应用部署到 Linux 上的应用服务。This quickstart tutorial shows how to deploy a Ruby on Rails app to App Service on Linux using the Azure CLI.


Ruby 开发堆栈目前仅支持 Ruby on Rails。The Ruby development stack only supports Ruby on Rails at this time.


如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.


下载示例Download the sample

在终端窗口中,运行以下命令,将示例应用存储库克隆到本地计算机:In a terminal window, run the following command to clone the sample app repository to your local machine:

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

在本地运行应用程序Run the application locally

在本地运行应用程序,以便你能了解将它部署到 Azure 时它的外观应该是什么样的。Run the application locally so that you see how it should look when you deploy it to Azure. 打开终端窗口,转到 hello-world 目录,然后使用 rails server 命令启动该服务器。Open a terminal window, change to the hello-world directory, and use the rails server command to start the server.

第一步是安装必需的 gem。The first step is to install the required gems. 示例中包含了 Gemfile,因此只需运行以下命令:There's a Gemfile included in the sample, so just run the following command:

bundle install

安装 gem 后,我们将使用捆绑程序启动应用:Once the gems are installed, we'll use bundler to start the app:

bundle exec rails server

使用 Web 浏览器导航到 http://localhost:3000 以在本地测试该应用。Using your web browser, navigate to http://localhost:3000 to test the app locally.

已配置了 Hello World

创建部署用户Create 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 like 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.

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

通常在附近的区域中创建资源组和资源。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

在 Azure CLI 中,使用 az appservice plan create 命令在资源组中创建应用服务计划。In the Azure CLI, create an App Service plan in the resource group 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 到 10 个实例)Scale count (1 to 10 instances)
  • SKU(基本、标准)SKU (Basic, Standard)

以下示例在免费定价层 (--sku F1) 和 Linux 容器 (--is-linux) 中创建名为 myAppServicePlan 的应用服务计划。The following example creates an App Service plan named myAppServicePlan in the Free pricing tier (--sku F1) and in a Linux container (--is-linux).

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

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

创建 Web 应用Create a web app

myAppServicePlan 应用服务计划中创建一个 Web 应用Create a web app in the myAppServicePlan App Service plan.

在 Azure CLI 中,可以使用 az webapp create 命令。In the Azure CLI, you can use 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 -). 运行时设置为 RUBY|2.6.2The runtime is set to RUBY|2.6.2. 若要查看所有受支持的运行时,请运行 az webapp list-runtimes --linuxTo see all supported runtimes, run az webapp list-runtimes --linux.

# Bash
az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app-name> --runtime "RUBY|2.6.2" --deployment-local-git
# PowerShell
az --% webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app-name> --runtime "RUBY|2.6.2" --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",
  "deploymentLocalGitUrl": "https://<username>@<app-name>.scm.chinacloudsites.cn/<app-name>.git",
  "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.cn/<app-name>.gitThe URL of the Git remote is shown in the deploymentLocalGitUrl property, with the format https://<username>@<app-name>.scm.chinacloudsites.cn/<app-name>.git. 保存此 URL,后续将会用到。Save this URL as you need it later.

浏览到该应用,查看使用内置映像新建的 Web 应用。Browse to the app to see your newly created web app with built-in image. <应用名称> 替换为 Web 应用名称。Replace <app name> with your web app name.


新 Web 应用应该如下所示:Here is what your new web app should look like:


部署应用程序Deploy your application

运行以下命令将本地应用程序部署到 Azure Web 应用:Run the following commands to deploy the local application to your Azure web app:

git remote add azure <Git deployment URL from above>
git push azure master

确认远程部署操作报告了成功消息。Confirm that the remote deployment operations report success. 命令生成的输出类似于以下文本:The commands produce output similar to the following text:

remote: Using turbolinks 5.2.0
remote: Using uglifier 4.1.20
remote: Using web-console 3.7.0
remote: Bundle complete! 18 Gemfile dependencies, 78 gems now installed.
remote: Bundled gems are installed into `/tmp/bundle`
remote: Zipping up bundle contents
remote: .......
remote: ~/site/repository
remote: Finished successfully.
remote: Running post deployment command(s)...
remote: Deployment successful.
remote: App container will begin restart within 10 seconds.
To https://<app-name>.scm.chinacloudsites.cn/<app-name>.git
   a6e73a2..ae34be9  master -> master

部署完成后,请等待大约 10 秒,然后重启 Web 应用,再导航到 Web 应用并验证结果。Once the deployment has completed, wait about 10 seconds for the web app to restart, and then navigate to the web app and verify the results.


更新的 Web 应用


应用重启时,可能会在浏览器中看到 HTTP 状态代码 Error 503 Server unavailable 或在默认页面中看到 Hey, Ruby developers!While the app is restarting, you may observe the HTTP status code Error 503 Server unavailable in the browser, or the Hey, Ruby developers! default page. 可能需要花费几分钟时间才能完全重启应用。It may take a few minutes for the app to fully restart.

清理部署Clean up deployment

运行示例脚本后,可以使用以下命令删除资源组以及与其关联的所有资源。After the sample script has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name myResourceGroup

后续步骤Next steps