Create a Linux virtual machine and install Nginx
- You could use the Azure portal, the Azure CLI, Azure PowerShell, or an Azure Resource Manager (ARM) template to create VM
Steps
- From Cloud Shell, run the following
az vm create
command to create a Linux VM:
az vm create \
--resource-group learn-ffdd40cf-ba09-42a6-adb6-964309d8927f \
--name my-vm \
--image UbuntuLTS \
--admin-username azureuser \
--generate-ssh-keys
- Run the following
az vm extension set
command to configure Nginx on your VM:
az vm extension set \
--resource-group learn-ffdd40cf-ba09-42a6-adb6-964309d8927f \
--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 uses the Custom Script Extension to run a Bash script on your VM. The script is stored on GitHub.
#!/bin/bash
# Update apt cache.
sudo apt-get update
# Install Nginx.
sudo apt-get install -y nginx
# Set the home page.
echo "<html><body><h2>Welcome to Azure! My name is $(hostname).</h2></body></html>" | sudo tee -a /var/www/html/index.html
- Runs
apt-get update
to download the latest package information from the internet. This step helps ensure that the next command can locate the latest version of the Nginx package.
- Installs Nginx.
- Sets the home page, /var/www/html/index.html, to print a welcome message that includes your VM's host name.