Create an Azure VM to host a DotNet Application using Azure CLI

Joseph Ariyo
4 min readMay 26, 2023

--

Azure provides a robust infrastructure for hosting and deploying applications, and Azure Virtual Machines (VMs) offer a flexible and scalable solution for running your applications in the cloud. In this article, I will walk you through the step-by-step process of creating an Azure VM to host a .NET application using Azure CLI. By following this guide, you will be able to set up a virtual machine environment that is ready to deploy your .NET application.

Prerequisites:

  1. An Azure subscription
  2. Azure CLI installed on your local machine. You can download and install it from the Azure CLI documentation: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli

Step 1: Sign in to Azure CLI
Open your terminal or command prompt and sign in to Azure using the following command and follow the instructions provided in the command prompt to complete the login process.:

az login

Alternatively, you can make use of Cloud shell on your Azure Portal. It is much easier as it has the CLI installed by default.

Step 2: Create a resource group
Choose or create a resource group to contain the Azure resources. Use the following command to create a resource group

az group create --name <resource_group_name> --location <location>

You should replace<resource_group_name> with a unique name for your resource group, and <location> with the Azure region where you want to deploy the resources.

Step 3:
Create an Azure VM to host your .NET application, execute the following command:

az vm create \
--resource-group <resource_group_name> \
--name <vm_name> \
--image Win2019Datacenter \
--admin-username <admin_username> \
--admin-password <admin_password> \
--size Standard_DS2_v2 \
--public-ip-address-dns-name <dns_name>

Replace <resource_group_name> with the name of the resource group created in Step 2, <vm_name> with a name for your virtual machine, <admin_username> with the desired username for the administrator account, <admin_password> with the password for the administrator account, Standard_DS2_v2 with the desired VM size, and <dns_name> with a unique DNS name for your VM.

Step 4:
To establish a remote connection to the virtual machine, use the following command to open the RDP port:

az vm open-port --resource-group <resource_group_name> --name <vm_name> --port 3389

Step 5:
Connect to the virtual machine via RDP Use a Remote Desktop client to connect to the Azure VM using its public IP address or DNS name. Enter the username and password specified during VM creation to establish the connection.

Step 6:
Enable the IIS feature with ASP.NET on a Windows Server 2019 Datacenter Virtual Machine;

  • Open the Server Manager by clicking on the Start menu and selecting “Server Manager.”
  • In the Server Manager, click on “Manage” in the top-right corner and select “Add Roles and Features.” This will open the “Add Roles and Features Wizard.”
  • On the “Before You Begin” page, click “Next” to proceed.
  • On the “Installation Type” page, select “Role-based or feature-based installation” and click “Next.”
  • On the “Server Selection” page, select the appropriate server and click “Next.”
  • On the “Server Roles” page, scroll down and select “Web Server (IIS)” by checking the box next to it. When prompted, click “Add Features” to include the required features for IIS.
  • In the “Features” section, expand “.NET Framework 4.7 Features” and select the required ASP.NET features. You may select “ASP.NET 4.7” and any other ASP.NET features you need.
  • Click “Next” to proceed.
  • On the “Web Server Role (IIS)” page, click “Next” without making any changes, unless you need to configure specific IIS options.
  • On the “Role Services” page, review the selected role services and features, and click “Next.”
  • On the “Confirmation” page, review your selections, and click “Install” to begin the installation process.
  • Wait for the installation to complete. You will see a progress bar indicating the installation progress.
  • Once the installation is finished, you can close the “Add Roles and Features Wizard” and the Server Manager.

Step 7:
Set up an ASP.NET core application on a running Azure VM using IIS Web Server.

  • Launch an elevated PowerShell session from the start menu by right-clicking on the Windows PowerShell -> Run as administrator:
  • Enter the following command to download the ASP.NET core tools while also restarting the web server.
cd Desktop\
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -uri https://download.visualstudio.microsoft.com/download/pr/c8b4d4c4-1f74-4533-80a1-c5813500c7a1/e6d2d78c0fba3744390952fd2ccad7d8/dotnet-hosting-7.0.2-win.exe -outfile dotnet-core.exe
C:\Users\testuser\Desktop\dotnet-core.exe /install /quiet /norestart
net stop was /y
net start w3svc
  • Next, download the .NET SDK:
Invoke-WebRequest -Uri https://download.visualstudio.microsoft.com/download/pr/01dfbf9b-d2d1-4bd2-acb1-51d998c4812e/cf4fd6732540a78b4f44cbd9a285ce80/dotnet-sdk-6.0.405-win-x64.exe -outfile dotnetinstall.exe
dotnetinstall.exe /install /quiet /norestart
C:\Users\testuser\Desktop\dotnetinstall.exe /install /quiet /norestart
  • Enter the following command in the PowerShell terminal that creates a new webapp and hit Enter:
cd Desktop
dotnet new webapp -o aspnetcoreapp
create new dotnet webapp
  • Run the following command in the PowerShell terminal to build and export the sample .NET application:
cd aspnetcoreapp
dotnet publish --configuration Release
sample .NET application
  • Remove the current files from the IIS Server directory C:\inetpub\wwwroot\ and copy the .NET exported files to IIS Server hosted directory.
Remove-Item -path C:\inetpub\wwwroot\*

Copy-Item .\bin\Release\net6.0\publish\* -Destination
C:\inetpub\wwwroot\ -Recurse
  • Open the Internet Explorer browser, type localhost and press Enter to view the homepage of IIS Server:
Edge browser

Congratulations you have learnt how to create an Azure VM to host a .NET application using Azure CLI.

If you found this article helpful, click on the like button or you can drop a comment.

Follow for more interesting stories. Until next time, keep looking up!

--

--