快速入门:在 Azure Stack Hub 中使用 Azure CLI 创建 Linux 服务器 VMQuickstart: Create a Linux server VM by using the Azure CLI in Azure Stack Hub

可以使用 Azure CLI 创建 Ubuntu Server 16.04 LTS 虚拟机 (VM)。You can create an Ubuntu Server 16.04 LTS virtual machine (VM) by using the Azure CLI. 在本文中,我们将创建和使用虚拟机。In this article, you create and use a virtual machine. 本文还介绍以下操作:This article also shows you how to:

  • 通过远程客户端连接到虚拟机。Connect to the virtual machine with a remote client.
  • 安装 NGINX Web 服务器并查看默认主页。Install an NGINX web server and view the default home page.
  • 清理未使用的资源。Clean up unused resources.

先决条件Prerequisites

  • Azure Stack Hub 市场中的 Linux 映像A Linux image in the Azure Stack Hub Marketplace

    默认情况下,Azure Stack Hub 市场不包含 Linux 映像。The Azure Stack Hub Marketplace doesn't contain a Linux image by default. 让 Azure Stack Hub 操作员提供你需要的 Ubuntu Server 16.04 LTS 映像。Have the Azure Stack Hub operator provide the Ubuntu Server 16.04 LTS image you need. 操作员可以使用将市场项从 Azure 下载到 Azure Stack Hub 中的说明。The operator can use the instructions in Download Marketplace items from Azure to Azure Stack Hub.

  • Azure Stack Hub 需要使用特定版本的 Azure CLI 来创建和管理其资源。Azure Stack Hub requires a specific version of the Azure CLI to create and manage its resources. 如果尚未针对 Azure Stack Hub 配置 Azure CLI,请登录到 Azure Stack 开发工具包(或登录到基于 Windows 的外部客户端,前提是已通过 VPN 建立了连接),按照说明安装并配置 Azure CLIIf you don't have the Azure CLI configured for Azure Stack Hub, sign in to the Azure Stack Development Kit (or a Windows-based external client if you're connected through VPN), and follow the instructions for installing and configuring the Azure CLI.

  • Windows 用户配置文件的 .ssh 目录中保存的名为 id_rsa.pub 的安全外壳 (SSH) 公钥。A public Secure Shell (SSH) key with the name id_rsa.pub saved in the .ssh directory of your Windows user profile. 有关如何创建 SSH 密钥的详细信息,请参阅使用 SSH 公钥For detailed information about creating SSH keys, see Use an SSH public key.

创建资源组Create a resource group

资源组是一个逻辑容器,可以在其中部署和管理 Azure Stack Hub 资源。A resource group is a logical container where you can deploy and manage Azure Stack Hub resources. 在开发工具包或 Azure Stack Hub 集成系统中,运行 az group create 命令创建资源组。From your development kit or the Azure Stack Hub integrated system, run the az group create command to create a resource group.

备注

我们在以下代码示例中为所有变量分配了值。We've assigned values for all variables in the following code examples. 但是,你可以分配自己的值。However, you can assign your own values.

以下示例在本地位置创建名为 myResourceGroup 的资源组:The following example creates a resource group named myResourceGroup in the local location:

az group create --name myResourceGroup --location local

创建虚拟机Create a virtual machine

可以使用 az vm create 命令创建虚拟机。Create a virtual machine by using the az vm create command. 以下示例创建名为 myVM 的 VM。The following example creates a VM named myVM. 此示例使用 Demouser 作为管理员用户名,使用 Demouser@123 作为管理员密码。The example uses Demouser as the admin username and Demouser@123 as the admin password. 将这些值更改为适合你的环境的值。Change these values to something that's appropriate for your environment.

az vm create \
  --resource-group "myResourceGroup" \
  --name "myVM" \
  --image "UbuntuLTS" \
  --admin-username "Demouser" \
  --admin-password "Demouser@123" \
  --location local

公共 IP 地址在 PublicIpAddress 参数中返回。The public IP address is returned in the PublicIpAddress parameter. 请记下该地址,供以后与虚拟机配合使用。Note the address for later use with the virtual machine.

为 Web 流量打开端口 80Open port 80 for web traffic

由于此虚拟机将用来运行 IIS Web 服务器,因此需要为 Internet 流量打开端口 80。Because this virtual machine is going to run the IIS web server, you need to open port 80 to internet traffic. 若要打开该端口,请使用 az vm open-port 命令:To open the port, use the az vm open-port command:

az vm open-port --port 80 --resource-group myResourceGroup --name myVM

使用 SSH 连接到虚拟机Use SSH to connect to the virtual machine

从安装了 SSH 的客户端计算机连接到虚拟机。From a client computer with SSH installed, connect to the virtual machine. 如果在 Windows 客户端上操作,请使用 PuTTY 创建连接。If you're working on a Windows client, use PuTTY to create the connection. 若要连接到虚拟机,请使用以下命令:To connect to the virtual machine, use the following command:

ssh <publicIpAddress>

安装 NGINX Web 服务器Install the NGINX web server

若要更新包源并安装最新的 NGINX 包,请运行以下脚本:To update package resources and install the latest NGINX package, run the following script:

#!/bin/bash

# update package source
apt-get -y update

# install NGINX
apt-get -y install nginx

查看 NGINX 欢迎页View the NGINX welcome page

在虚拟机上安装 NGINX Web 服务器并打开端口 80 后,可通过虚拟机的公共 IP 地址访问 Web 服务器。With the NGINX web server installed, and port 80 open on your virtual machine, you can access the web server by using the virtual machine's public IP address. 为此,请打开浏览器并转到 http://<public IP address>To do so, open a browser, and go to http://<public IP address>.

NGINX Web 服务器欢迎页

清理资源Clean up resources

清理不再需要的资源。Clean up the resources that you don't need any longer. 可以使用 az group delete 命令删除它们。You can use the az group delete command to remove them. 运行以下命令:Run the following command:

az group delete --name myResourceGroup

后续步骤Next steps

在本快速入门中,你已部署了一个带有 Web 服务器的基本 Linux 服务器虚拟机。In this quickstart, you deployed a basic Linux server virtual machine with a web server. 若要详细了解 Azure Stack Hub 虚拟机,请参阅 Azure Stack Hub 中虚拟机的注意事项To learn more about Azure Stack Hub virtual machines, see Considerations for virtual machines in Azure Stack Hub.