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

使用 NGINX 创建 VMCreate a VM with NGINX

此脚本创建一个 Azure 虚拟机,并使用 Azure 虚拟机自定义脚本扩展安装 NGINX。This script creates an Azure Virtual Machine and uses the Azure Virtual Machine Custom Script Extension to install NGINX. 运行此脚本后,可通过虚拟机的公共 IP 地址访问演示网站。After running the script, you can access a demo website on the public IP address of the virtual machine.

若要运行此示例,请确保已安装最新的 Azure CLI 2.0To run this sample, make sure you have installed the latest Azure CLI 2.0. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

此示例在 Bash shell 中正常工作。This sample works in a Bash shell. 有关在 Windows 客户端上运行 Azure CLI 脚本的选项,请参阅在 Windows 上安装 Azure CLIFor options on running Azure CLI scripts on Windows client, see Install the Azure CLI on Windows.

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

示例脚本Sample script

#!/bin/bash

# Create a resource group.
az group create --name myResourceGroup --location westeurope

# Create a new virtual machine, this creates SSH keys if not present.
az vm create --resource-group myResourceGroup --name myVM --image UbuntuLTS --generate-ssh-keys

# Open port 80 to allow web traffic to host.
az vm open-port --port 80 --resource-group myResourceGroup --name myVM 

# Use CustomScript extension to install NGINX.
az vm extension set \
  --publisher Microsoft.Azure.Extensions \
  --version 2.0 \
  --name CustomScript \
  --vm-name myVM \
  --resource-group myResourceGroup \
  --settings '{"commandToExecute":"apt-get -y update && apt-get -y install nginx"}'

自定义脚本扩展Custom Script Extension

自定义脚本扩展将此脚本复制到虚拟机上。The custom script extension copies this script onto the virtual machine. 然后运行该脚本,安装和配置 NGINX Web 服务器。The script is then run to install and configure an NGINX web server.

#!/bin/bash

# update package source
apt-get -y update

# install NGINX
apt-get -y install nginx

清理部署Clean up deployment

运行以下命令来删除资源组、VM 和所有相关资源。Run the following command to remove the resource group, VM, and all related resources.

az group delete --name myResourceGroup

脚本说明Script explanation

此脚本使用以下命令创建资源组、虚拟机和所有相关资源。This script uses the following commands to create a resource group, virtual machine, and all related resources. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 说明Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az vm createaz vm create 创建虚拟机。Creates the virtual machine. 此命令还指定要使用的虚拟机映像和管理凭据。This command also specifies the virtual machine image to be used, and administrative credentials.
az vm open-portaz vm open-port 创建网络安全组规则,以允许入站流量。Creates a network security group rule to allow inbound traffic. 在此示例中,将为 HTTP 流量打开端口 80。In this sample, port 80 is opened for HTTP traffic.
azure vm extension setazure vm extension set 将虚拟机扩展添加到 VM 并运行该扩展。Adds and runs a virtual machine extension to a VM. 在此示例中,使用自定义脚本扩展来安装 NGINX。In this sample, the custom script extension is used to install NGINX.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Linux VM 文档中找到其他虚拟机 CLI 脚本示例。Additional virtual machine CLI script samples can be found in the Azure Linux VM documentation.