DEV Community

1suleyman
1suleyman

Posted on

Exercise 01: Configure an Azure Linux Virtual Machine

Introduction

In this exercise, I learned how to create a Linux-based virtual machine (VM) in Azure, connect to it using SSH, and install the Nginx web server. This exercise helped me gain hands-on experience with the creation and management of Linux VMs, as well as performing basic administrative tasks such as installing OS updates and setting up a web server.

Scenario

For this task, I was tasked with setting up a web server for a new eCommerce website. This required provisioning a Linux VM, using SSH to securely connect to it, and then installing the Nginx web server. Lastly, I needed to ensure that the virtual machine was up-to-date and properly configured to serve web content.

Skills Practiced:

  • Using the Azure portal to create a Linux virtual machine.
  • Connecting to the virtual machine via SSH.
  • Installing the Nginx web service on the VM.
  • Running OS updates on the Linux VM.

Step-by-Step Guide

Step 1: Use the Azure Portal to Create a Virtual Machine

1️⃣ Sign in to the Azure Portal

First, I signed into the Azure portal here.

2️⃣ Create a Virtual Machine

In the Azure portal, I searched for "Virtual Machines" and selected Create. I filled out the Basics tab as follows:

Setting Value
Subscription [Your Azure Subscription]
Resource Group RG1
Virtual Machine Name VM1
Region East US
Availability Options No infrastructure redundancy required
Security Type Standard
Image Ubuntu Server 24.04 LTS - x64 Gen2
Size Standard_DS2_v2 (2 vCPUs, 8 GB RAM)
Authentication Type SSH public key
Username adminuser
SSH Public Key Source Generate new key pair
SSH Key Type RSA SSH Format
Key Pair Name VM1_key
Public Inbound Ports None

3️⃣ Configure Disks, Networking, and Management

  • For disks, I selected Premium SSD (30 GiB default).
  • For Networking, I chose the default Virtual Network (VNet) and ensured Public IP was selected.
  • I disabled auto-shutdown under Management.

4️⃣ Review and Create

After reviewing the settings, I clicked Create and waited for the deployment to complete. Once completed, I clicked on Go to Resource to see the details of my newly created VM.


Step 2: Connect to the Virtual Machine and Install OS Updates

1️⃣ Configure Network Security Group (NSG) to Allow SSH

Before I could connect via SSH, I needed to configure port 22 (SSH) to allow access. I did this by navigating to the Network settings and adding a new Inbound port rule for SSH.

2️⃣ Connect Using SSH

To connect to the VM, I used SSH with the key I generated earlier. Here's how I connected from my Mac terminal:

   chmod 400 ~/Downloads/VM1_key.pem
   ssh -i ~/Downloads/VM1_key.pem adminuser@<public_ip_address>
Enter fullscreen mode Exit fullscreen mode

I replaced <public_ip_address> with the actual IP address of my VM from the Overview page in Azure.

3️⃣ Update the Operating System

Once connected to the VM, I updated the OS by running these commands:

   sudo apt update
   sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode

I confirmed the installation by typing 'Y' when prompted.


Step 3: Install the Nginx Web Service and Test

1️⃣ Install Nginx

I installed the Nginx web server with the following command:

   sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

When prompted, I typed 'Y' to continue with the installation.

2️⃣ Start Nginx

I started the Nginx service using the command:

   sudo systemctl start nginx
Enter fullscreen mode Exit fullscreen mode

3️⃣ Configure Nginx to Launch on Boot

I made Nginx start automatically whenever the VM reboots by running:

   sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

4️⃣ Check Nginx Status

I verified that the Nginx service was running with the command:

   service nginx status
Enter fullscreen mode Exit fullscreen mode

5️⃣ Test Nginx

To test Nginx, I opened a browser and navigated to http://. I should see the default Nginx welcome page.


Challenges Encountered and Resolved

  • Permission Issues with SSH Key: When I tried to SSH into the VM, I encountered the error: "Permission denied (publickey)." This was caused by incorrect file permissions for the SSH key. I fixed this by running the following command on my Mac:
   chmod 400 ~/Downloads/VM1_key.pem
Enter fullscreen mode Exit fullscreen mode

This ensured that the key was readable only by me, allowing SSH to work.

  • SSH Connection Issues in Azure Cloud Shell: I initially attempted to connect via Azure Cloud Shell, but ran into issues because Cloud Shell cannot access local private keys. To resolve this, I switched to my Mac's Terminal and connected using the appropriate key file.

Key Learnings

1️⃣ Azure Virtual Machines

Azure VMs are on-demand scalable computing resources. I learned how to create and configure a Linux-based virtual machine using the Azure portal, specifying details such as size, image, and authentication method.

2️⃣ SSH Access to Linux VMs

SSH is a secure method to connect to Linux VMs in Azure. I successfully configured the VM to allow SSH access, created a new SSH key, and connected to the VM from my local machine.

3️⃣ Managing the VM with OS Updates

I installed essential OS updates to keep the system secure and up-to-date. This is a crucial task for maintaining the health of the virtual machine.

4️⃣ Web Server Deployment

I learned how to install the Nginx web server and configured it to start automatically when the VM boots up. This is an essential skill for setting up web applications in a production environment.


Conclusion

This exercise provided me with practical experience in deploying and managing a Linux-based virtual machine in Azure. I gained hands-on skills in using SSH to securely connect to the VM, updating the operating system, and setting up the Nginx web server to serve content.

Through this, I now have a solid understanding of provisioning, managing, and securing a Linux VM in Azure, a key skill for deploying web applications in the cloud.

🚀 Stay tuned for more as I continue my learning journey with Azure!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Bump.sh

Hate writing docs?

Hate undocumented APIs even more?

Bump.sh generates an always up-to-date API reference site for REST and Event-Driven Architectures.

Plug it in your CI. It fetches your OpenAPI and AsyncAPI (GraphQL pending) spec files, and even generates a diff. Gather all of your API docs in a single source of truth.

Try it for free

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay