Skip to content

Instantly share code, notes, and snippets.

@caeb92
Created February 22, 2020 21:24
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save caeb92/22a12a2cd27dd75b6c2267a90b2c244d to your computer and use it in GitHub Desktop.
Save caeb92/22a12a2cd27dd75b6c2267a90b2c244d to your computer and use it in GitHub Desktop.
PM2 fork and cluster mode configuration examples

Configure a new PM2 ecosytem

You must especificate the file JSON wich have the proper configuration for your app wich environment do you want to use (declared in your ecosystem file).

pm2 start ecosystem_cluster_mode.json --env production
pm2 save (Autostart your application when PM2 service is started/restarted)
pm2 ls (Shows PM2 runnings apps)
netstat -ntpl | grep pm2 (Filter running PM2 applications)

Flag --env

Indicates wich configuration use for your new ecosystem, more information at https://pm2.keymetrics.io/docs/usage/environment/

  • env: Environment variables for developement
  • env_production: Environment variables for production

Error saving PM2 configuration

Sometimes PM2 dont save correctly is configuration, so if the server was restarted your applications aren't going to autostart, to resolve this problem follow this steps (be careful with your others saved ecosystems, rm -rf ~/.pm2 has going to delete all your saved configurations from PM2).

# Stop and delete your app
pm2 stop app_name
pm2 delete app_name

# Kill PM2 process
ps -fea | grep pm2
kill -9 [PM2_PID]
rm -rf ~/.pm2

# Reconfigure the ecosystem for your app
pm2 start ecosystem_cluster_mode.json --env production
pm2 save

Ecosystem explains

Fork mode

Create a separated instance for your app with N replicas, the parameter increment_var is used for asign a unique port number to every replica avoiding port crashs.

Cluster mode

The cluster mode allows networked Node.js applications (http(s)/tcp/udp server) to be scaled accross all CPUs available, without any code modifications, the application port number is unique for the entire cluster.

{
"apps": [
{
"name": "app_name",
"script": "/software/app_name/dist/main.js",
"instances": 2,
"exec_mode": "cluster",
"autorestart": true,
"watch": false,
"max_memory_restart": "1G",
"error_file": "/software/app_name/logs/errors.txt",
"out_file": "/software/app_name/logs/logs.txt",
"env_production": {
"NODE_ENV": "production",
"PORT": 3000,
"JWT_SEED": "secretKey"
}
}
]
}
{
"apps": [
{
"name": "app_name",
"script": "/software/app_name/dist/main.js",
"instances": 4,
"exec_mode": "fork",
"increment_var": "PORT",
"autorestart": true,
"watch": false,
"max_memory_restart": "1G",
"error_file": "/software/app_name/logs/errors.txt",
"out_file": "/software/app_name/logs/logs.txt",
"env_production": {
"NODE_ENV": "production",
"PORT": 3000,
"JWT_SEED": "secret"
}
}
]
}
@malikkurosaki
Copy link

nice

@RamDivrania
Copy link

I'm having this problem with a similar setup.

Unitech/pm2#5580

Could you maybe guide @caeb92?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment