Using the PowerShell in Azure to provide a Linux VM (virtual machine) with NGINX support

%3CLINGO-SUB%20id%3D%22lingo-sub-1795729%22%20slang%3D%22en-US%22%3EUsing%20the%20PowerShell%20in%20Azure%20to%20provide%20a%20Linux%20VM%20(virtual%20machine)%20with%20NGINX%20support%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1795729%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20Azure%20friends%2C%3C%2FP%3E%3CP%3EI%20used%20the%20PowerShell%20ISE%20for%20this%20configuration.%20But%20you%20are%20also%20very%20welcome%20to%20use%20Visual%20Studio%20Code%2C%20just%20as%20you%20wish.%26nbsp%3BPlease%20start%20with%20the%20following%20steps%20to%20begin%20the%20deployment%20(the%20Hashtags%20are%20comments)%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23The%20first%20two%20lines%20have%20nothing%20to%20do%20with%20the%20configuration%2C%20but%20make%20some%20space%20below%20in%20the%20blue%20part%20of%20the%20ISE%3C%2FP%3E%3CP%3E%3CSTRONG%3ESet-Location%20C%3A%5CTemp%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EClear-Host%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23So%20that%20you%20can%20carry%20out%20the%20configuration%2C%20you%20need%20the%20necessary%20cmdlets%2C%20these%20are%20contained%20in%20the%20module%20Az%20(is%20the%20higher-level%20module%20from%20a%20number%20of%20submodules)%3C%2FP%3E%3CP%3E%3CSTRONG%3EInstall-Module%20-Name%20Az%20-Force%20-AllowClobber%20-Verbose%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Log%20into%20Azure%3CBR%20%2F%3E%3CSTRONG%3EConnect-AzAccount%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Select%20the%20correct%20subscription%3CSTRONG%3E%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet-AzContext%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet-AzSubscription%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet-AzSubscription%20-SubscriptionName%20%22your%20subscription%20name%22%20%7C%20Select-AzSubscription%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Variables%20for%20common%20values%3CBR%20%2F%3E%3CSTRONG%3E%24resourceGroup%20%3D%20%22myResourceGroup%22%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24location%20%3D%20%22westeurope%22%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24vmName%20%3D%20%22myVM%22%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Define%20user%20name%20and%20blank%20password%20(never%20put%20a%20secret%20in%20code%2C%20but%20for%20this%20demo%20it%20is%20OK)%3CBR%20%2F%3E%3CSTRONG%3E%24securePassword%20%3D%20ConvertTo-SecureString%20'P%40ssw0rd123!!'%20-AsPlainText%20-Force%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24cred%20%3D%20New-Object%20System.Management.Automation.PSCredential%20(%22tom%22%2C%20%24securePassword)%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Create%20a%20resource%20group%3CBR%20%2F%3E%3CSTRONG%3ENew-AzResourceGroup%20-Name%20%24resourceGroup%20-Location%20%24location%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Create%20a%20subnet%20configuration%3CBR%20%2F%3E%3CSTRONG%3E%24subnetConfig%20%3D%20New-AzVirtualNetworkSubnetConfig%20-Name%20mySubnet%20-AddressPrefix%20192.168.1.0%2F24%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Create%20a%20virtual%20network%3CBR%20%2F%3E%3CSTRONG%3E%24vnet%20%3D%20New-AzVirtualNetwork%20-ResourceGroupName%20%24resourceGroup%20-Location%20%24location%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-Name%20MYvNET%20-AddressPrefix%20192.168.0.0%2F16%20-Subnet%20%24subnetConfig%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Create%20a%20public%20IP%20address%20and%20specify%20a%20DNS%20name%3CBR%20%2F%3E%3CSTRONG%3E%24pip%20%3D%20New-AzPublicIpAddress%20-ResourceGroupName%20%24resourceGroup%20-Location%20%24location%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-Name%20%22mypublicdns%24(Get-Random)%22%20-AllocationMethod%20Static%20-IdleTimeoutInMinutes%204%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Create%20an%20inbound%20network%20security%20group%20rule%20for%20port%2022%3CBR%20%2F%3E%3CSTRONG%3E%24nsgRuleSSH%20%3D%20New-AzNetworkSecurityRuleConfig%20-Name%20myNetworkSecurityGroupRuleSSH%20-Protocol%20Tcp%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-Direction%20Inbound%20-Priority%201000%20-SourceAddressPrefix%20*%20-SourcePortRange%20*%20-DestinationAddressPrefix%20*%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-DestinationPortRange%2022%20-Access%20Allow%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Create%20an%20inbound%20network%20security%20group%20rule%20for%20port%2080%3CBR%20%2F%3E%3CSTRONG%3E%24nsgRuleHTTP%20%3D%20New-AzNetworkSecurityRuleConfig%20-Name%20myNetworkSecurityGroupRuleHTTP%20-Protocol%20Tcp%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-Direction%20Inbound%20-Priority%202000%20-SourceAddressPrefix%20*%20-SourcePortRange%20*%20-DestinationAddressPrefix%20*%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-DestinationPortRange%2080%20-Access%20Allow%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Create%20a%20network%20security%20group%3CBR%20%2F%3E%3CSTRONG%3E%24nsg%20%3D%20New-AzNetworkSecurityGroup%20-ResourceGroupName%20%24resourceGroup%20-Location%20%24location%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-Name%20myNetworkSecurityGroup%20-SecurityRules%20%24nsgRuleSSH%2C%24nsgRuleHTTP%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Create%20a%20virtual%20network%20card%20and%20associate%20with%20public%20IP%20address%20and%20NSG%3CBR%20%2F%3E%3CSTRONG%3E%24nic%20%3D%20New-AzNetworkInterface%20-Name%20myNic%20-ResourceGroupName%20%24resourceGroup%20-Location%20%24location%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-SubnetId%20%24vnet.Subnets%5B0%5D.Id%20-PublicIpAddressId%20%24pip.Id%20-NetworkSecurityGroupId%20%24nsg.Id%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Create%20a%20virtual%20machine%20configuration%3CBR%20%2F%3E%3CSTRONG%3E%24vmConfig%20%3D%20New-AzVMConfig%20-VMName%20%24vmName%20-VMSize%20Standard_DS2_v2%20%7C%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ESet-AzVMOperatingSystem%20-Linux%20-ComputerName%20%24vmName%20-Credential%20%24cred%20%7C%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ESet-AzVMSourceImage%20-PublisherName%20Canonical%20-Offer%20UbuntuServer%20-Skus%2018.04-LTS%20-Version%20latest%20%7C%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EAdd-AzVMNetworkInterface%20-Id%20%24nic.Id%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Create%20a%20virtual%20machine%3CBR%20%2F%3E%3CSTRONG%3ENew-AzVM%20-ResourceGroupName%20%24resourceGroup%20-Location%20%24location%20-VM%20%24vmConfig%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Install%20NGINX.%3CBR%20%2F%3E%3CSTRONG%3E%24PublicSettings%20%3D%20'%7B%22commandToExecute%22%3A%22apt-get%20-y%20update%20%26amp%3B%26amp%3B%20apt-get%20-y%20install%20nginx%22%7D'%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ESet-AzVMExtension%20-ExtensionName%20%22NGINX%22%20-ResourceGroupName%20%24resourceGroup%20-VMName%20%24vmName%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-Publisher%20%22Microsoft.Azure.Extensions%22%20-ExtensionType%20%22CustomScript%22%20-TypeHandlerVersion%202.0%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-SettingString%20%24PublicSettings%20-Location%20%24location%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Check%20NGINX%20within%20the%20vm%20or%20use%20the%20public%20IP%20of%20the%20VM%20in%20a%20browser%20session.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20you%20have%20configured%20a%20linux%20virtual%20machine%20with%20the%20PowerShell%20in%20Azure!%20Congratulations!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Delete%20all%20resources%20(when%20you%20no%20longer%20need%20it)%3CBR%20%2F%3E%3CSTRONG%3ERemove-AzResourceGroup%20-Name%20myResourceGroup%20-Force%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20this%20article%20was%20useful.%20Best%20regards%2C%20Tom%20Wechsler%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EP.S.%26nbsp%3BAll%20scripts%20(%23PowerShell%2C%20Azure%20CLI%2C%20%23Terraform%2C%20%23ARM)%20that%20I%20use%20can%20be%20found%20on%20github!%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ftomwechsler%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Ftomwechsler%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1795729%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ECompute%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EHands-on-Labs%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EVirtual%20Machine%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Frequent Contributor

 

Hi Azure friends,

I used the PowerShell ISE for this configuration. But you are also very welcome to use Visual Studio Code, just as you wish. Please start with the following steps to begin the deployment (the Hashtags are comments):

 

#The first two lines have nothing to do with the configuration, but make some space below in the blue part of the ISE

Set-Location C:\Temp
Clear-Host

 

#So that you can carry out the configuration, you need the necessary cmdlets, these are contained in the module Az (is the higher-level module from a number of submodules)

Install-Module -Name Az -Force -AllowClobber -Verbose

 

#Log into Azure
Connect-AzAccount

 

#Select the correct subscription

Get-AzContext

Get-AzSubscription

Get-AzSubscription -SubscriptionName "your subscription name" | Select-AzSubscription

 

# Variables for common values
$resourceGroup = "myResourceGroup"
$location = "westeurope"
$vmName = "myVM"

 

# Define user name and blank password (never put a secret in code, but for this demo it is OK)
$securePassword = ConvertTo-SecureString 'P@ssw0rd123!!' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("tom", $securePassword)

 

# Create a resource group
New-AzResourceGroup -Name $resourceGroup -Location $location

 

# Create a subnet configuration
$subnetConfig = New-AzVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24

 

# Create a virtual network
$vnet = New-AzVirtualNetwork -ResourceGroupName $resourceGroup -Location $location `
-Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig

 

# Create a public IP address and specify a DNS name
$pip = New-AzPublicIpAddress -ResourceGroupName $resourceGroup -Location $location `
-Name "mypublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4

 

# Create an inbound network security group rule for port 22
$nsgRuleSSH = New-AzNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleSSH -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 22 -Access Allow

 

# Create an inbound network security group rule for port 80
$nsgRuleHTTP = New-AzNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleHTTP -Protocol Tcp `
-Direction Inbound -Priority 2000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 80 -Access Allow

 

# Create a network security group
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `
-Name myNetworkSecurityGroup -SecurityRules $nsgRuleSSH,$nsgRuleHTTP

 

# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzNetworkInterface -Name myNic -ResourceGroupName $resourceGroup -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

 

# Create a virtual machine configuration
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize Standard_DS2_v2 | `
Set-AzVMOperatingSystem -Linux -ComputerName $vmName -Credential $cred | `
Set-AzVMSourceImage -PublisherName Canonical -Offer UbuntuServer -Skus 18.04-LTS -Version latest | `
Add-AzVMNetworkInterface -Id $nic.Id

 

# Create a virtual machine
New-AzVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig

 

# Install NGINX.
$PublicSettings = '{"commandToExecute":"apt-get -y update && apt-get -y install nginx"}'

Set-AzVMExtension -ExtensionName "NGINX" -ResourceGroupName $resourceGroup -VMName $vmName `
-Publisher "Microsoft.Azure.Extensions" -ExtensionType "CustomScript" -TypeHandlerVersion 2.0 `
-SettingString $PublicSettings -Location $location

 

#Check NGINX within the vm or use the public IP of the VM in a browser session.

 

Now you have configured a linux virtual machine with the PowerShell in Azure! Congratulations!

 

#Delete all resources (when you no longer need it)
Remove-AzResourceGroup -Name myResourceGroup -Force

 

I hope this article was useful. Best regards, Tom Wechsler

 

P.S. All scripts (#PowerShell, Azure CLI, #Terraform, #ARM) that I use can be found on github! https://github.com/tomwechsler

 

0 Replies