Install PM2 globally
npm install -g pm2@6.0.14
pm2 -v # 6.0.14
Enable PM2 on boot
pm2 startup
# run the command it outputs (usually sudo env PATH=... pm2 startup systemd -u $USER --hp $HOME)
Example ecosystem.config.js
Make sure to have this file in your project root before starting PM2.
module.exports = {
apps: [
{
name: 'j-optic-backend',
script: 'dist/main.js',
cwd: '/var/www/j-optic-backend',
instances: 1,
autorestart: true,
env: {
NODE_ENV: 'production',
},
error_file: '/var/log/pm2/j-optic-backend-error.log',
out_file: '/var/log/pm2/j-optic-backend-out.log',
},
],
}
Give each app a unique error_file / out_file path. Duplicate log paths across apps will cause PM2 to write mixed/garbled logs.
Common commands
pm2 start ecosystem.config.js
pm2 list
pm2 logs j-optic-backend
pm2 restart j-optic-backend --update-env
pm2 save
Since PM2 was installed via nvm-managed npm, it lives under the same Node version’s global path. If you switch the default Node version with nvm alias default, reinstall PM2 globally under the new version or PM2 commands may stop resolving.