-
항해 99 5기 TIL_43항해 99 2022. 2. 22. 05:40
▶ Today I Learned
<CS>
다익스트라 알고리즘
:최단 경로를 찾는 알고리즘
https://mattlee.tistory.com/50
링크를 참조하면 이해가 빠르다 :)
<클론코딩>
[서버 배포]
EC2 생성
github에 파일 올려놓기
sudo chmod 400 받은 키페어 끌어다 놓기 (키페어의 접근 권한을 바꿔준다.)
ssh -i 받은키페어를끌어다놓기 ubuntu@AWS에적힌내아이피 (SSH로 접속하기)
서버에 node.js 설치
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - sudo apt-get install -y nodejs
서버에 MongoDB 설치
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add - echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list sudo apt-get update sudo apt-get install -y mongodb-org
MongoDB 실행 (아무 반응이 없다면 잘 된것)
sudo service mongod start
서버에 접속하여 Git clone으로 해당 파일 가져오기 (환경변수는 올라가지 않도록 했으므로 Filezilla 같은 것을 통해 서버에 따로 .env파일을 넣어주어야 함)
보안 포트 열어주기
AWS에 해당 인스턴스 보안 그룹 -> 인바운드 규칙 편집에 들어가서 80, 27017 등 필요한 포트 모두 열기
(필요에 따라 사용 포트번호를 기본 번호인 80으로 바꿔주기)
ex) 80으로 접속해도 3000으로 연결되게 하기
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
iptables
AWS가 아닌 리눅스 운영체제 내부에서의 방화벽 역할을 하는 프로그램
서버의 MongoDB에 로컬 IP에서 접속하기
기본적으로 mongod.conf내에서 etc에 bindIp 가 로컬인 127.0.0.1로 되어있다.
이를 모든 아이피에서 접근 가능하도록 하려면 0.0.0.0로 바꾸어주어야 한다.
이는 아래의 코드로 설정 가능하다.
# MongoDB set user, set conf file // 주석이므로 신경쓰지 말자 mongo admin --eval 'db.createUser({user: "넣고 싶은 아이디", pwd: "넣고 싶은 비밀번호", roles:["root"]});' sudo sh -c 'echo "security:\n authorization: enabled" >> /etc/mongod.conf' sudo sed -i "s,\\(^[[:blank:]]*bindIp:\\) .*,\\1 0.0.0.0," /etc/mongod.conf // 이 부분이 바꾸는 부분 sudo service mongod stop sudo service mongod start // 적용을 위해 DB를 껐다가 키는 구간이다.
또는 sudo 권한으로 vi나 vim을 이용해 설정 가능하다고 한다.
// etc로 접속 // sudo vi mongod.conf 로 접근 // i키를 눌러 insert를 활성화시키고 변경하고 싶은 내용 변경 // 변경이 끝났다면 esc를 눌러 작업을 종료하고 :wq를 입력 후 엔터하여 하던 작업을 저장하고 종료하기
단, bindIp만을 바꾸는 작업 전에 아래의 코드는 필요하므로 우선은 저 코드를 복사하여 이용하는 것이 좋겠다.
mongo admin --eval 'db.createUser({user: "넣고 싶은 아이디", pwd: "넣고 싶은 비밀번호", roles:["root"]});' sudo sh -c 'echo "security:\n authorization: enabled" >> /etc/mongod.conf'
+ 프로그램 꺼지지 않게 하기
pm2
서버에 접속해 다음의 코드 실행
sudo -s // 관리자 권한으로 실행 npm install -g pm2 // -g를 넣어 특정 폴더나 프로젝트가 아닌 전역프로그램으로 설치 pm2 start app.js // pm2로 실행하여 이상이 없다면 문제 없음
▶ 느낀 점
서버와 DB 배포, Git에 관해서 어느 정도 알고 있다고 생각했는데 DB 배포가 되지 않아 수 시간을 고생했다.
하지만 아무리 생각하고 찾아도 감이 잡히지 않아 잘하시는 크루원 분에게 여쭈어보았고 그분의 경험으로 얻은 지식을
전수받을 수 있었다.
그 설명을 듣고나니 그간 왜 DB 연동문제가 있었는지 좀 더 알게 되었다.
이런게 바로 지식의 나눔이고 영향력이구나 생각했다.
질문도 좀 더 자주하고 또 누군가의 질문에 좀 더 답변할 수 있도록 열심히 공부해야지 하며 다짐했다.
도와주신 그 분께 정말 감사하다 :)
▶ 공부 시 참고 링크들
https://medium.com/sjk5766/jwt-json-web-token-%EC%86%8C%EA%B0%9C-49e211c65b45
https://www.lainyzine.com/ko/article/git-stash-usage-saving-changes-without-commit/
https://stackoverflow.com/questions/23517464/error-cannot-pull-with-rebase-you-have-unstaged-changes
파일 질라에서 .env가 넘어가지 않았을 때 해결법
https://threeidiotscoding.tistory.com/14
'항해 99' 카테고리의 다른 글
항해 99 5기 TIL_45 (0) 2022.02.24 항해 99 5기 TIL_44 (0) 2022.02.23 항해 99 5기 WIL_6 (0) 2022.02.20 항해 99 5기 TIL_42 (0) 2022.02.20 항해 99 5기 TIL_41 (0) 2022.02.20