aws pm2 서버 자동 실행 설정

aws ec2 안에 next.js 를 이용해서 서비스를 구성하였고 프로젝트 폴더에 package.json 파일이 있다.

package.json 의 name 키에 값을 아래 프로젝트 이름이란 곳에 넣어 서버를 가동시킨다.

pm2 start npm --name "프로젝트 이름" -- start
//pm2 start npm --name "naminsik" -- start

pm2가 정상적으로 가동되었는 지 가동된 리스트를 확인해본다.

pm2 list

정상적으로 가동되었다면 아래 순서로 입력하여 자동 실행 등록한다.

pm2 startup
pm2 save

하지만 위 커맨드 중 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/v18.18.0/bin /home/ubuntu/.nvm/versions/node/v18.18.0/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu

내용 안에도 써 있듯이 복사 붙여넣기 하면 된다.

아래는 startup 부터 save까지 캡쳐한 코드이다.

ubuntu@ip-172-31-12-000:~/nextjs$ 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/v18.18.0/bin /home/ubuntu/.nvm/versions/node/v18.18.0/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu
ubuntu@ip-172-31-12-000:~/nextjs$ sudo env PATH=$PATH:/home/ubuntu/.nvm/versions/node/v18.18.0/bin /home/ubuntu/.nvm/versions/node/v18.18.0/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu

                        -------------

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


                          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] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=ubuntu
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/home/ubuntu/.nvm/versions/node/v18.18.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ubuntu/.nvm/versions/node/v18.18.0/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/home/ubuntu/.pm2
PIDFile=/home/ubuntu/.pm2/pm2.pid
Restart=on-failure

ExecStart=/home/ubuntu/.nvm/versions/node/v18.18.0/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/home/ubuntu/.nvm/versions/node/v18.18.0/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/home/ubuntu/.nvm/versions/node/v18.18.0/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-ubuntu.service
Command list
[ 'systemctl enable pm2-ubuntu' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-ubuntu.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-ubuntu...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-ubuntu.service → /etc/systemd/system/pm2-ubuntu.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd
ubuntu@ip-172-31-12-000:~/nextjs$ pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /home/ubuntu/.pm2/dump.pm2


자동 실행이 안되거나 잘못된 이름으로 서비스를 구동시켰을 때는 pm2 kill 로 지운다. pm2 stop은 서비스를 정지만 시킨다.

Subscribe
Notify of
guest

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.

0 댓글
Inline Feedbacks
View all comments
TOP