본문 바로가기

서버

pm2 윈도우 서비스 등록

시스템 재 부팅시 리눅스와 달리 윈도우즈에서는 PM2에 등록된 앱이 자동으로 원할하게 실행되지 않는다.

PM2 도움말에도 pm2-windows-servicepm2-windows-startup 을 사용하라고 하지만 잘 작동을 안한다.

아마도 예전 버전의 pm2 와 윈도우즈에서는 작동했던 것 같다.

이 문제를 해결한 좋은 자료를 링크하고 정리해 본다.

https://adamtuttle.codes/blog/2019/node-apps-as-windows-services/

https://blog.cloudboost.io/nodejs-pm2-startup-on-windows-db0906328d75

해결방법을 정리하면 윈도우즈 운영체제 서비스로 PM2 를 등록하여 실행하는 pm2-windows-service 패키지를 사용하는 것이다.

0. 가장 먼저 해야할 일은 윈도우즈 환경 변수> 시스템 변수 에서 PM2_HOME 를 생성하고 값을 c:\etc\.pm2 로 설정한다.

이유는 로그온 사용자 폴더에 설치된 pm2 를 로그온 사용자와 무관하게 접근할 수 있는 폴더로 이동하는 것이다. 그래야 윈도우즈 부팅시 로그온 전에 pm2가 실행될 수 있다.

1. npm 으로 패키지를 설치한다. 이미 설치되어 있어도 다시 설치해도 된다.

> npm i -g pm2 pm2-windows-service npm-check-updates

2. 그런 다음 윈도우즈 서비스에 등록하기 위해

pm2-service-install 명령을 실행해야 하는데 여기서 hang on 멈춰버리는 문제가 발생한다.

> pm2-service-install > Perform environment setup (recommended)? Yes

이를 해결하려면 다음 명령처럼 pm2-windows-serviec 폴더로 이동하여 ncu(npm-check-updates) 명령을 사용하여 inquirer 모듈로 최신버전으로 package.json 을 업데이트 하고 의존성 패키지를 npm i 로 다시 설치하는 것이다.

> cd %AppData%\npm\node_modules\pm2-windows-service > ncu inquirer -u > npm i

그런 다음 다시 다음의 명령으로 pm2를 윈도우즈 서비스에 등록한다.

> pm2-service-install

그리고 다음처럼 설정하면 된다.

> Perform environment setup (recommended)? Yes
> Set PM2_HOME? Yes
PM2_HOME value (this path should be accessible to the service user and should not contain any "user-context" variables [e.g. %APPDATA%]): c:\etc\.pm2\
> Set PM2_SERVICE_SCRIPTS (the list of start-up scripts for pm2)? Yes
> Set the list of start scripts/files (semi-colon separated json config files or js files) (I left this one blank)
> Set PM2_SERVICE_PM2_DIR (the location of the global pm2 to use with the service)? [recommended] Yes Specify the directory containing the pm2 version to be used by the service
 
C:\USERS\ADMINISTRATOR\APPDATA\ROAMING\NPM\node_modules\pm2\index.js PM2 service installed and started.

서비스에 등록되고 스타트되었다고 하면

사용할 모든 앱을 실행하고 저장한다.

> pm2 save

save 명령은 실행중인 모든 앱 목록을 %PM2_HOME%\dump.pm2 파일에 저장하고 resurrect 명령으로 간단하게 모든 앱을 재 실행할 때 사용된다. 결국 재부팅시 pm2 실행시 resurrect 명령이 실행된다고 보면 된다.

> pm2 resurrect

이제 운영체제를 재 부팅하고 설정된 앱들이 잘 실행 중인지 확인하자

> pm2 ls

서비스에 등록된 것도 확인하면 모든 것이 만족스럽다.

'서버' 카테고리의 다른 글

gitlab 명령어 및 에러  (0) 2022.08.11
nginx 멀티 ssl 포트  (0) 2022.07.28
nginx window 서비스 등록  (0) 2022.07.28
window cerbot 자동 갱신  (0) 2022.07.09
vue + nginx + certbot  (0) 2022.07.09