In this quick guide, I demonstrate how to deploy a Linux virtual machine (VM) on Azure using the Azure CLI, install an Nginx Web Server, and open port 80. Azure resources can be provisioned and managed via the Azure Portal and Azure CLI, either directly from the command line or using scripts and PowerShell cmdlets. We’ll be installing the most recent Ubuntu LTS image in this guide. At the end of this guide, you will be able to install the NGINX web server, establish an SSH connection to the VM, and start using it just as you would use it in real-life scenarios. The Azure Cloud Shell, according to Microsoft, is a free interactive shell that you can use to run the steps to create and manage resources on Azure. It has common Azure tools preinstalled and configured for use with your account. Azure CLI can be assessed by clicking on the Cloud Shell icon on the Azure Portal from the upper right corner of the screen. Alternatively, you can also open Cloud Shell in a separate browser tab by going to https://shell.azure.com/bash. If you prefer to install and use the CLI locally, you can also do so by downloading and installing Azure CLI version 2.0.30 or later. Run
az --version to find the new version. If you need to install or upgrade, see Install Azure CLI. You can also learn how to back up an Azure VM from the VM settings, remove Azure VM: How to delete a Virtual Machine via the Azure Portal, how to Add or Remove Network Interface from a VM in Azure, how To Configure VM Update Management on Azure Stack Hub.
Create Azure Linux VM Using Azure CLI
To get started with creating your first Linux VM with Azure CLI, sign into Azure Portal. Note: to be able to create a VM on Azure, you must have an active subscription. If you don’t have one, you can sign up for an Azure free trial account here.
After you have logged into the portal, the first thing to create is the Azure Resource Group which is a logical container into which Azure resources are deployed and managed. In our example here we create a resource group named techdirecharchiveRG in the
westus location using the CLI command below:
az group create --name techdirectarchiveRG --location westus
Now, let’s proceed to create the Linux VM. Still, on the same Cloud Shell window, type the command below:
avm create --name techdirectvm --resource-group techdirectarchiveRG --location westus --image UbuntuLTS --admin-user azureuser --generate-ssh-keys
Installing the Nginx Web Server
Next, to make good use of the VM we just created, we need to install the NGINX web server. To do this we need to update the package sources first and install the Nginx web server. To do this, run the below command:
az vm run-command invoke -g techdirectarchiveRG -n techdirectvm --command-id RunShellScript --scripts "sudo apt-get update && sudo apt-get install -y nginx"
Opening Port 80
The last step in this series is to open the TCP port 80 for web traffic to and from our web server. When you first provision a Linux Virtual Machine in Azure, only SSH connections are opened by default. Open TCP port 80 with the command
az vm open-port to use with the NGINX web server:
az vm open-port --port 80 --resource-group techdirectarchiveRG --name techdirectvm
Accessing the Web Server via Web Browser
Now you can browse to the NGINX default welcome page using any of your preferred web browsers. Access the overview page of the virtual machine to copy the public IP address to serve as the website address.
The default NGINX website is displayed as shown in the screenshot below: