Deploy a NodeJS Application on AWS EC2 using Amazon Linux 2 AMI and NGINX

Rajesh Mishra
4 min readJun 18, 2021

In this article, I’ll walk you through a basic setup guide for deploying a NodeJS Application on AWS EC2 using NGINX. The AMI used here is Amazon Linux 2 AMI.

What’s so uncommon about this?

The main reason behind writing this article is, I want to keep a note of the process and the issue I faced while doing it for the first time; also might help somebody having the same issue as I had. [I’m new to this domain. In a learning phase :)].

What was the issue?

Earlier I had experience in setting up and deploying a node app using Ubuntu AMI. For learning purposes, I was trying it out using Linux 2 AMI. There were mainly 2 issues I encountered:

  • Installing Nginx: Couldn’t just install Nginx directly using yum
  • Configuring Nginx: Even after installing Nginx, the right way, I couldn’t find the path to sites-enabled or sites-available.

What’s the solution?

1. Installing Nginx

After a lot of googling and a with the help of one of my colleagues (Subham Kumar Sahu), I found 2 ways to do this.

  1. Using Amazon Linux Extras. Use the following command to install Nginx:
    $ sudo amazon-linux-extras install rust1
    To know more about Amazon Linux Extras follow this link.
  2. The CentOS way: Since Amazon Linux 2 is just like CentOS, we can follow the steps to get Nginx installed on our EC2 instance.

To verify this, type the following command after you connect to your instance from a CLI.
$ cat /etc/os-release

It will return the following information:

NAME=”Amazon Linux”
ID_LIKE=”centos rhel fedora”
PRETTY_NAME=”Amazon Linux 2"

To follow along with the installation in CentOS, go to the link:

To add NGINX yum repository, create a file with the following command
$ sudo vim /etc/yum.repos.d/nginx.repo

And then copy and paste the following configuration into that and save it

name=nginx repo

Now run the following command to install Nginx
$ sudo yum install nginx

To verify installation
$ nginx -v

To start Nginx, type
$ sudo service nginx start

Go to your browser and type in your Public IP, you’ll be able to see the Nginx welcome page. So the installation part is done.

Now let’s move to the second issue. (Configuring the Nginx to point to the port where our application is running).

Note: At this point in time, I expect you to know how to take a pull of your code from Github or any cloud repositories you’re using. If not follow along or else skip it.

Cloning your repository to the EC2 Instance

  1. Go to Github or any repositories you’re using
  2. Copy the project URL
  3. Type git clone <YOUR REPOSITORY URL>
  4. cd into the folder
  5. Do npm install
  6. To check if your application is working perfectly, run it by npm start or node app.js
  7. Then install pm2 process manager to keep your app up and running
  8. Open up your browser and enter your public IP followed by your app’s port number (e.g. http://<PUBLICIP>:<PORT NUMBER>). You will be able to see your application loading.

Now usually in order to point directly to our application port. We are going to use port forwarding to achieve this. HTTP uses port 80 by default and we need to forward all the requests made to our server to port 300 or <YOUR PORT> where our app is running. We will be using Nginx as a reverse proxy in front of our application server.

To do this, go to /etc/nginx/sites-enabled. Did you find the path? I couldn’t. So here comes the solution to this.

While using Debian/Ubuntu, these are automatically created, but here we need to manually create these and do the configuration.

Create the sites-enabled folder. Go to the Nginx folder:
$ cd /etc/nginx

Then create the folder:
$ sudo mkdir sites-enabled

Also in the Nginx folder, open nginx.conf file:
$ sudo vim nginx.conf

And inside http add the following line:
include /etc/nginx/sites-enabled/*;

Now cd into sites-enabled and create a default file:
$ sudo touch default
Then open the default file:
$ sudo vim default

Paste the following Nginx configuration and save it:

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
location / {
proxy_pass<YOUR PORT>;
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;

Now restart the Nginx server to apply the changes:
$ sudo service nginx restart

Done! Now you can go to your browser and type in your Public IP. It will directly load your application.


  • How to install NodeJS and NPM in the EC instance: Click Here
  • How to install PM2 Process Manager
    $ sudo npm install pm2 -g

Please note, this might not be the optimal solution, but this worked out for me. I intended to do it with the bare minimum things required. JIC, you have a better approach please go ahead with it and do not forget to share in the comments.




Rajesh Mishra

A coder by profession, sharing life experiences with the Words. Learning more about Productivity, Habits, Decision Making and ambitious towards self freedom.