본문 바로가기

개발바닥/BackEnd

PM2를 사용하여 Multi-Node 실행

  • pm2를 이용하여 nodejs를 관리가 되어야 하므로 global 영역에 설치 되어야 함
  • npm install -g pm2

아래 파일을 등록하여 node를 여러개 가동 할 수 있도록 한다.

var ignoreWatch = [
  'node_modules',
  'logs',
  'data',
  'dist',
  'conf'
]
// Options reference: https://pm2.keymetrics.io/docs/usage/application-declaration/
module.exports = {
  apps : [
  {
    name: 'server-1',
    script: 'index.js',
    autorestart: true,
    watch: true,
    ignore_watch: ignoreWatch,
    max_memory_restart: '1500M',
    env: {
      'port': '3000'
    }
  },
  {
    name: 'server-2',
    script: 'index.js',
    autorestart: true,
    watch: true,
    ignore_watch: ignoreWatch,
    max_memory_restart: '1500M',
    env: {
      'port': '3001'
    }
  },
  {
    name: 'server-3',
    script: 'index.js',
    autorestart: true,
    watch: true,
    ignore_watch: ignoreWatch,
    max_memory_restart: '1500M',
    env: {
      'port': '3002'
    }
  },
  {
    name: 'server-4',
    script: 'index.js',
    autorestart: true,
    watch: true,
    ignore_watch: ignoreWatch,
    max_memory_restart: '1500M',
    env: {
      'port': '3003'
    }
  },
  ]
};​
  • 서비스 실행 ecosystem형식의 파일을 등록하여 사용할 것
$ pm2 [start|restart|stop|delete] ecosystem.config.js​
  • 서비스 등록 및 관리
$ pm2 startup // 시스템 boot 활성화
$ pm2 save
  • 새로운 NodeJS 업그레이드시에는 서비스 재등록
$ pm2 unstartup // 시스템 boot 비활성화
$ pm2 startup​