Deploy Your Node.js App on Azure Virtual Machine with a Custom Domain

Deploy Your Node.js App on Azure Virtual Machine with a Custom Domain

Introduction

Welcome to this guide on deploying a custom Node.js application on the Azure Cloud platform. In this article, we will walk you through the steps to deploy your application and configure a custom domain for your virtual machine

What is Nodejs?

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. It is designed to build scalable network applications and is known for its event-driven, non-blocking I/O model, which makes it efficient and lightweight.

What is Azure?

Azure is a cloud computing platform by Microsoft offering a wide range of services for computing, analytics, storage, and networking. It supports various programming languages, frameworks, and operating systems, making it versatile for developers and enterprises. Key features include scalability, flexibility, robust security, global reach, integrated services, cost management, hybrid capabilities, and tools to enhance developer productivity. Azure helps businesses accelerate digital transformation, improve efficiency, and drive innovation.


Pre-requisites for Deployment

To follow along with this article, you need an Azure account, a custom domain, and knowledge of Node.js, NGINX, and the basics of Azure Cloud.

Azure Account Setup

  1. Visit the Azure Website and Sign Up: Go to the Azure website and click on "Start free" to begin the sign-up process. Sign in with your Microsoft account or create a new one.

  2. Verify Your Identity: Enter your details and verify your identity via phone call or text message as prompted.

  3. Enter Payment Information: Provide a credit card for identity verification. You won't be charged unless you exceed the free tier limits.

  4. Complete and Explore: Review the terms and conditions, complete the sign-up process, and start exploring Azure services.

Custom Domain Acquisition

For this guide, I am using a domain from Porkbun. You can buy any domain you want. I will show you the steps to configure the domain at the end.

Necessary Knowledge and Tools

To follow this article, you need an Azure account, a custom domain, and knowledge of Node.js, NGINX, and basic Azure Cloud concepts. You should also know basic Linux commands, Node.js commands, how NGINX and reverse proxy work, and how to use the Azure cloud interface.


Setting Up the Azure Virtual Machine

  1. Go to the Azure Portal: Visit this link and sign in to your Azure account.

  2. Create a Virtual Machine: Click on "Create a resource" and then select "Virtual Machine".

  3. Configure Basic Settings:

    • Subscription: Select "Azure for Students".

    • Resource Group: Create a new resource group by selecting "(New) Resource group" and providing a name.

  4. Instance Details:

    • Virtual Machine Name: Enter "Nishitbaria" or any name you prefer.

    • Region: Choose the region closest to you.

    • Availability Options: Select "Availability zone" and choose the number of zones as per your requirement.

  5. Image and Size:

    • Image: Select "Ubuntu LTS 22".

    • Size: Choose "Standard_D2s_v3 - 2 vcpus, 8 GiB memory (₹6,376.87/month)".

      💡
      You can select any size of the virtual machine based on your requirements. The size you choose will depend on the resources your application needs, such as CPU, memory, and storage. Make sure to evaluate your application's demands and select a size that provides the necessary performance and capacity.
  6. Administrator Account:

    • Username: Enter a username of your choice, for example, "exampleuser".

    • SSH Key Pair: Generate a new key pair and download the file. This key pair will be used to authenticate the user when connecting via SSH.

  7. Networking:

    • Select Inbound Ports: Open ports 22, 443, and 80.

      💡
      An inbound port on a virtual machine is a network port that allows incoming traffic from external sources to access services hosted on the VM. It’s essential for enabling connectivity to applications and managing these ports is crucial for security and functionality.
  8. Review and Create: Review all the settings and click on "Create" to deploy your virtual machine.

How to connect your Virtual Machine

  1. Access the Virtual Machine: Click on the virtual machine we created. It should look like this:

  2. Initiate Connection: Click on the "Connect" button.

  3. Select SSH Connection Method: Choose "SSH" from the connection options.

  4. Choose Your Operating System: Based on your operating system, select the appropriate option. In this example, we are using Windows.

  5. SSH Command: Use the following command to connect to your virtual machine. Replace [your_path] with the path where your key-value pair is saved and [username]@ with your VM's public IP address.

ssh -i [your_path/your_ssh_key] [username_vitualmachine]@[public_ip_address]

Example:

ssh -i C:\Users\nishit\Desktop\system\Nishitbaria_key.pem Nishitbaria@10.0.0.7

Establish Connection: Press "Enter". When prompted, type "yes" and press "Enter" again to confirm the connection.


Deploying the Node.js Application

Step 1: Install Node.js on Your Linux System

First, connect to your virtual machine. Follow the instructions in this DigitalOcean article to install Node.js and npm on Ubuntu 20.04. Here is a brief summary of the steps:

  1. Update your package index:

     sudo apt update
    
  2. Install Node.js and npm:

     sudo apt install nodejs npm
    
  3. Verify the installation:

     node -v
     npm -v
    

Step 2: Clone Your GitHub Repository

Clone your GitHub repository to deploy your application. Here’s a basic repository you can try:

  1. Clone the repository:

     git clone https://github.com/Killer2op200/Basic_express_app.git
    
  2. Navigate to the repository directory:

     cd Basic_express_app
    
  3. Install dependencies:

     npm install
    
  4. Start the application:

     npm run start
    

Step 3: Open a New Port on the Virtual Machine

To access your virtual machine using the IP address, you need to open the necessary ports. Specifically, you need to open HTTP (port 80) and HTTPS (port 443). Additionally, we’ll install Nginx to forward requests to port 3000, where our server is running.

Adding Inbound Rules

An inbound rule in a Network Security Group (NSG) controls the incoming traffic to your Azure resources. It specifies which types of traffic are allowed or denied to enter your VM based on parameters like source, destination, port range, and protocol. By setting these rules, you can manage the security and accessibility of your VM.

Step 4: Install Nginx on Your Virtual Machine

To make requests to 4.242.56.62 automatically redirect to 4.242.56.62:3000, we’ll set up a reverse proxy using Nginx.

  1. Update your package index:

     sudo apt update
    
  2. Install Nginx:

     sudo apt install nginx
    

Configure Nginx

  1. Create a new Nginx configuration file:

     sudo nano /etc/nginx/sites-available/reverse-proxy
    
  2. Add the following configuration to this file:

     server {
         listen 80;
         server_name [your_public_ip_address];
    
         location / {
             proxy_pass [your_public_ip_address:3000]
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header X-Forwarded-Proto $scheme;
         }
     }
    
  3. Enable the configuration:

     sudo ln -s /etc/nginx/sites-available/reverse-proxy /etc/nginx/sites-enabled/
    
  4. Test the Nginx configuration:

     sudo nginx -t
    
  5. Restart Nginx to apply the changes:

     sudo systemctl restart nginx
    

With this setup, when you navigate to http://[your_virtual_machine_ip], Nginx will forward the request to http://[your_virtual_machine_ip]:3000. Make sure to replace [your_virtual_machine_ip] with the actual public IP address of your virtual machine.


Connecting a Custom Domain

To configure your domain nodeapp.nishitbaria.co to point to your IP address [your_public_id_address], follow these steps:

  1. Log in to Your Domain Registrar:

    • Log in to the account where you registered your domain (e.g., GoDaddy, Namecheap).
  2. Access DNS Management:

    • Go to the DNS management section for your domain in my case its nishitbaria.co.
  3. Add an A Record:

    • Type: A

    • Name: nodeapp

    • Value: [your_public_ip_address]

    • TTL: 1 Hour (or leave default)

  4. Verify the Configuration:

    • Wait for DNS propagation (a few minutes to a couple of hours).

    • Open your web browser and navigate to http://nodeapp.nishitbaria.co to see your Node.js application.

With these steps, your custom domain nodeapp.nishitbaria.co will be configured to point to your Node.js application running on [your_public_ip_address].


Conclusion

In this article, you have learned how to deploy a custom Node.js application on the Azure Cloud platform and set up a custom domain for your virtual machine. This involved setting up an Azure account, creating a virtual machine, deploying the application, configuring Nginx for reverse proxy, and linking a custom domain to your VM.

Thank you for reading my article! If you found it helpful or interesting, please consider sharing it with your developer friends. For more content like this, don't forget to follow me on Hash Node.

If you're interested in learning more about deploying applications and other web development topics, consider subscribing to my blog or newsletter. You'll receive updates whenever I publish new articles.

I'd love to hear about your experiences with deploying applications on Azure. Feel free to share your thoughts or any interesting use cases in the comments section below.

Connect with me on Twitter, GitHub, LinkedIn.

Thank you for reading!