How To Create AWS EC2 Instance And Host NodeJS Application With Nginx

Hello everyone. I hope you all are safe. In this article, we are going to discuss how to create an AWS instance and host your Node.js application with Nginx. You will learn how to get your Node.js application up and running in a few minutes with AWS. 
 

What is AWS?

 
So in one line, AWS stands for Amazon Web Services and it is the cloud computing platform from Amazon; for more you can read here.
 

What is EC2?

 
EC2 stands for Amazon Elastic Compute Cloud (Amazon EC2) and it is a web service that provides various compute capacity in the cloud. It lets us create the desired computing environment quickly. For example, if you need a server up and running for you to test or deploy your application it will take only a minute or two to set up your server in EC2.
 
So let us create a virtual machine and host a node application. At first, you need to have an AWS account. if you're a student then there is an easier way to get an AWS account using GitHub Student Developer Pack you can check it out here.
 
Step 1
 
Go to AWS and click on AWS Management Console under My Account then choose Root user or IAM user based on the description and enter your email. 
 
step1
Click on next and then enter your password and click on Sign in.
 
step2
Step 2
 
After signing in you will see this screen under the compute; click on EC2 then you will be presented with the below screen. 
 
step2
 
Click on launch instance.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Step 3
 
Then choose your preferred server image, I want to install an Ubuntu server so I will proceed with this.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
Then I will choose the free tier. What is the free tier?
 
In this tier, for the first 12 months following your AWS sign-up date, you get up to 750 hours of micro instances each month. When your free usage tier expires or if your usage
exceeds the free tier restrictions, you pay standard, pay-as-you-go service rates.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Step 4
 
Click on Configure instance details I will leave everything default here.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Step 5
 
Click on next to add storage. The default provided storage is 8 GB you can add more as per your need.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Step 6
 
Now click on the next to add a tag. A tag consists of a case-sensitive key-value pair. For example, you could define a tag with key = Name and value = Webserver. So I am adding node instance.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Step 7
 
Click on next Configure Security Group
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Step 8
 
Then click on review and launch now.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
You will be asked to choose a key pair for authorization purposes. Choose to create a new key-pair and enter the name of your Key Pair and then click on download Key Pair.
 
You need to keep it safe.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Click on view instance and you will see your instance running 
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
You will see the newly created instance is running.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Step 9
 
Choose your instance and click on connect, now we will be using SSH client in our case it will be putty.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
If you're on windows then you can download putty here and if you're Linux then you can install putty using your terminal. For Linux, these are the commands.
  1. sudo apt-get update
  2. Sudo apt-get install -y putty
  3. putty
Now to connect you need to convert your keypairname.pem file to keypairname.ppk for authorization purposes. You can read how to do it. So after converting launch Putty and follow the steps to connect.
 
Step 10
 
Open putty and enter the public IP of your EC2 instance, in my case, it is 3.19.62.96. In Putty, go to SSH and click auth and locate your keypairnname.ppk.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
This is my Putty screen:
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Click on open and a Putty security alert window will be prompted click on accept and wait for some time. 
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
After this, you must see the login and you need to enter your username which is ubuntu by default. Enter your username and hit enter and wait for some time. 
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
If everything looks good then you will see the below screen and finally, you're running your Ubuntu server in EC2.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Step 11 - Now install the Nginx
  
Enter the following commands to install Nginx.
  1. $ sudo apt-get update && sudo apt-get upgrade -y  
  2. $ sudo apt-get install nginx -y  
 Now let us check if it is installed or not and if yes then we are going to start it. So use these commands.
  1. $ sudo systemctl status nginx    # To check the status of nginx  
  2. $ sudo systemctl start nginx     # To start nginx  
 Now let us enable Nginx using the following commands.
  1. $ sudo systemctl enable nginx  
Nginx should be up and running, now we need to setup Nodejs on the server.
 
Install Nodejs using the following commands,
  1. $ sudo apt-get update  
  2. $ sudo apt-get install nodejs  
 Now install npm using the following command,
  1. $ sudo apt-get install npm  
Verify Nodejs and npm installations using the below commands
  1. $ node --version  
  2. $ npm --version  
 It should return the Nodejs and npm versions installed. Now we need to setup Nginx as a reverse proxy for Node js application.use these commands for this.
  1. $ cd /etc/nginx/sites-available/  
  2. $ sudo nano default  
 Now add the following code after server_name_; to the file and press CTRL+O to write and CTRL+X to exit.
  1. location / {  
  2.         proxy_pass http://private_ip_address:8080;  
  3.         proxy_http_version 1.1;  
  4.         proxy_set_header Upgrade $http_upgrade;  
  5.         proxy_set_header Connection 'upgrade';  
  6.         proxy_set_header Host $host;  
  7.         proxy_cache_bypass $http_upgrade;  
  8.      }  
Replace //private_ip_address with your private IP of the EC2 instance. final file looks something like this.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Test the configuration of Nginx using the command below,
  1. $ sudo nginx -t  
Then reload Nginx if OK is displayed.
  1. $ sudo /etc/init.d/nginx reload  
So, it's time for creating our Node application, and for this, I am creating a directory called testapp and will be creating my app within this directory and also installing express. 
  1. $ mkdir testapp  
  2. $ cd testapp  
  3. $ npm install express  
Now let's create app.js and add some code.
  1. sudo nano app.js  
Add the following code and press CTRL+O and CTRL+X.
  1. var express = require('express');  
  2. var app = express();  
  3. app.get('/', function(req, res){  
  4.    res.send("Hello World!");  
  5. });  
  6. app.listen(8080, 'private_ip_address');// replace private ip with your private ip of EC2 instance  
 So, the final code for me looks something like this,
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
 
Now run your app using the following command.
  1. $ node app.js  
Now go to your browser and enter your IP you must see your app running output is something like this.
 
How To Create AWS EC2 Instance And Host NodeJS Application With Nginx
Now you can install pm2 for running your application automatically when the server restarts.
  1. $ sudo npm install pm2 -g  
If it is installed navigate to your project directory enter the following command. 
  1. $ pm2 start app.js  
That's it, I hope this article is helpful.
 
Thanks for reading, stay safe.