您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

使用 Azure CLI 创建 Linux 虚拟机Create a Linux virtual machine with the Azure CLI

Azure CLI 用于从命令行或脚本创建和管理 Azure 资源。The Azure CLI is used to create and manage Azure resources from the command line or in scripts. 本快速入门教程详细介绍了如何使用 Azure CLI 部署运行 Ubuntu 服务器的虚拟机。This quickstart details using the Azure CLI to deploy a virtual machine running Ubuntu server. 服务器部署以后,将创建 SSH 连接,并且安装 NGINX webserver。Once the server is deployed, an SSH connection is created, and an NGINX webserver is installed.

如果还没有 Azure 订阅,可以在开始前创建一个 免费帐户If you don't have an Azure subscription, create a free account before you begin.

启动 Azure Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它预安装有常用 Azure 工具并将其配置与帐户一起使用。It has common Azure tools preinstalled and configured to use with your account. 请直接单击“复制”对代码进行复制,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. 可以通过两种方式来启动 Cloud Shell:There are two ways to launch the Cloud Shell:

单击代码块右上角的“试用”。Click Try It in the upper right corner of a code block. 本文中的 Cloud Shell
单击 Azure 门户右上角菜单上的“Cloud Shell”按钮。Click the Cloud Shell button on the menu in the upper right of the Azure portal. 门户中的 Cloud Shell

如果选择在本地安装并使用 CLI,此快速入门教程要求运行 Azure CLI 2.0.4 版或更高版本。If you choose to install and use the CLI locally, this quickstart requires that you are running the Azure CLI version 2.0.4 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLI 2.0If you need to install or upgrade, see Install Azure CLI 2.0.

创建资源组Create a resource group

使用 az group create 命令创建资源组。Create a resource group with the az group create command. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed.

以下示例在“eastus”位置创建名为“myResourceGroup”的资源组。The following example creates a resource group named myResourceGroup in the eastus location.

az group create --name myResourceGroup --location eastus

创建虚拟机Create virtual machine

使用 az vm create 命令创建 VM。Create a VM with the az vm create command.

下面的示例创建一个名为 myVM 的 VM,并且在默认密钥位置中不存在 SSH 密钥时创建这些密钥。The following example creates a VM named myVM and creates SSH keys if they do not already exist in a default key location. 若要使用特定的一组密钥,请使用 --ssh-key-value 选项。To use a specific set of keys, use the --ssh-key-value option.

az vm create --resource-group myResourceGroup --name myVM --image UbuntuLTS --generate-ssh-keys

创建 VM 后,Azure CLI 会显示类似于以下示例的信息。When the VM has been created, the Azure CLI shows information similar to the following example. 记下 publicIpAddressTake note of the publicIpAddress. 此地址用于访问 VM。This address is used to access the VM.

{
  "fqdns": "",
  "id": "/subscriptions/d5b9d4b7-6fc1-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM",
  "location": "eastus",
  "macAddress": "00-0D-3A-23-9A-49",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "40.68.254.142",
  "resourceGroup": "myResourceGroup"
}

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

默认情况下,仅允许通过 SSH 连接登录到 Azure 中部署的 Linux 虚拟机。By default only SSH connections are allowed into Linux virtual machines deployed in Azure. 如果此 VM 将用作 Web 服务器,则需要从 Internet 打开端口 80。If this VM is going to be a webserver, you need to open port 80 from the Internet. 使用 az vm open-port 命令打开所需端口。Use the az vm open-port command to open the desired port.

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

通过 SSH 连接到 VMSSH into your VM

使用以下命令创建与虚拟机的 SSH 会话。Use the following command to create an SSH session with the virtual machine. 确保将 替换为虚拟机的相应公共 IP 地址。Make sure to replace with the correct public IP address of your virtual machine. 在上例中,我们的 IP 地址为 40.68.254.142In our example above our IP address was 40.68.254.142.

ssh <publicIpAddress>

安装 NGINXInstall NGINX

使用以下命令更新包源并安装最新的 NGINX 包。Use the following commands to update package sources and install the latest NGINX package.

# update package source
sudo apt-get -y update

# install NGINX
sudo apt-get -y install nginx

查看 NGINX 欢迎页View the NGINX welcome page

NGINX 已安装,并且现在已从 Internet 打开 VM 上的端口 80 - 可以使用所选的 Web 浏览器查看默认的 NGINX 欢迎页。With NGINX installed and port 80 now open on your VM from the Internet - you can use a web browser of your choice to view the default NGINX welcome page. 请务必使用前面记录的 publicIpAddress 访问默认页面。Be sure to use the publicIpAddress you documented above to visit the default page.

NGINX 默认站点

清理资源Clean up resources

如果不再需要资源组、VM 和所有相关的资源,可以使用 az group delete 命令将其删除。When no longer needed, you can use the az group delete command to remove the resource group, VM, and all related resources. 退出 SSH 会话,返回 VM,然后删除资源,如下所示:Exit the SSH session to your VM, then delete the resources as follows:

az group delete --name myResourceGroup

后续步骤Next steps

在本快速入门中,部署了一台简单的虚拟机、一条网络安全组规则,并安装了一台 Web 服务器。In this quick start, you’ve deployed a simple virtual machine, a network security group rule, and installed a web server. 若要详细了解 Azure 虚拟机,请继续学习 Linux VM 的教程。To learn more about Azure virtual machines, continue to the tutorial for Linux VMs.