将 Ruby Web 应用部署到 Azure Stack Hub 中的 VMDeploy a Ruby web app to a VM in Azure Stack Hub

可以创建一个 VM 来托管 Azure Stack Hub 中的 Ruby Web 应用。You can create a VM to host your Ruby web app in Azure Stack Hub. 在本文中,你将设置一个服务器,将该服务器配置为托管 Ruby Web 应用,然后将该应用部署到 Azure Stack Hub。In this article, you set up a server, configure the server to host your Ruby web app, and then deploy the app to Azure Stack Hub.

本文使用 Ruby 以及 Ruby on Rails Web 框架。This article uses Ruby and a Ruby on Rails web framework.

创建 VMCreate a VM

  1. 在 Azure Stack Hub 中设置 VM。Set up your VM in Azure Stack Hub. 有关说明,请参阅部署 Linux VM 以在 Azure Stack Hub 中托管 Web 应用For instructions, see Deploy a Linux VM to host a web app in Azure Stack Hub.

  2. 在“VM 网络”窗格中,确保可以访问以下端口:In the VM network pane, make sure the following ports are accessible:

    端口Port 协议Protocol 说明Description
    8080 HTTPHTTP 超文本传输协议 (HTTP) 是用于从服务器传递网页的协议。Hypertext Transfer Protocol (HTTP) is the protocol that's used to deliver webpages from servers. 客户端使用 DNS 名称或 IP 地址通过 HTTP 进行连接。Clients connect via HTTP with a DNS name or IP address.
    443443 HTTPSHTTPS 安全超文本传输协议 (HTTPS) 是 HTTP 的安全版本,它需要一个安全证书,并允许对信息进行加密传输。Hypertext Transfer Protocol Secure (HTTPS) is a secure version of HTTP that requires a security certificate and allows for the encrypted transmission of information.
    2222 SSHSSH 安全外壳 (SSH) 是一种用于安全通信的加密网络协议。Secure Shell (SSH) is an encrypted network protocol for secure communications. 你在 SSH 客户端上使用此连接来配置 VM 并部署应用。You use this connection with an SSH client to configure the VM and deploy the app.
    33893389 RDPRDP 可选。Optional. 远程桌面协议 (RDP) 允许远程桌面连接使用计算机的图形用户界面。The Remote Desktop Protocol (RDP) allows a remote desktop connection to use a graphic user interface on your machine.
    30003000 自定义Custom 开发中的 Ruby on Rails Web 框架使用的端口。The port that's used by the Ruby on Rails web framework in development. 对于生产服务器,通过 80 和 443 路由流量。For a production server, you route your traffic through 80 and 443.

安装 RubyInstall Ruby

  1. 使用 SSH 客户端连接到 VM。Connect to your VM by using your SSH client. 有关说明,请参阅使用 PuTTy 通过 SSH 进行连接For instructions, see Connect via SSH with PuTTy.

  2. 安装 PPA 存储库。Install the PPA repository. 在 VM 上的 bash 提示符下,输入以下命令:At the bash prompt on your VM, enter the following commands:

    sudo apt -y install software-properties-common
    sudo apt-add-repository ppa:brightbox/ruby-ng
    
    sudo apt update
    
  3. 在 VM 上安装 Ruby 和 Ruby on Rails。Install Ruby and Ruby on Rails on your VM. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following commands:

    sudo apt install ruby
    gem install rails -v 4.2.6
    
  4. 安装 Ruby on Rails 依赖项。Install Ruby on Rails dependencies. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following commands:

    sudo apt-get install make
    sudo apt-get install gcc
    sudo apt-get install sqlite3
    sudo apt-get install nodejs
    sudo gem install sqlite
    sudo gem install bundler
    

    备注

    安装 Ruby on Rails 依赖项时,可能需要反复运行 sudo gem install bundlerWhile you're installing Ruby on Rails dependencies, you might need to repeatedly run sudo gem install bundler. 如果安装失败,请查看错误日志并解决问题。If the installation fails, review the error logs and resolve the issues.

  5. 验证你的安装。Validate your installation. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following command:

        ruby -v
    
  6. 安装 Git,一种广泛分布的版本控制和源代码管理 (SCM) 系统。Install Git, a widely distributed version control and source code management (SCM) system. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following command:

       sudo apt-get -y install git
    

创建并运行应用Create and run an app

  1. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following commands:

        rails new myapp
        cd myapp
        rails server -b 0.0.0.0 -p 3000
    
  2. 转到新服务器。Go to your new server. 应会看到你的 Web 应用程序正在运行。You should see your running web application.

       http://yourhostname.chinacloudapp.cn:3000
    

后续步骤Next steps