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.
Click on next and then enter your password and click on Sign in.
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.
Click on launch instance.
Step 3
Then choose your preferred server image, I want to install an Ubuntu server so I will proceed with this.
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.
Step 4
Click on Configure instance details I will leave everything default here.
Step 5
Click on next to add storage. The default provided storage is 8 GB you can add more as per your need.
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.
Step 7
Click on next Configure Security Group
Step 8
Then click on review and launch now.
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.
Click on view instance and you will see your instance running
You will see the newly created instance is running.
Step 9
Choose your instance and click on connect, now we will be using SSH client in our case it will be putty.
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.
- sudo apt-get update
- Sudo apt-get install -y putty
- 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.
This is my Putty screen:
Click on open and a Putty security alert window will be prompted click on accept and wait for some time.
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.
If everything looks good then you will see the below screen and finally, you're running your Ubuntu server in EC2.
Step 11 - Now install the Nginx
Enter the following commands to install Nginx.
- $ sudo apt-get update && sudo apt-get upgrade -y
- $ 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.
- $ sudo systemctl status nginx # To check the status of nginx
- $ sudo systemctl start nginx # To start nginx
Now let us enable Nginx using the following commands.
- $ 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,
- $ sudo apt-get update
- $ sudo apt-get install nodejs
Now install npm using the following command,
- $ sudo apt-get install npm
Verify Nodejs and npm installations using the below commands
- $ node --version
- $ 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.
- $ cd /etc/nginx/sites-available/
- $ 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.
- location / {
- proxy_pass http:
- 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;
- }
Replace //private_ip_address with your private IP of the EC2 instance. final file looks something like this.
Test the configuration of Nginx using the command below,
Then reload Nginx if OK is displayed.
- $ 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.
- $ mkdir testapp
- $ cd testapp
- $ npm install express
Now let's create app.js and add some code.
Add the following code and press CTRL+O and CTRL+X.
- var express = require('express');
- var app = express();
- app.get('/', function(req, res){
- res.send("Hello World!");
- });
- app.listen(8080, 'private_ip_address');
So, the final code for me looks something like this,
Now run your app using the following command.
Now go to your browser and enter your IP you must see your app running output is something like this.
Now you can install pm2 for running your application automatically when the server restarts.
- $ sudo npm install pm2 -g
If it is installed navigate to your project directory enter the following command.
That's it, I hope this article is helpful.
Thanks for reading, stay safe.