Deploy NodeJS & Next JS with pm2 in EC2

Deploy NodeJS & Next JS with pm2 in EC2

Process Management

PM2 is a daemon process manager that will help you manage and keep your application online. Getting started with PM2 is straightforward, it is offered as a simple and intuitive CLI, installable via NPM.

Steps

step1 : Launch the ec2 instance

step 2: Security Group , here port 4000 for node app access

step 3 : Then, connect the ec2 instance and run the following code as follows

sudo apt update
sudo su -

step 4: Install latest version of the node js and npm

curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
sudo apt install -y nodejs
node -v 
npm -v
npm install -g npm@10.4.0

step 5: Install the pm2 server

npm install pm2@latest -g

step 6: Then

cd /
mkdir nodeApp
npm init
npm install express
touch index.js

step 7 : Now inside index.js file

// Import required modules
const express = require('express');

// Create an Express application
const app = express();

// Define a route handler for the root path
app.get('/', (req, res) => {
    res.send('Hello World!');
});

// Start the server on port 4000
app.listen(4000, () => {
    console.log('Server is running on port 4000');
});

step 8: Now run the following

cd nodeApp
pm2 start

Then we get

Then finally when we open the public ip with the port 4000

Note:- pm2 Reference https://pm2.keymetrics.io/docs/usage/quick-start/

Thanks for reading.........................................