DEV Community

Cover image for How to Set Up Nginx & SSL for Your JavaScript App on Azure VM : A Beginner’s Guide
kristarking
kristarking

Posted on

How to Set Up Nginx & SSL for Your JavaScript App on Azure VM : A Beginner’s Guide

Table of Contents

  1. Setting Up an Azure Virtual Machine Creating the VM Connecting to Your VM
  2. Updating the System and Installing Essential Packages
  3. Setting Up a Firewall
  4. Installing and Configuring Nginx Installing Nginx Allowing Web Traffic
  5. Deploying Your JavaScript Application Navigating to the Web Root Directory Cloning Your Application from GitHub Setting Proper File Permissions
  6. Configuring Nginx for Your Application Enabling the Configuration
  7. Setting Up SSL with Certbot Installing Certbot Enabling Auto-Renewal
  8. Final Security Configurations
  9. Conclusion

When building a web application, ensuring it is properly deployed and secured is crucial. In this guide, I will walk you through deploying a JavaScript application on an Azure Virtual Machine (VM) using Nginx as a web server. This guide is beginner-friendly, ensuring that even if you are new to cloud computing, you can follow along and get your application live on the internet with a secure SSL certificate that auto-renews and map the VM's public IP address to a domain name, ensuring that users can access your application through a custom domain instead of an IP address.

First, let's get an application to work with, Google search a JS app on github

Github search image

Github search image 2

Github search image 3

Step 1: Setting Up an Azure Virtual Machine

Creating the VM
To begin, log in to your Azure portal and navigate to Virtual Machines. Click Create > Azure Virtual Machine and provide the necessary details:

  • Image: Choose Ubuntu 22.04 LTS (or a later version for the latest security updates).
  • Size: Select a VM size based on your application’s resource requirements.
  • Authentication Type: Choose SSH Public Key for security.
  • Networking: Ensure SSH (port 22, Port 80 and Port 443) is allowed so you can connect remotely.

VM Image 1
VM Image 2

Once all configurations are set, click Create and wait for deployment to complete. Afterward, go to the VM Overview section to copy the Public IP Address—this will be needed to connect to the VM.

VM IP

Connecting to Your VM

To access your virtual machine, open a terminal (or Command Prompt if on Windows) and use SSH to connect:

ssh user@your_vm_ip
Enter fullscreen mode Exit fullscreen mode

Replace your_vm_ip with the actual public IP address of your VM. Once connected, you are inside your server and ready to begin setup.
SSH Image


Step 2: Updating the System and Installing Essential Packages

Before deploying your application, it is best to update the system to ensure everything runs smoothly:

sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

This command updates the package list and installs any available upgrades.

Next, install essential packages needed for deployment:

sudo apt install -y git curl ufw
Enter fullscreen mode Exit fullscreen mode
  • git allows you to clone your application from GitHub.
  • curl helps with downloading files and checking connections.
  • ufw is the Uncomplicated Firewall tool for securing your server.

TerminalImage description

Step 3: Setting Up a Firewall

To protect your VM, configure a firewall to allow only necessary traffic.

Enable OpenSSH access so you don’t get locked out:

sudo ufw allow OpenSSH
Enter fullscreen mode Exit fullscreen mode

Then enable the firewall:

sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

Your firewall is now protecting your server while still allowing SSH access.


Firewall Image description

Step 4: Installing and Configuring Nginx

Nginx is a powerful web server that will serve your JavaScript application.

Installing Nginx

To install Nginx, run:

sudo apt install -y nginx
Enter fullscreen mode Exit fullscreen mode

After installation, start the Nginx service:

sudo systemctl start nginx
Enter fullscreen mode Exit fullscreen mode

To check if it is running correctly:

systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Terminal Image description 3
If everything is set up properly, you should see a message indicating that Nginx is active.

nginx Image description

Allowing Web Traffic

Now, update the firewall rules to allow web traffic:

sudo ufw allow 'Nginx Full'
Enter fullscreen mode Exit fullscreen mode

This ensures users can access your site via HTTP and HTTPS.


Step 5: Deploying Your JavaScript Application

Your application files must be placed in the web server's root directory. To do this:

Navigating to the Web Root Directory

Move into the Nginx directory where website files are stored:

cd /var/www
Enter fullscreen mode Exit fullscreen mode

Cloning Your Application from GitHub

Use git to pull your project from GitHub:

sudo git clone your-git-repo
Enter fullscreen mode Exit fullscreen mode

Rename the folder to your preferred website name:

sudo mv devportfolio your-web-name (In my case, I'm using heros)
Enter fullscreen mode Exit fullscreen mode

This means your application will be served from /var/www/heros.

Command Image description 1

Setting Proper File Permissions

Adjust permissions to ensure Nginx can serve the files correctly:

sudo chown -R www-data:www-data /var/www/heros
sudo chmod -R 755 /var/www/heros
Enter fullscreen mode Exit fullscreen mode

This allows Nginx to read and serve the files while keeping them secure.


Command Image description 2

Step 6: Configuring Nginx for Your Application

Create a new configuration file for your application:

sudo nano /etc/nginx/sites-available/heros
Enter fullscreen mode Exit fullscreen mode

Inside the file, add:

server {
    listen 80;
    server_name heros.com.ng www.heros.com.ng;

    root /var/www/heros;
    index index.html;

    location /.well-known/acme-challenge/ {
        root /var/www/heros;
    }
}
Enter fullscreen mode Exit fullscreen mode

This tells Nginx to serve your application from /var/www/heros and listen on port 80.

Save and close the file.

Config Image description

Enabling the Configuration

Create a symbolic link to enable your new Nginx configuration:

sudo ln -s /etc/nginx/sites-available/heros /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

Test the Nginx configuration:

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

If there are no errors, reload Nginx:

sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Nginx Image description
Step 6b: Map your public IP to your domain name. Domain name providers are different, if you don't know how to map your IP to your domain name, contact your domain name provider, to email you the step-by-step guide to do that. In my case, I'm using smartweb as my domain name provider

IP mapping Image description

IP 2 Image description

IP 3Image description

Step 7: Setting Up SSL with Certbot

Securing your site with HTTPS is critical. We will use Certbot to obtain and automatically renew an SSL certificate.

Installing Certbot

First, install Certbot:

sudo snap install certbot --classic
Enter fullscreen mode Exit fullscreen mode

Then, obtain and install the SSL certificate:

sudo certbot --nginx -d heros.com.ng -d www.heros.com.ng
Enter fullscreen mode Exit fullscreen mode

Certbot will automatically configure your Nginx settings for SSL.

Enabling Auto-Renewal

Certbot automatically renews certificates, but we can verify by running a test renewal:

sudo certbot renew --dry-run
Enter fullscreen mode Exit fullscreen mode

This ensures your SSL certificate will renew without issues.


SSL Image description 1

SSL Image description 2

SSL Image description 3

Step 8: Final Security Configurations

Re-enable the firewall with all necessary rules:

sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

Firewal enabled Image description
Your application should now be live at:

https://heros.com.ng
Enter fullscreen mode Exit fullscreen mode

Dev Image description

Dev 2 description


By following this guide, you have successfully deployed a JavaScript application on an Azure VM with Nginx and SSL. You now have a fully functional and secure web application. Additionally, we have mapped the VM's public IP address to a domain name, ensuring that users can access your application through a custom domain instead of an IP address.

If you found this guide helpful, share your thoughts in the comments!

Happy coding! 🚀

Top comments (0)