본문 바로가기
2021 웹개발 101

[AWS] elastic beanstalk 에서 react 배포 502 오류 (code ELIFECYCLE errno 126)

by Senna 2021. 12. 31.

 

AWS elastiic beanstalk에서 react 어플리케이션을 배포했다.

그런데 책과 달리 내 실습환경에서는 오류가 나기 시작했다.

 

로그 내용을 살펴보니 이랬다.

Dec 30 16:00:30 ip-172-31-46-206 web: > todo-front@0.1.0 start /var/app/current
Dec 30 16:00:30 ip-172-31-46-206 web: > react-scripts start
Dec 30 16:00:30 ip-172-31-46-206 web: sh: /var/app/current/node_modules/.bin/react-scripts: Permission denied
Dec 30 16:00:30 ip-172-31-46-206 web: npm ERR! code ELIFECYCLE
Dec 30 16:00:30 ip-172-31-46-206 web: npm ERR! errno 126
Dec 30 16:00:30 ip-172-31-46-206 web: npm ERR! todo-front@0.1.0 start: `react-scripts start`
Dec 30 16:00:30 ip-172-31-46-206 web: npm ERR! Exit status 126
Dec 30 16:00:30 ip-172-31-46-206 web: npm ERR!
Dec 30 16:00:30 ip-172-31-46-206 web: npm ERR! Failed at the todo-front@0.1.0 start script.
Dec 30 16:00:30 ip-172-31-46-206 web: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Dec 30 16:00:30 ip-172-31-46-206 web: npm ERR! A complete log of this run can be found in:
Dec 30 16:00:30 ip-172-31-46-206 web: npm ERR!     /home/webapp/.npm/_logs/2021-12-30T16_00_30_717Z-debug.log

 

구글링했더니 .npmrc 라는 파일을 만들어서 unsafe-perm=true를 적어주라고 했는데 전혀 먹지 않았다.

 

로그를 다시 살펴보니 Permission denied가 보였다.

그리고 구글링했을 때 elastic beanstalk의 ec2에 접속하는 유저는 기본적으로 ec2-user라고 했다.

( https://stackoverflow.com/questions/46001516/beanstalk-node-js-deployment-node-gyp-fails-due-to-permission-denied/46001517#46001517 )

그러므로 이 ec2-user라는 계정이 current/node_modules라는 곳에 접근 권한이 없는 것이다.

 

 

다시 말하면 current/node_modules라는 폴더에 ec2-user가 접근할 수 있으면 되는 것이다..!

사실 이왕이면 기본 유저를 root으로 바꾸면 좋겠지만..

어떻게 하는지 모르겠어서,,, ㅠㅠ 내가 아는 최선의 방법을 수행하기로 했다.

 

1. elastic beanstalk에 SSH 연결을 설정해주었다. 나는 그냥 아예 새로 init부터 설정했다.

( https://docs.aws.amazon.com/ko_kr/elasticbeanstalk/latest/dg/eb3-ssh.html )

2. eb create 후에 eb ssh 입력해서 EC2의 터미널 접속

3. 기본 유저가 ec2-user로 접속 된다. 이 계정으로 cd /var/app/current 후 cd node_modules 하면 거절될 것이다.

4. sudo su로 root 유저로 전환한다.

5. setfacl -R -m u:ec2-user:rwx node_modules

   = node_modules 및 하위 폴더에 대한 ec2-user의 액세스 권한 부여

( https://askubuntu.com/questions/487527/give-specific-user-permission-to-write-to-a-folder-using-w-notation )

6. sudo service nginx restart (nginx 웹서버 재시작)

7. AWS콘솔 - elastic beanstalk - 구성에서 환경속성 PORT=3000 설정

 

이후에 접속해보면 잘 표시 되었다.

드디어..!!

(혹시 오류나면 
10-1. ps -ef | grep node 로 보고 실행되는 react start가 있으면 kill pid번호로 죽이고 npm run start&
10-2. 혹은 그냥 sudo service nginx restart... )

 


 

그런데 여전히 에러 메시지는 많이 뜬다..

무슨 에러인지 모르겠다...ㅠㅠㅠ

그래도 실행은 너무너무 잘된다..

 

이틀 내내 붙잡고 있었는데 드디어 502 Bad Gateway 화면을 탈출하게 되어서 너무 기쁘다..

휴 속이 다 시원하다 ㅠㅠ