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

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?

What was the issue?

  • 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”
VERSION=”2"
ID=”amzn”
ID_LIKE=”centos rhel fedora”
VERSION_ID=”2"
PRETTY_NAME=”Amazon Linux 2"
ANSI_COLOR=”0;33"
CPE_NAME=”cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL=”https://amazonlinux.com/"

To follow along with the installation in CentOS, go to the link:
https://www.nginx.com/nginx-wiki/build/dirhtml/start/topics/tutorials/install/

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

[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

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. Copy the project URL
  2. Type git clone <YOUR REPOSITORY URL>
  3. cd into the folder
  4. Do npm install
  5. To check if your application is working perfectly, run it by npm start or node app.js
  6. Then install pm2 process manager to keep your app up and running
  7. 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 http://127.0.0.1:<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.

Bonus:

  • 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.

Credits:

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