Skip to content

Instantly share code, notes, and snippets.

@l-wagner
Created January 22, 2021 13:04
Show Gist options
  • Save l-wagner/a34efb86cb93adcd30e35f22c6e3e372 to your computer and use it in GitHub Desktop.
Save l-wagner/a34efb86cb93adcd30e35f22c6e3e372 to your computer and use it in GitHub Desktop.
PM2 Ecosystem config example
// ============== PM2 ECOSYSTEM FILE =================
// /srv/www/pm2/ecosystem.config.js
// shared environment variables
const commonEnv = {
devEnv: {},
prodEnv: {
SHARED_SECRET: '123456',
ANOTHER_SHARED_SECRET: '123456',
NODE_ENV: 'PROD',
},
};
// individual variables live in app's .env files
// NODE npm start script loading individual .env files: "start": " nodemon --exec babel-node -r dotenv/config app.js",
module.exports = {
apps: [
{
name: 'APP-NAME', // will be used to refer to app in pm2 commands.
// it's convenient if it's the same as nginx location and folder name but not necessary
cwd: '/path/to/app', // I prefer absolute paths for clarity but this can be a relative path
script: 'npm',
args: 'start',
log_date_format: 'YYYY-MM-DD HH:mm Z',
env: {
PORT: 1234,
...commonEnv.prod,
},
},
{
name: 'ANOTHER-APP',
cwd: '/path/to/app',
script: 'npm',
args: 'start',
env: {
...commonEnv.prod,
},
},
],
};
// pm2 commands:
// $ pm2 ecosystem // generate sample file
// $ pm2 start/reload/restart/stop app-name
// $ pm2 logs app-name --lines 5000
// $ pm2 start ecosystem.config.js
// ============== ECOSYSTEM END =================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment