SankalpHaritash21/NodeJS_AWS

Deploying a scalable Node.js app on AWS EC2, configuring security, and using PM2.

★ 1Forks 0JavaScriptGitHub ↗Compare
awsaws-ec2pm2security

README

Deploying a Node.js Application on AWS EC2 Instance

NodeJS to AWS

Deploying Node.js Application on AWS EC2 Instance

This guide will walk you through the steps to deploy a Node.js application on an AWS EC2 Linux instance.

Prerequisites

  • An AWS account
  • Basic knowledge of Node.js
  • A Node.js application to deploy

Steps to Deploy the Application

1. Login to AWS Console

  • Log in to your AWS account.
  • Navigate to the EC2 service by typing "EC2" in the services search bar.

2. Launch an EC2 Instance

  • Select Launch Instance.
  • Name your instance, for example: nodejs_AWS.
  • Choose the appropriate AMI (Amazon Machine Image). In this case, select Ubuntu or any Linux-based AMI.
  • Configure your instance, select t2.micro or as per your needs.
  • In the Key pair section, create or choose a key pair to access the instance later.

3. Configure Security Group

  • Allow HTTP access from the internet.
  • Ensure your security group allows port 3000 (or whatever port your Node.js application uses) for TCP traffic.

4. Connect to the EC2 Instance

Once your instance is running:

  • Open the terminal and SSH into the instance:

    ssh -i your-key-file.pem ubuntu@your-ec2-public-ip

Switch to the superuser:

sudo su
  1. Install Node.js and Git
  • Install Git:
sudo apt update
sudo apt install git
  • Install Node.js using Node Version Manager (nvm):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
source ~/.nvm/nvm.sh
nvm install 16

Verify the installation:

node -v
git --version
  1. Clone the Repository Clone your Node.js application from GitHub:
git clone https://github.com/your-username/your-nodejs-repo.git
cd your-nodejs-repo
  1. Install Dependencies Install the required dependencies:
npm install
  1. Start the Application Run the application:
node index.js

Your application should now be running. Verify by visiting http://your-ec2-public-ip:3000 in your browser.

  1. Configure Security Group for Port 3000 If you haven't already configured security groups:

Go to EC2 Dashboard.

  • Select your running instance
  • Navigate to Security Groups.
  • Edit Inbound Rules and allow Custom TCP on Port 3000 from Anywhere.
  1. Run the Application with PM2

To keep your application running even after disconnecting from the terminal, install and use PM2:

Install PM2 globally:

npm install pm2 -g

Start your app with PM2:

pm2 start index.js

Verify that the app is running with:

pm2 list

Now, even if you disconnect from the SSH session, your application will continue running.

Node Running Server AWS

Contributors

SankalpHaritash21

Issues