How To Setup The PM2 Config With Node JS Application & PM2 Basic Commands



    PM2 Basic Concept to Advanced With Step By Step Code Sample.

    Here I have mentioned the list of major PM2 topics below.
  • What is PM2?
  • Why We Use PM2?
  • How to Setup PM2 in the NodeJs Application (https://egghead.io/lessons/node-js-configuring-pm2-for-node-applications)
  • UseCase Of PM2 
  • PM2 Commands
  • PM2 Code Sample
  • PM2 List
  • How to Pass the Environment Variable using PM2? (https://egghead.io/lessons/node-js-passing-environment-variables-to-node-js-using-pm2)

PM2 Official website: PM2 

Installing PM2:- 

 PM2 NPM Package : - PM2 NPM

 By using this command we can install the pm2 globally in our system

  •      npm install pm2 -g

   Next Step, In the Sample Terminal Just Type, pm2 init simple

   it will automatically generate a sample configuration file for us.

  • pm2 init simple

File /Users/admin1/Desktop/sampleapp/ecosystem.config.js generated

ecosystem.config.js

module.exports = {
apps : [{
name : "application1",
script : "./index.js"
}]
}


// # Start all applications
// pm2 start ecosystem.config.js

// # Stop all
// pm2 stop ecosystem.config.js

// # Restart all
// pm2 restart ecosystem.config.js

// # Reload all
// pm2 reload ecosystem.config.js

// # Delete all
// pm2 delete ecosystem.config.js


  PM2 Useful Commands

  •  pm2 start ecosystem.config.js
  •  pm2 stop ecosystem.config.js 
  •  pm2 restart ecosystem.config.js
  •  pm2 reload ecosystem.config.js
  •  pm2 delete ecosystem.config.js

How to Pass the Environment Variable using PM2?


module.exports = {
  apps : [{
    name   : "application1",
    script : "./index.js",
    env_production: {
       NODE_ENV: "PROD"
    },
    env_development: {
       NODE_ENV: "DEV"
    }
  }]
}

 For Reference Here I have mentioned the proper video explanation for  How To Pass the Environment Variable using PM2


What is PM2?

PM2 is nothing but a Production Process Manager for the Node JS applications. By Using PM2 We can monitor 

the Error logs, System logs,

and memory size, Process ID (Pid), 

CPU (Central processing unit)

Mem (Node Memory).

PM2 Has a Build In load balancer for Node Js Production Application. It Allows Us to keep the Node Js Application Process alive Forever.

Main Use Case of the PM2 Setup, We can reload the node server without downtime so it will not affect the user in the production server 

3. How to Setup PM2 in the NodeJs Server?


4.UseCase Of PM2 


5.PM2 Commands


6. PM2 Code Sample



Post a Comment

Previous Post Next Post