DEV Community

Rajat Kumar Nayak
Rajat Kumar Nayak

Posted on • Updated on

Effortless Node.js Deployment on Amazon EC2 with tmux: A Step-by-Step Guide

In this blog, we will have a clear and step-by-step understanding of how to host our node application on an Amazon EC2 instance and how to keep the application running.

Introduction:
Imagine you're a chef in a busy kitchen, about to serve a delicious dish. But just as you're ready, someone unplugs the oven!

Problem:
That's what it's like hosting an application on EC2. When you close the terminal, your app goes offline, leaving users in the lurch.

Solution:
Enter tmux, your kitchen assistant! It keeps your app running in the background, like a backup generator for your oven. With tmux, your app stays online, serving users seamlessly.

So, when hosting on EC2, don't forget tmux. It's the secret ingredient for uninterrupted service!

Now Let's look at the steps to implement it..

The entire steps will be broadly classified into 3 main categories.

Step-1 Develop a Node JS Application.

  • Initiate a Node Application
    npm init -y

  • Install express dotenv, express npm package.
    npm install express dotenv

  • Create a .env file.

PORT=3000
Enter fullscreen mode Exit fullscreen mode

Image description

Create a sample node application.

require("dotenv").config();
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3001;

app.get("/", (req, res) => {
  res.send("Welcome to AWS World");
});


app.listen(PORT, () => {
  console.log(`Server running on ${PORT}`);
});

Enter fullscreen mode Exit fullscreen mode

Now your node application is ready to push into the remote repository.

  • Initialize a git repo using:
    git init

  • Make sure to add .gitignore file and add node_modules folder inside .gitignore.

Image description

  • Create the main branch
    git checkout -b main

  • Add the remote origin:
    git remote add origin <repo-link>

  • Then Commit the changes with the following commands:
    git add .
    git commit -m <commit message>

  • Push the commit into the remote repo using:
    git push origin <branch-name>

Now we pushed the code successfully into the remote repository.

Step-2 It's time to start with the AWS EC2 hoisting part.

I have attached a video below which describes how to create an EC2 instance.

After you have the instance now it time to setup the ports to open for our application to display.

  • Head over to the security groups option in the instance dashboard.
    Image description

  • Click on Edit Inbound Rules Option.

Image description

  • Add a new rule.

Image description

Now we are ready with setting up the EC2 instance now let's move to connecting our instance and hoisting the application.

Setting up SSH and Hoisting

Set up SSH in your local system.

You can check whether SSH is present in your system by running ssh in your command prompt.

Image description

If the output is not the same as above then you can setup SSH by following the steps mentioned in the provided blog link.

Setup SSH

After you are done with SSH follow the below-mentioned steps.

  • Run the bellow command to connect to the instance. ssh -i <key-path> ubuntu@<public-ip>

Image description

  • Make a directory of your choice name.
    mkdir node-app

  • Move inside the director.
    cd node-app

You can check for git using the git --version command.
If not found you can install using sudo apt install git

Image description

  • clone the remote app that we created previously.

Image description

  • Move inside the cloned folder using cd node-app

Image description

  • Then you can install packages using
    npm install

  • It will install all necessary packages.

  • Install tmux using
    sudo apt install tmux

Image description

  • Start a new tmux session: tmux new-session -s session_name

Image description

  • Run the node application in the tmux session using
    node index.js

  • To detach from the tmux session without stopping the Node.js app, press Ctrl+b then d.

Hurray you have hoisted the application with continuous running.

Image description

  • You can again attach to a pre-existing session using the following command. tmux attach -t session_name

So we have pretty much covered how to host a node application in an EC2 instance and run it continuously with Node JS. If you have any doubt I will be more than happy to help. You can mail your queries to 'rajatnayak1582002@gmail.com'.

Top comments (0)