본문 바로가기
node.js

[Aws ec2] nginx + Socket.io 연동

by 오늘도 깨달았다 2022. 4. 19.
반응형

nginx는 설치되어있다고 가정

 

1. root 유저로 변경

sudo su

2. 패키지파일들 업데이트

apt-get update

3. ubuntu nodejs 설치

curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash - 
sudo apt-get install -y nodejs

4. npm 설치

Node.js의 패키지 생태계인 npm은 세계에서 가장 큰 오픈 소스 라이브러리 생태계이다.

 apt-get install npm

npm 설치 시 의존성이 하나도 없다고 떠서 에러를 해결했다. 

 

error 내용 

 

에러 해결 명령어들

sudo apt remove --purge nodejs npm
sudo apt clean
sudo apt autoclean
sudo apt install -f
sudo apt autoremove
sudo apt install curl
cd ~
curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash -
sudo apt-get install -y nodejs
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn
node -v && npm -v      

 

 

5. express 설치

npm install -g express
npm install -g express-generator@4
npm install -g nodemon

nodemon이란 js 파일 변경시 서버에서 계속 모니터링 하여 재업로드해준다.

환경설정 끝

 

 

node 폴더 생성 및 서버 세팅

mkdir node-project && cd node-project && sudo mkdir testNode && cd testNode

express -e // node.js 패키지 생성

npm install // npm 인스톨

vi app.js //서버 세팅

 

 

nodejs 설치 완료


nginx 설정 시작 

 

1. /etc/nginx/nginx.conf 파일의 http{} 안에 원하는 proxy pass 추가

//슬래시 밑에 원하는 proxy 경로 설정 나의 경우엔 chat
location /chat {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;

	  //원하는 포트 
      proxy_pass http://localhost:3000;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }

 

2. 위에서 만든 node 폴더로 이동

cd /node-project/testNode

3. index.js 파일 생성 & 기본적인 채팅 확인용 정보 넣기

vi index.js
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);

app.get('/chat', (req, res) => {
  res.send('<h1>Hello world</h1>');
});

server.listen(3000, () => {
  console.log('listening on *:3000');
});

저장 후 나오기

 

4. 나오는지 테스트

node index.js

주소창에 localhost:3000 입력

 

Hello World 나오면 성공 

 

잘 나온다면 설치완료!

 

 

 


참고

https://archijude.tistory.com/367?category=972023 출처: https://archijude.tistory.com/394 [글을 잠깐 혼자 써봤던 진성 프로그래머]

반응형

'node.js' 카테고리의 다른 글

nodejs - redis Sorted SET - ZADD  (0) 2022.08.25
nodejs- 현재시간 숫자만 구하는 법  (0) 2022.08.25
[node.js] redis 연결  (0) 2022.08.23
node.js OpenSSL 적용과정 - let's encrypt  (0) 2022.08.15
소켓 채팅 기술선정  (0) 2022.04.19

댓글