Effortless Node.js Deployment on Amazon EC2 with tmux: A Step-by-Step Guide
Tags: node, aws, devops, javascript
Published: 2024-02-04 13:37:54

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
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rtbfmzyh91c4l5t53fq3.png)

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}`);
});

```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ud0wyvyjsu6uzk0jxzmd.png)

- 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.

{% embed https://youtu.be/vBd9sintVAA %}

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xg2nna06rk9i274pv4l3.png)

- Click on **Edit Inbound Rules** Option.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fwqc3gqqlbwp0hzuj9p.png)

- Add a new rule.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yw0ch8tkky0hyyuw5w7x.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xmc0yozewbc6jw94vts.png)

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](https://interworks.com/blog/2021/09/08/how-to-enable-ssh-commands-in-windows/)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijhvpg4ulj0jkeisecs9.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlx8bcu56szxx4oga5ia.png)

- clone the remote app that we created previously.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c9y7hm6kednw17obwy9e.png)

- Move inside the cloned folder using `cd node-app`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njl0drk7s4av8a52u5vg.png)

- Then you can install packages using 
`npm install`

- It will install all necessary packages.

- Install tmux using
`sudo apt install tmux`
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hyf90sm8rkzaoma7m2xz.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukkkrxl6tjvzvm6r6mv0.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/685e9wo76z55j9xb4pob.png)

- 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'.