pm2 logs를 보면 다음과 같은 문제로 계속해서 재실행되는 것을 볼수가 있다.
Script /home/ec2-user/app.js had too many unstable restarts (16). Stopped. "errored"
해당 문제에 대한 원인을 찾기 위해 bin/www를 실행시키면 다음과 같은 로그가 찍혀있다.
node:internal/fs/utils:350
throw err;
^
Error: EACCES: permission denied, open '/etc/letsencrypt/live/test.com/privkey.pem'
at Object.openSync (node:fs:603:3)
at Object.readFileSync (node:fs:471:35)
at Object.<anonymous> (/home/ec2-user/mini/app.js:14:13)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Module.require (node:internal/modules/cjs/loader:1143:19)
at require (node:internal/modules/cjs/helpers:119:18)
at Object.<anonymous> (/home/ec2-user/mini/bin/www:7:11) {
errno: -13,
syscall: 'open',
code: 'EACCES',
path: '/etc/letsencrypt/live/test.com/privkey.pem'
}
pem키에 대한 권한 문제로 일반 사용자가 pm2를 수행시키면 실행이 되지 않는다. 그래서 떠오른 방식은 setUid를 통해 pm2에 root 권한을 주는 것. 하지만 해당 방식은 보안에 좋지 않을 것이라 생각했다. 그러던 중 스택오버플로우에서 해당 방식에 대한 문제를 찾을 수 있었다.
1.sudo groupadd nodecert
2. sudo useradd nodeuser -G nodecert
3. sudo chgrp -R nodecert /etc/letsencrypt/live
4. sudo chgrp -R nodecert /etc/letsencrypt/archive
5. sudo chmod -R 750 /etc/letsencrypt/live
6. sudo chmod -R 750 /etc/letsencrypt/archive
7. ec2 재부팅 또는 exit
8. sudo passwd nodeuser
9. su nodeuser
10. pm2 start ecosystemconfig.js
Ref
'문제해결' 카테고리의 다른 글
MongoDB issue 정리 (3) | 2024.09.09 |
---|---|
Git rebase가 잘못 수행된 경우 (0) | 2024.09.09 |
pm2 had too many unstable restarts(16). stopped. “errored” (0) | 2024.06.08 |
[AWS] EC2 Nginx host not found in upstream ELB (0) | 2023.08.18 |
[SpringBoot] JSON과 image 동시에 값 보내기 (0) | 2023.08.18 |