Site icon CodePortal

Getting started with NodeJS application on EC2 server using AWS

In this tutorial, I’m going to show you how to create EC2 server and setup NodeJS application on it. At the end of this tutorial, we’ll have working example of NodeJS application on EC2 server.

Required things before we start:

  1. Knowledge about NodeJS
  2. Knowledge about AWS and its EC2 related services
  3. AWS account (not compulsory, you can create it later on while you practice it)
  4. Domain name (not compulsory)

Once you have above things ready, let’s go through steps quickly:

  1. Create and Start EC2 server
  2. Login into server via SSH key and putty
  3. Install NodeJS
  4. Create NodeJS server
  5. Keep NodeJS process running using pm2

Alright!! So without any delay, let’s start..

1. Create and Start EC2

Login into you AWS account via and create new EC2 server. To create EC2 server, go to Services and click on EC2. On this page, click on Launch Instance and you’ll see lots of different options to select from. Search for “ubuntu” on search bar like below

From search result, select “Ubuntu Server 20.04 LTS (HVM), SSD Volume Type” like I pointed on above screenshot. Once you select it, you’ll see list of Instance Type. Select t2.micro which is available for Free Tier for first year on new signup. Select t2.micro and click on “Review and Launch” button as shown as below screenshot.

In next screen, just click on “Launch” button. Upon clicking on it, you’ll see popup to create new key pair. Go ahead and create new key paid for your server. Give it a name and “Download Key Pair“. Keep it in safe place. After downloading it, click on “Launch Instances“.

Once you click on link as per above screenshot, you’ll go to Instances page where it shows you once your instance is in Running state.

Next, step is to login into your newly created account via SSH Key paid generated above.

2. Login into server via SSH key and putty

In this step, we’ll first create PPK file using the .pem file we’ve downloaded using PuTTYgen software. Open PuTTYgen and load .pem file you’ve downloaded and click on “Save Private Key

Now, we’ll use this PPK file to login into server using Putty software. Just follow below screenshots step by step and you’re good to go:

You can get Public IP from the EC2 dashboard like this —

Next, is to open Putty and mention ubuntu@IP_ADDRESS in Host Name in Session section

Now, go to Connection->SSH and click on Auth. Then select your PPK file you’ve created from PuTTYgen software.

After selecting file, go back to Session and click on “Open” button from bottom.

3. Install NodeJS

So, after login into server, first thing we would be doing is to install NodeJS and NPM. In this example we’ll install NodeJS version 12. Run below commands step by step to install NodeJS

curl -o- | bash

source ~/.bashrc

nvm install 12

node –version

After you run final command you will see v12.x.x as your node version.

4. Create NodeJS server

Now that we’ve successfully installed NodeJS, its time to create NodeJS server/application which will run on port 3477

const express = require('express');
const app = express();

app.get('/', (req, res) => {

app.listen(3477, () => console.log('Server running on port 3477'));

npm init

Just keep pressing enter to use default values until you have package.json file created. You can check this via executing ls command

npm install express –save-dev

Once you execute above, you’ll have node_modules folder created with all the required dependencies.

Go to AWS EC2 dashboard and select your Instance via clicking on Instance ID:

Scroll down and open Security tab:

Go to security groups and open it as per the screenshot below:

Now, edit Inbound Rule:

Add new rule via clicking on “Add Rule”:

Open port 3477 as per screenshot below and Save rules:

So, now that you’ve opened 3477 port, go to putty/ssh console and run following command

node index.js


Now, go to http://IP_ADDRESS:3477 to visit your NodeJS application served from AWS EC2 server. You’ll see following output in browser:

That’s it 🙂

In this tutorial, we’ve learned to create and setup brand new EC2 server and installed NodeJS & NPM in it. After that, we’ve created simple NodeJS application and made it serve on custom port 3477. We’ve also covered how to open custom port for EC2 server via modifying Inbound Rules!!

You may wondering how to keep NodeJS server in background so whenever you close putty or shutdown SSH, it won’t stop NodeJS server. So, you can do that in via using pm2 package.

5. Keep NodeJS process running via pm2

Just run following commands to install pm2 and add NodeJS process as a background job

npm i -g pm2

pm2 start index.js

pm2 startup

pm2 save

Now, you can relax and close your SSH console. pm2 will keep your NodeJS process active in background.

In next tutorial, we’ll go through setting up Nginx and assign real domain to our EC2 application so we can run application directly using domain name instead of IP Address!!

Exit mobile version