Step-by-Step Guide to Deploy a Node.js Server on Amazon EC2 with NGINX and pm2

Β·

6 min read

Hello there! 🌟

In this guide, we'll walk through the step-by-step process of deploying a Node.js application on an AWS EC2 Ubuntu and accessing it in your browser. Whether you're a beginner or looking to brush up on your skills, this tutorial will make the deployment process simple and clear. Let's get started! πŸš€

Deploy NodeJS APP on AWS EC2 Instance | NodeJS on EC2 | Running NodeJS APP  on AWS EC2 | AWS Projects

Before diving into the deployment process, let's first understand the basics of Node.js, AWS, EC2, and Ubuntu AMI.

Node.js

Node.js is an open-source, cross-platform JavaScript runtime environment Written in C++. that allows developers to build scalable and efficient server-side applications.

What Makes Node JS Considerable for Software Development?

It is mostly used for creating server-side APIs, and command-line tools and real-time applications.

Amazon Web Services (AWS)

Amazon Web Services (AWS) is a comprehensive cloud computing platform provided by Amazon.

Amazon Web Services - Wikipedia

It offers a wide range of services such as computing power, storage, databases, networking, machine learning, and security β€” all available on a pay-as-you-go basis. Key AWS services include EC2 (virtual servers), S3 (object storage), and RDS (managed databases), among many others.

AWS EC2 (Elastic Compute Cloud)

Amazon EC2 (Elastic Compute Cloud) is a web service that provides scalable virtual servers in the AWS cloud. These virtual servers, known as instances, allow you to run applications just like you would on a physical machine β€” but with greater flexibility and control.

Amazon EC2 | AWS Cheat Sheet

EC2 is ideal for hosting web applications, running development environments, or managing data processing tasks. It’s a powerful solution for deploying scalable and resilient systems in the cloud.

Ubuntu AMI (Amazon Machine Image)

An Ubuntu AMI (Amazon Machine Image) is a pre-configured virtual machine image provided by AWS that comes with the Ubuntu operating system installed. It serves as a template for launching EC2 instances with Ubuntu as the base OS.

When launching an EC2 instance with an Ubuntu AMI, you get a lightweight yet powerful OS optimized for performance and security in cloud environments.

So the steps that we need to perform here are as follows:

  1. Create a AWS EC2 instance

  2. Connect your EC2 Instance.

  3. Install NodeJS and NPM

  4. Install Git

  5. Clone the repository from GitHub

  6. Install all the required dependencies

  7. Run the application

  8. Set up a Node.js application with pm2 for process management and monitoring

  9. Install and configure NGINX to proxy requests to your Node.js application

  10. Access the application in browser

Now we will start performing all these steps one by one :

Getting Started with Amazon Web Services (AWS)

Explore the page AWS free cloud services to get started with AWS services. Create a free account.

Step 1: Sign in to AWS Console
Create a free account and Login to your AWS Management Console.

Step 2: Navigate to EC2 Dashboard
Once you login, you will be able to access the AWS console. Search for EC2 service in the search bar and select EC2 option.

Step 3: Launch Instance
Find and click on the Launch Instance option on the dashboard or inside the instances page.

Step 4: Choose an Amazon Machine Image (AMI)
Select an image for your instance. An image is a template for your instance with information like the operating system and required software for your virtual machine. For this example, choose a free-tier eligible Amazon Linux AMI or ubuntu.

Step 5: Choose an Instance Type
Select an instance type. The default, t2.micro, is eligible for the AWS Free Tier.

Step 6: Key Pair

It allows you to create Key pair (a private and a public key) for logging in to your virtual machine. If you have already created any Key pair select it from the dropdown. Or else, create a new Key pair.
Download the key pair file and store it as it can be used to log in to your instance.

Step 7: Review and Launch
Keep all the settings as default for now. review and launch the instance.

Connect to your EC2 Instance

We can connect to EC2 by clicking on the instance ID from instances page and then on connect option.

It is mostly used for creating server-side APIs, and command-line tools and real-time applications.

Once connected, run the following command to gain superuser (root) access:

sudo su

Install NodeJS and NPM

Now we need to install Node.js and NPM Update the package manager and install Node.js and NPM:

sudo apt update
sudo apt install nodejs
sudo apt install npm

This installs the latest stable version of Node.js.

Install Git**

Now we need to take the source code of the Node.js application from the GitHub so we have to install Git first so that we can clone the particular repository of the Node.js application. and ubuntu AMI Git is installed default.

Clone the repository from GitHub

After installing Git, now we will clone the repository of the Node.js application.


Repo Link:-
https://github.com/AbhijitSagare/Node.js-On-AWS.git

Use the below command to do so :

This command will clone(creating a copy) the repository on the machine by creating a folder by the name β€œnodejs-on-AWS”.

Now get into that folder and check the sub folders and files of the Node.js application. Use the command to check the files.

#cd nodejs-on-AWS
#ls -ltr

Install Dependencies and Start the Application Run the following commands:

npm install
node run start

our application is now running on port 4000. πŸŽ‰

Allow Public Access to Port 4000

By default, AWS blocks external access to ports. Follow these steps to allow public access to port 4000:

Open AWS Management Console

Go to EC2 Dashboard β†’ Click on your Instance

Click on the Security Group associated with the instance Click Edit Inbound Rules Add a new rule: Type: Custom TCP Port: 4000 Source: 0.0.0.0/0 (to allow all) Click Save Rules

Access Your Application in a Browser Find your EC2 Public IP Address from the AWS console.

Open the browser and enter:

http://your-ec2-public-ip:4000/

Run the Node.js App in the Background.

Using pm2 ensures the app keeps running even after you close the terminal.

  1. Install pm2:

     sudo npm install -g pm2
    

Step 2: Start the application using PM2 Start your project using pm2 start command followed by the path of the starting file of your project.

Step 8: Enable Traffic on Port 80 (Optional)

If you want to access your app via the default HTTP port:

  1. Install nginx:
sudo apt install -y nginx

Configure Nginx as a reverse proxy:

sudo nano /etc/nginx/nginx.conf

Add this block inside the server block:

location / {
    proxy_pass http://localhost:4000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

Restart Nginx:

sudo systemctl restart nginx

Access Your Node.js App

Visit http://<your-ec2-public-ip> in your browser.

πŸš€ Your Node.js app is now live on AWS EC2! If you face any issues, let me know. 😊

Β