Server/환경 구축 - Ubuntu

[Ubuntu] PM2 설치 및 사용

sangjun-pro 2024. 2. 26. 18:02

PM2 ( Process Manager 2 ) 설치 이유

- 에러 발생 후에도 프로그램 재실행 ( 무중단 )

- 멀티 코어 CPU에서 Node.js 동작의 성능 향상

 

1. pm2 설치

- 호환성 에러 발생 - node 버전 변경 필요

ubuntu@ubuntu:~$ npm install pm2 -g // pm2 전역 설치

// 호환성 에러 발생 - node 버전 14.14 이상 필요

npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'proxy-agent@6.3.1',
npm WARN EBADENGINE   required: { node: '>= 14' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'agent-base@7.1.0',
npm WARN EBADENGINE   required: { node: '>= 14' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'http-proxy-agent@7.0.2',
npm WARN EBADENGINE   required: { node: '>= 14' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'https-proxy-agent@7.0.4',
npm WARN EBADENGINE   required: { node: '>= 14' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'pac-proxy-agent@7.0.1',
npm WARN EBADENGINE   required: { node: '>= 14' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'socks-proxy-agent@8.0.2',
npm WARN EBADENGINE   required: { node: '>= 14' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'get-uri@6.0.3',
npm WARN EBADENGINE   required: { node: '>= 14' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'pac-resolver@7.0.1',
npm WARN EBADENGINE   required: { node: '>= 14' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'data-uri-to-buffer@6.0.2',
npm WARN EBADENGINE   required: { node: '>= 14' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'fs-extra@11.2.0',
npm WARN EBADENGINE   required: { node: '>=14.14' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'degenerator@5.0.1',
npm WARN EBADENGINE   required: { node: '>= 14' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/local/lib/node_modules/pm2
npm ERR! dest /usr/local/lib/node_modules/.pm2-0kfCfexq
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/pm2' -> '/usr/local/lib/node_modules/.pm2-0kfCfexq'
npm ERR!  [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/pm2' -> '/usr/local/lib/node_modules/.pm2-0kfCfexq'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'rename',
npm ERR!   path: '/usr/local/lib/node_modules/pm2',
npm ERR!   dest: '/usr/local/lib/node_modules/.pm2-0kfCfexq'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ubuntu/.npm/_logs/2024-02-26T07_57_45_061Z-debug-0.log

 

2. node 버전 변경

( https://sangjunui.tistory.com/75 )

 

 

3. pm2 설치 재실행

uuid warn 발생하였으나 일단 skip..

ubuntu@ubuntu:~$ npm install pm2 -g
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.

changed 162 packages in 18s

13 packages are looking for funding
  run `npm fund` for details

 

 

4. pm2 프로세스 실행

ubuntu@ubuntu:~$ pm2 start index.js // 프로세스 실행


//이외
ubuntu@ubuntu:~$ pm2 stop index.js // 실행
ubuntu@ubuntu:~$ pm2 restart index.js // 재실행
ubuntu@ubuntu:~$ pm2 delete index.js // 제거
ubuntu@ubuntu:~$ pm2 list // 프로세스 목록
ubuntu@ubuntu:~$ pm2 log // 로그
ubuntu@ubuntu:~$ pm2 monit // 모니터링

 

 

5. pm2 자동 실행

ubuntu@ubuntu:~/work$ pm2 startup //
[PM2] Init System found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/home/ubuntu/.nvm/versions/node/v21.6.2/bin /home/ubuntu/.nvm/versions/node/v21.6.2/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu

// 4번 행 재실행
ubuntu@ubuntu:~/work$ sudo env PATH=$PATH:/home/ubuntu/.nvm/versions/node/v21.6.2/bin /home/ubuntu/.nvm/versions/node/v21.6.2/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu
                        -------------

__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
 _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
  _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
   _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
    _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
     _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
      _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
       _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
        _\///______________\///______________\///__\///////////////__

.....
$ pm2 unstartup systemd


ubuntu@ubuntu:~/work$ pm2 save // 저장

'Server > 환경 구축 - Ubuntu' 카테고리의 다른 글

[Ubuntu] Grafana 설치 및 사용  (0) 2025.04.09
[Ubuntu] Node.js DB 연동 ( Mysql )  (0) 2024.02.10
[Ubuntu] MySQL DB 설치  (0) 2024.02.07
[Ubuntu] Nodejs/npm 설치  (0) 2023.12.19
[Ubuntu] FTP 설정  (0) 2023.08.29