[Caddy] 3000포트가 열리지 않는 문제 해결(+PM2로 무중단 서비스 적용)
서론
로컬에서 API를 추가 구현하고 클라우드 서버에 pull 받아 기능을 테스트하는 도중 여러 가지 문제가 발생했다.
Connection reset by port 22, port 22: Operation timed out, Connection closed by port 22, port 22: no matching host key type found 등 다양한 SSH 포트 접속 에러를 맞이하다가 새로운 인스턴스를 생성하여 볼륨 설정을 다시 해준 후 SSH로 접속할 수 있었다.
그런데 평소에 Node.js 애플리케이션을 자동으로 실행해왔던 Caddy가 말썽이다.
sudo systemctl restart caddy 명령어를 실행해도 3000포트가 열리지 않아 node app.js 명령어를 통해 수동으로 3000포트를 열어줘야 했다.
문제 해결 전 체크사항
1. Caddy 실행 확인
우선 Caddy가 실행되고 있는지 확인한다.
sudo systemctl status caddy
active (running) 상태라면 Caddy는 정상적으로 실행되고 있는 것이다.
2. 리슨(listen) 상태 확인
netstat -tuln | grep LISTEN
현재 시스템에서 활성화된 네트워크 연결을 확인하고 특정 포트들의 리슨 상태를 확인한다.
3000포트가 없다.
3. 3000포트 확인
3000포트가 열려 있는지 정확히 확인한다.
sudo lsof -i :3000
아무것도 출력되지 않는다면 3000포트가 열려있지 않은 상태인 것이다.
해결
PM2
PM2는 Node.js 프로세스를 관리하는 도구로 애플리케이션을 쉽게 시작, 중지, 재시작, 서버가 재부팅되었을 때 자동으로 다시 시작할 수 있다.
1️⃣ PM2 설치
npm install -g pm2 명령어 실행시 에러가 난다면 홈 디렉토리 아래에 설치한다.
권한 문제를 피하고 시스템 관리자 권한 없이 패키지를 사용할 수 있다.
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.profile
source ~/.profile
npm install -g pm2
2️⃣ 애플리케이션 실행
$ pm2 start app.js
-------------
__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
_\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
_\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
_\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
_\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
_\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
_\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
_\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
_\///______________\///______________\///__\///////////////__
Runtime Edition
PM2 is a Production Process Manager for Node.js applications
with a built-in Load Balancer.
Start and Daemonize any application:
$ pm2 start app.js
Load Balance 4 instances of api.js:
$ pm2 start api.js -i 4
Monitor in production:
$ pm2 monitor
Make pm2 auto-boot at server restart:
$ pm2 startup
To go further checkout:
http://pm2.io/
-------------
[PM2] Spawning PM2 daemon with pm2_home=/home/ubuntu/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /home/ubuntu/banana/app.js in fork_mode (1 instance)
[PM2] Done.
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name │ mode │ ↺ │ status │ cpu │ memory │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0 │ app │ fork │ 0 │ online │ 0% │ 43.4mb │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
$ pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /home/ubuntu/.pm2/dump.pm2
$ pm2 startup
[PM2] Init System found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /home/ubuntu/.npm-global/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu
3️⃣ Caddy 실행
sudo systemctl restart caddy
4️⃣ 포트 확인
netstat -tuln | grep LISTEN
아까는 없던 3000포트 리슨이 확인되었다.
sudo lsof -i :3000
이제 PM2와 Caddy를 활용해 프로젝트를 자동 실행할 수 있게 되었다 : )