Level 1
0 / 100 XP

Create an Azure Virtual Machine via CLI

In this lesson, you will use the CLI to create an Azure virtual machine (VM), install Nginx (a popular web server), and then delete the VM along with all associated resources. You will use the Azure CLI for this task and work within a resource group named FreeResourcesRG.

Task 1: Create a Linux Virtual Machine and Install Nginx

Use the following Azure CLI commands to create a Linux VM and install Nginx. The Custom Script Extension will be used to install Nginx on your VM after it is created.

Open Cloud Shell

Access the Azure Portal.
Click on the Cloud Shell icon in the top menu bar.

Create a Linux Virtual Machine

Run the following command to create a Linux VM in the FreeResourcesRG resource group:

Text
az vm create \\ --resource-group FreeResourcesRG \\ --name my-vm \\ --public-ip-sku Standard \\ --image Ubuntu2204 \\ --admin-username azureuser \\ --generate-ssh-keys

This command will create a VM named my-vm with Ubuntu 22.04, using SSH keys for authentication. The VM creation will take a few minutes.

Install Nginx on the VM

After the VM is created, run the following command to install Nginx using the Custom Script Extension:

Text
az vm extension set \\ --resource-group FreeResourcesRG \\ --vm-name my-vm \\ --name customScript \\ --publisher Microsoft.Azure.Extensions \\ --version 2.1 \\ --settings '{"fileUris":["https://raw.githubusercontent.com/MicrosoftDocs/mslearn-welcome-to-azure/master/configure-nginx.sh"]}' \\ --protected-settings '{"commandToExecute": "./configure-nginx.sh"}'

This command will:

  • Download the Bash script from GitHub.
  • Run the script to update the package list, install Nginx, and set a custom welcome message.

Task 2: Verify Nginx Installation

Get the Public IP Address

Run the following command to get the public IP address of your VM:…