How to setup Node.js server on AWS EC2

How to setup Node.js server on AWS EC2

August 20, 2018 / Nirav Shah

How to setup Node.js server on AWS EC2?

Node.js is a JavaScript runtime environment that allows one to run JS on the server. It is built on the open-source V8 JavaScript engine used in Chrome and written in C++ which executes JS in a standalone environment.

Node.js is an open source server environment and free. Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.). It uses JavaScript on the server and can generate dynamic page content.

Node.js can create, open, read, write, delete, and close files on the server. It can collect form data. Thiscan add, delete, modify data in your database

AWS (Amazon Web Services), a subsidiary of Amazon, is a cloud computing platform which provides resources for computation such as EC2 instances, various database and storage facilities as well as networking and content delivery options.

Nginx is a free and open-source HTTP server which doubles up as a reverse proxy server or as a mail proxy server. It is a high performance server with simple configuration options and low resource consumption.

EC2 (Elastic Compute Cloud) is a web service which provides computing power in the form of scalable server instances. Multiple EC2 instances are run on the same machine inside Amazon’s computing infrastructure. When an EC2 instance is initiated, a set amount of resources (OS, RAM, Disk Space etc., chosen at the time of initiation) are allotted to the user from the machine it runs on. This allows sharing of resources between multiple users.

Linux AMI is a copy of Linux (image) provided by AWS which can be used for free on an EC2 instance as an OS. It can contain pre-installed packages such as Python, MySQL, Node.js etc., depending on the chosen AMI.

Steps To Deploy Node JS Application On AWS EC2 Server

  1. SSH into your instance
  2. Install Node.js
  3. Install Git and clone repository from GitHub
  4. Start the node.js app
  5. Keep the app running

1.SSH into your instance

Depending on the OS you are using on your PC, there are different methods to SSH into your instance. You can see the applicable method by clicking on Connect in the above screen.

In terminal run command below to login to your linux instance –

$ ssh -i username@hostname

PuTTY frequent disconnection / timeout
If you use PuTTY, you will see that the terminal frequently becomes unresponsive because of a session timeout. You can change the session settings to keep the connection alive by going to the category Connection of the loaded session and change the value of Seconds between keepalives to 30. This will enable sending empty packets to the remote server via SSH every 30 seconds to keep the session alive.

2. Install Node.js

To set up Node.js on your Linux instance

> Install node version manager (nvm) by typing the following at the command line.

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash

> We will use nvm to install Node.js because nvm can install multiple versions of Node.js and allow you to switch between them.
Activate nvm by typing the following at the command line.

$ . ~/.nvm/nvm.sh

> Use nvm to install the version of Node.js you intend to use by typing the following at the command line.

$ nvm install 4.4.5

> Installing Node.js also installs the Node Package Manager (npm) so you can install additional modules as needed. Test that Node.js is installed and running correctly by typing the following at the command line.

$ node -e “console.log(‘Running Node.js ‘ + process.version)”

This should display the following message that confirms the installed version of Node.js running.

Running Node.js v4.4.5

3. Install Git and clone repository from GitHub

We will install Git to clone your node.js repository from your GitHub account. To install Git, run the following command.

$ sudo yum install git

This will install the latest version of Git on the instance. Once installation is done, you are ready to clone your repository. Run the following command to clone your repository. Replace with your GitHub username, with the name of your node.js app repository and with the name of the folder you want to clone the repository into. This folder will be created under /home/ec2-user/.

$ git clone https://github.com//.git

Run cd to move inside this folder and ls to see its contents.

4. Run the node.js webapp

If your start script does not include npm install you should run it manually first, otherwise you can directly issue the command to start the app which should install the dependencies automatically. There are several ways to start the app depending on how you have set it up. Choose one which suits you.

$ node server.js

$ node app.js

$ node ./bin/www

$ npm start

The app will now run at the port number that you have specified in the start script. It could be 3000, 8000, 8080 etc. Make a note of this port number and enter the public DNS of the instance with the port number in the browser to see the app output. We use port 3000, you can replace it with yours and your public link will look something like following.

http://ec2-54-191-104-59.us-west-2.compute.amazonaws.com:3000

As you see, this link will not work and there would be nothing to show on the page and the browser connection will timeout. This is because we haven’t opened port 3000 for access to public. This is where the Security Group comes in. In the AWS management console, go to Security Groups, select the security group that you have created for your EC2 instance (launch-wizard-1 or similar) and edit the Inbound rules.

5. Keep the app running

The app will run as long as you have the terminal open and you have not stopped it manually. But as soon as you close the terminal or press Ctrl-C or its equivalent, the app will stop as its process is not running in the background, it’s tied to the active terminal. You can write a script to run the process in the background but you will also have to take care of restarting the process in the background in case it crashes or if you perform a pull from the repository or if you update the app in some other way. But there’s an easier way to do all this by using a process manager for node called PM2. We will focus on the auto-restart and background process functionalities of PM2 here. More functionalities in a later article on continuous integration.

Kill all the running node processes (if any)

$ killall -9 node

Install PM2 globally with the following command:

$ npm install pm2 -g

Move to the cloned folder and execute one of the following commands depending on how you had started the app before:

$ pm2 start server.js

$ pm2 start app.js

$ pm2 start ./bin/www

$ pm2 start npm –name “” — start

Now even if you close the terminal and check the url in the browser, the app will be running. Sweet! For automatically running PM2 when the server restarts, issue the following command:

$ pm2 startup

This will output a script in the terminal which you need to run, copy the script that it outputs and run it in the terminal window. Now, save all the currently running processes so that they can be run again whenever PM2 restarts either manually or by a script with the following command:

$ pm2 save

Now you can try rebooting the instance and visit the url and see if the app works or not. To see a list of the currently active node processes with their process names, run pm2 list and to monitor any one of the processes run pm2 monit.

We hope that above steps would help you to install a NodeJS server on an AWS EC2 instance and deploy your webapp to it using GIT. Contact us if you want all of the above to be handled by professionals and use your precious time in something more significant….. like making your business more successful. And yes you would definitely count on us, as we are AWS standard consulting partner.

Install Node Package Manager in AWS Linux machine

Talk to AWS Certified Consultant

    Spread Love By Sharing:

    Let Us Talk About Your AWS Development Requirements

    Have queries about your AWS project ideas and concepts? Please drop in your project details to discuss with our AWS experts, professionals and consultants.

    • Swift Hiring and Onboarding
    • Experienced and Trained AWS Team
    • Quality Consulting and Programming
    Let’s Connect and Discuss Your Project