將 Ruby Web 應用程式部署至 Azure Stack Hub 中的 VMDeploy a Ruby web app to a VM in Azure Stack Hub

您可以在 Azure Stack Hub 中建立用來裝載 Ruby Web 應用程式的 VM。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. 如需相關指示,請參閱在 Azure Stack Hub 中部署用來裝載 Web 應用程式的 Linux VMFor 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 CustomCustom 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. 如需指示,請參閱透過 SSH 與 PuTTy 進行連線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.cloudapp.net:3000
    

後續步驟Next steps