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

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

创建 VMCreate a VM

  1. 按照部署 Linux VM 以在 Azure Stack Hub 中托管 Web 应用中的说明,在 Azure Stack Hub 中设置 VM。Set up your VM in Azure Stack Hub by following the instructions in Deploy a Linux VM to host a web app in Azure Stack Hub.

  2. 在“VM 网络”窗格中,确保可以访问以下端口:In the VM network pane, make sure that 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 开发中的 Go Web 框架使用端口 3000。Port 3000 is used by the Go web framework in development. 对于生产服务器,通过 80 和 443 路由流量。For a production server, you route your traffic through 80 and 443.

安装 GoInstall Go

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

  2. 在 VM 上的 bash 提示符下,输入以下命令:At the bash prompt on your VM, enter the following commands:

    wget https://dl.google.com/go/go1.10.linux-amd64.tar.gz
    sudo tar -xvf go1.10.linux-amd64.tar.gz
    sudo mv go /usr/local
    
  3. 在 VM 上设置 Go 环境。Set up the Go environment on your VM. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following commands:

    export GOROOT=/usr/local/go
    export GOPATH=$HOME/Projects/ADMFactory/Golang
    export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
    
    vi ~/.profile
    
  4. 验证你的安装。Validate your installation. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following command:

        go version
    
  5. 安装 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
    

部署和运行应用Deploy and run the app

  1. 在 VM 上设置 Git 存储库。Set up your Git repository on the VM. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following commands:

       git clone https://github.com/appleboy/go-hello
    
       cd go-hello
       go get -d
    
  2. 启动应用。Start the app. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following command:

       go run hello-world.go
    
  3. 转到新服务器。Go to your new server. 应会看到你的 Web 应用程序正在运行。You should see your running web application.

       http://yourhostname.cloudapp.net:3000
    

后续步骤Next steps