为 Azure 应用服务配置 Linux Ruby 应用Configure a Linux Ruby app for Azure App Service

本文介绍 Azure 应用服务如何在 Linux 容器中运行 Ruby 应用,以及如何按需自定义应用服务的行为。This article describes how Azure App Service runs Ruby apps in a Linux container, and how you can customize the behavior of App Service when needed. Ruby 应用必须与所有必需的 gem 一起部署。Ruby apps must be deployed with all the required gems.

对于在应用服务中使用内置 Linux 容器的 Ruby 开发人员,本指南为其提供了关键概念和说明。This guide provides key concepts and instructions for Ruby developers who use a built-in Linux container in App Service. 若从未使用过 Azure 应用服务,则首先应按照 Ruby 快速入门以及将 Ruby 与 PostgreSQL 配合使用教程进行操作。If you've never used Azure App Service, you should follow the Ruby quickstart and Ruby with PostgreSQL tutorial first.

显示 Ruby 版本Show Ruby version

若要显示当前的 Ruby 版本,请在 Azure CLI 中运行以下命令:To show the current Ruby version, run the following command in the Azure CLI:

az webapp config show --resource-group <resource-group-name> --name <app-name> --query linuxFxVersion

若要显示所有受支持的 Ruby 版本,请在 Azure CLI 中运行以下命令:To show all supported Ruby versions, run the following command in the Azure CLI:

az webapp list-runtimes --linux | grep RUBY

设置 Ruby 版本Set Ruby version

在 Azure CLI 中运行以下命令,将 Ruby 版本设置为 2.3:Run the following command in the Azure CLI to set the Ruby version to 2.3:

az webapp config set --resource-group <resource-group-name> --name <app-name> --linux-fx-version "RUBY|2.3"

备注

如果在部署期间看到与以下类似的错误:If you see errors similar to the following during deployment time:

Your Ruby version is 2.3.3, but your Gemfile specified 2.3.1

or

rbenv: version `2.3.1' is not installed

访问环境变量Access environment variables

在应用服务中,可以在应用代码外部设置应用设置In App Service, you can set app settings outside of your app code. 然后,可以使用标准的 ENV['<path-name>'] 模式访问这些设置。Then you can access them using the standard ENV['<path-name>'] pattern. 例如,若要访问名为 WEBSITE_SITE_NAME 的应用设置,请使用以下代码:For example, to access an app setting called WEBSITE_SITE_NAME, use the following code:

ENV['WEBSITE_SITE_NAME']

自定义部署Customize deployment

部署 Git 存储库或者部署启用了生成过程的 Zip 包时,部署引擎 (Kudu) 会默认运行以下后期部署步骤:When you deploy a Git repository, or a Zip package with build processes switched on, the deployment engine (Kudu) automatically runs the following post-deployment steps by default:

  1. 检查 Gemfile 是否存在。Check if a Gemfile exists.
  2. 运行 bundle cleanRun bundle clean.
  3. 运行 bundle install --path "vendor/bundle"Run bundle install --path "vendor/bundle".
  4. 运行 bundle package,将 gems 打包到 vendor/cache 文件夹中。Run bundle package to package gems into vendor/cache folder.

使用 --without 标志Use --without flag

要使用 --without 标志运行 bundle install,请将 BUNDLE_WITHOUT 应用设置设置为以逗号分隔的组列表。To run bundle install with the --without flag, set the BUNDLE_WITHOUT app setting to a comma-separated list of groups. 例如,使用以下命令将其设置为 development,testFor example, the following command sets it to development,test.

az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings BUNDLE_WITHOUT="development,test"

如果定义了此设置,那么部署引擎将使用 --without $BUNDLE_WITHOUT 运行 bundle installIf this setting is defined, then the deployment engine runs bundle install with --without $BUNDLE_WITHOUT.

预编译资产Precompile assets

默认情况下,后期部署步骤不会预编译资产。The post-deployment steps don't precompile assets by default. 要启用资产预编译,请将 ASSETS_PRECOMPILE 应用设置设为 trueTo turn on asset precompilation, set the ASSETS_PRECOMPILE app setting to true. 然后在后期部署步骤结束时运行命令 bundle exec rake --trace assets:precompileThen the command bundle exec rake --trace assets:precompile is run at the end of the post-deployment steps. 例如:For example:

az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings ASSETS_PRECOMPILE=true

有关详细信息,请参阅提供静态资产For more information, see Serve static assets.

自定义启动Customize start-up

默认情况下,Ruby 容器按以下顺序启动 Rails 服务器(有关详细信息,请参阅启动脚本):By default, the Ruby container starts the Rails server in the following sequence (for more information, see the start-up script):

  1. 生成 secret_key_base 值(如果尚不存在)。Generate a secret_key_base value, if one doesn't exist already. 应用在生产模式下运行时需要此值。This value is required for the app to run in production mode.
  2. RAILS_ENV 环境变量设置为 productionSet the RAILS_ENV environment variable to production.
  3. 在 tmp / pids 目录中,删除先前运行 Rails 服务器留下的任何 .pid 文件 。Delete any .pid file in the tmp/pids directory that's left by a previously running Rails server.
  4. 检查是否安装了所有依赖项。Check if all dependencies are installed. 如果没有,请尝试从本地 vendor/cache 目录安装 gems。If not, try installing gems from the local vendor/cache directory.
  5. 运行 rails server -e $RAILS_ENVRun rails server -e $RAILS_ENV.

可按以下方式自定义启动进程:You can customize the start-up process in the following ways:

提供静态资产Serve static assets

默认情况下,Ruby 容器中的 Rails 服务器在生产模式下运行,假定资产已进行预编译并由 Web 服务器提供。The Rails server in the Ruby container runs in production mode by default, and assumes that assets are precompiled and are served by your web server. 要从 Rails 服务器提供静态资产,需完成两个操作:To serve static assets from the Rails server, you need to do two things:

在非生产模式下运行Run in non-production mode

默认情况下,Rails 服务器在生产模式下运行。The Rails server runs in production mode by default. 例如,要在开发模式下运行,请将 RAILS_ENV 应用设置设为 developmentTo run in development mode, for example, set the RAILS_ENV app setting to development.

az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings RAILS_ENV="development"

但是,仅此设置会导致 Rails 服务器在开发模式下启动,该模式仅接受 localhost 请求,并且在容器外部无法访问。However, this setting alone causes the Rails server to start in development mode, which accepts localhost requests only and isn't accessible outside of the container. 要接受远程客户端请求,请将 APP_COMMAND_LINE 应用设置设为 rails server -b 0.0.0.0To accept remote client requests, set the APP_COMMAND_LINE app setting to rails server -b 0.0.0.0. 此应用设置使你可在 Ruby 容器中运行自定义命令。This app setting lets you run a custom command in the Ruby container. 例如:For example:

az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings APP_COMMAND_LINE="rails server -b 0.0.0.0"

手动设置 secret_key_baseSet secret_key_base manually

要使用自己的 secret_key_base 值而不是让应用服务生成一个值,请使用所需的值设置 SECRET_KEY_BASE 应用设置To use your own secret_key_base value instead of letting App Service generate one for you, set the SECRET_KEY_BASE app setting with the value you want. 例如:For example:

az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings SECRET_KEY_BASE="<key-base-value>"

访问诊断日志Access diagnostic logs

若要访问应用服务中的应用程序代码内生成的控制台日志,请在 Azure CLI 中运行以下命令以打开诊断日志记录:To access the console logs generated from inside your application code in App Service, turn on diagnostics logging by running the following command in the Azure CLI:

az webapp log config --resource-group <resource-group-name> --name <app-name> --application-logging true --level Verbose

--level 的可能值为:ErrorWarningInfoVerbosePossible values for --level are: Error, Warning, Info, and Verbose. 每个后续级别包括上一个级别。Each subsequent level includes the previous level. 例如:Error 仅包含错误消息,Verbose 则包含所有消息。For example: Error includes only error messages, and Verbose includes all messages.

启用诊断日志记录功能以后,请运行以下命令来查看日志流:Once diagnostic logging is turned on, run the following command to see the log stream:

az webapp log tail --resource-group <resource-group-name> --name <app-name>

如果没有立即看到控制台日志,请在 30 秒后重新查看。If you don't see console logs immediately, check again in 30 seconds.

备注

也可通过浏览器在 https://<app-name>.scm.chinacloudsites.cn/api/logs/docker 中检查日志文件。You can also inspect the log files from the browser at https://<app-name>.scm.chinacloudsites.cn/api/logs/docker.

若要随时停止日志流式处理,请键入 Ctrl+CTo stop log streaming at any time, type Ctrl+C.

在浏览器中打开 SSH 会话Open SSH session in browser

若要通过容器打开直接的 SSH 会话,应用应该处于正在运行状态。To make open a direct SSH session with your container, your app should be running.

将以下 URL 粘贴到浏览器中,将 <app-name> 替换为应用名称:Paste the following URL into your browser and replace <app-name> with your app name:

https://<app-name>.scm.chinacloudsites.cn/webssh/host

如果尚未进行身份验证,则需通过要连接的 Azure 订阅进行身份验证。If you're not yet authenticated, you're required to authenticate with your Azure subscription to connect. 完成身份验证以后,可以看到一个浏览器内 shell,可以在其中的容器中运行命令。Once authenticated, you see an in-browser shell, where you can run commands inside your container.

SSH 连接

备注

/home 目录之外进行的任何更改均存储在容器本身中,在应用重启后不保留。Any changes you make outside the /home directory are stored in the container itself and don't persist beyond an app restart.

若要从本地计算机打开远程 SSH 会话,请参阅从远程 shell 打开 SSH 会话To open a remote SSH session from your local machine, see Open SSH session from remote shell.

日志中的 robots933456robots933456 in logs

你可能会在容器日志中看到以下消息:You may see the following message in the container logs:

2019-04-08T14:07:56.641002476Z "-" - - [08/Apr/2019:14:07:56 +0000] "GET /robots933456.txt HTTP/1.1" 404 415 "-" "-"

可以放心忽略此消息。You can safely ignore this message. /robots933456.txt 是一个虚拟 URL 路径,应用服务使用它来检查容器能否为请求提供服务。/robots933456.txt is a dummy URL path that App Service uses to check if the container is capable of serving requests. 404 响应只是指示该路径不存在,但它让应用服务知道容器处于正常状态并已准备就绪,可以响应请求。A 404 response simply indicates that the path doesn't exist, but it lets App Service know that the container is healthy and ready to respond to requests.

后续步骤Next steps