PM2 best tools for running multiple instance nodejs applications
Sometimes we need to run more than one instance in one server and need to manage automaticly these instances for example I need to run socket cluster in 8 instance and when one of these instance down automaticly restart.
For solve this problem you can use PM2.
PM2 is a production process manager for Node. js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
For more detail you can use pm2 documentation:
PM2 is a daemon process manager that will help you manage and keep your application online 24/7.
Install PM2
You can use code below for install node js package but before run this command you must install nodejs
npm install pm2 -g
FEATURES
A Complete feature set for production environment, built with a worldwide community of developers and enterprises.
BEHAVIOR CONFIGURATION
SOURCE MAP SUPPORT
CONTAINER INTEGRATION
WATCH & RELOAD
LOG MANAGEMENT
MONITORING
MODULE SYSTEM
MAX MEMORY RELOAD
CLUSTER MODE
HOT RELOAD
DEVELOPMENT WORKFLOW
STARTUP SCRIPTS
DEPLOYMENT WORKFLOW
PAAS COMPATIBLE
KEYMETRICS MONITORING
API
Run node js with PM2
After install package you can run your node js application with code below.
pm2 start app.js
This package have a lot of senarios for run node js application we can write some of them
If you like to know more senarios you can follow documents.
For Node.js applications, PM2 includes an automatic load balancer that will share all HTTP[s]/Websocket/TCP/UDP connections between each spawned processes.
To start an application in Cluster mode:
$ pm2 start app.js -i max
Ecosystem File
You can also create a configuration file, called Ecosystem File, to manage multiple applications. To generate an Ecosystem file:
$ pm2 ecosystem
This will generate an ecosystem.config.js file:
module.exports = {
apps : [{
name: "app",
script: "./app.js",
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
}
}, {
name: 'worker',
script: 'worker.js'
}]
}