Spring Boot 프로젝트를 PM2 로 관리하기 위해 EC2에 Node.js 와 PM2를 설치합니다.
환경
- 2021년 5월
- Node.js v16.2.0
- npm v7.14.0
- PM2 v4.5.6
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
$ . ~/.nvm/nvm.sh
$ nvm install node
$ nvm install 14.17.0
$ node -e "console.log('Running Node.js ' + process.version)"
$ npm install -g npm@7.14.0
$ npm install pm2 -g
sudo nano app.json
{
"apps": [{
"name": "xxx",
"cwd": ".",
"args": [
"-jar",
"[경로]/build/libs/xxx-0.0.1-SNAPSHOT.jar"
],
"env": {
},
"script": "/usr/bin/java",
"node_args": [],
"log_date_format": "YYYY-MM-DD HH:mm Z",
"exec_interpreter": "none",
"exec_mode": "fork"
}]
}
$ pm2 start app.json
$ pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /home/ec2-user/.pm2/dump.pm2
$ pm2 startup
sudo env PATH=$PATH:/home/ec2-user/.nvm/versions/node/v16.2.0/bin /home/ec2-user/.nvm/versions/node/v16.2.0/lib/node_modules/pm2/bin/pm2 startup systemd -u ec2-user --hp /home/ec2-user
$ sudo env PATH=$PATH:/home/ec2-user/.nvm/versions/node/v16.2.0/bin /home/ec2-user/.nvm/versions/node/v16.2.0/lib/node_modules/pm2/bin/pm2 startup systemd -u ec2-user --hp /home/ec2-user
...
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target
[Service]
Type=forking
User=ec2-user
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/home/ec2-user/.nvm/versions/node/v16.2.0/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin:/home/ec2-user/.nvm/versions/node/v16.2.0/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/home/ec2-user/.pm2
PIDFile=/home/ec2-user/.pm2/pm2.pid
Restart=on-failure
ExecStart=/home/ec2-user/.nvm/versions/node/v16.2.0/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/home/ec2-user/.nvm/versions/node/v16.2.0/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/home/ec2-user/.nvm/versions/node/v16.2.0/lib/node_modules/pm2/bin/pm2 kill
[Install]
WantedBy=multi-user.target
Target path
/etc/systemd/system/pm2-ec2-user.service
Command list
[ 'systemctl enable pm2-ec2-user' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-ec2-user.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-ec2-user...
Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-ec2-user.service to /etc/systemd/system/pm2-ec2-user.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save
[PM2] Remove init script via:
$ pm2 unstartup systemd
$ pm2 list
$ pm2 log 0
재시작이 느려서 모르고 인스턴스 종료를 눌러버렸어요. :C
언제 다시 세팅하지?... 블로그 보고 금방 했어요 ;D