Post

GCP VM에 NodeJS https 서버 구성하기

GCP VM에 NodeJS https 서버 구성하기

1. AWS에서 도메인 등록하기

  1. AWS Route 53 콘솔로 이동
  2. 도메인 등록
    1. ‘도메인 등록’ 메뉴로 이동하여 원하는 도메인을 검색하고 등록
    2. 도메인을 등록하면 Route 53에서 자동으로 호스팅 영역이 생성됨

2. GCP에서 VM 인스턴스 생성 및 설정

  1. GCP 콘솔로 이동
  2. 왼쪽 메뉴에서 Compute Engine → VM 인스턴스 만들기 클릭
  3. 설정
    1. 이름 : (원하는 이름)
    2. 리전 : (서버 위치)
    3. 영역 : 모두
    4. 부팅 디스크 : (Ubuntu, Debian)
    5. 방화벽 : HTTP 및 HTTPS 트래픽 허용 체크(없으면 인스턴스를 생성하고 수정을 할때 방화벽 설정 가능)
  4. 고정 IP 설정

3. AWS에 등록한 도메인과 GCP IP 연결

  1. 레코드 추가
    1. 레코드 이름 : (비워놓기)
    2. 레코드 유형 : A
    3. 값 : GCP 고정 IP 주소
    4. 라우팅 정책 : 단순 라우팅
  2. DNS 전파 확인
    1. AWS에 아까 등록한 도메인으로 ping 날리기

      1
      
       ping your-domain.com
      
    2. ping 결과(이 문자가 반복해서 나타남)

      1
      
       64 bytes from 34.22.72.6: icmp_seq=2 ttl=54 time=63.042 ms
      

4. GCP VM에서 Node.js 서버 구성

  1. GCP Compute Engine → VM 인스턴스에서 해당 인스턴스 SSH접속
    1. SSH 클릭

      Image

  2. Node.js 및 npm 설치

    1
    2
    
     sudo apt update
     sudo apt install -y nodejs npm
    
  3. 프로젝트 디렉토리 생성(프로젝트 이름은 마음대로)

    1
    2
    
     mkdir ~/my-node-project
     cd my-node-project
    

5. Let’s Encrypt로 HTTPS 설정(SSL 인증서)

  1. Certbot 설치

    1
    2
    
     sudo apt update
     sudo apt install -y certbot
    
  2. SSL 인증서 발급
    1. 성공 시 /etc/letsencrypt/live/your-domain.com/에 인증서가 저장됨
    1
    
     sudo certbot certonly --standalone -d your-domain.com
    
  3. js 파일 만들기
    1. “nano”는 파일을 만들거나 수정하는 명령어다.
    1
    
     nano index.js
    
  4. 기본 HTTPS 서버 코드 작성

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
     const fs = require('fs');
     const https = require('https');
     const express = require('express');
        
     const app = express();
     const PORT = 80;
     const HTTPS_PORT = 443;
        
     const options = {
         key: fs.readFileSync('/etc/letsencrypt/live/your-domain.com/privkey.pem'),
         cert: fs.readFileSync('/etc/letsencrypt/live/your-domain.com/fullchain.pem'),
     };
        
     app.get('/', (req, res) => {
         res.send('Hello, HTTPS World!');
     });
        
     const http = require('http');
     http.createServer((req, res) => {
         res.writeHead(301, { Location: `https://${req.headers.host}${req.url}` });
         res.end();
     }).listen(PORT, () => {
         console.log(`HTTP server running on port ${PORT}, redirecting to HTTPS.`);
     });
        
     https.createServer(options, app).listen(HTTPS_PORT, () => {
         console.log(`HTTPS server running on port ${HTTPS_PORT}`);
     });
        
    
  5. 서버 실행

    1
    
     sudo node index.js
    
  6. 브라우저에서 테스트
    1. https:///your-domain.com 으로 접속하여 동작 확인

6. 인증서 자동 갱신 설정

  • Crontab 열기:

    1
    
      sudo crontab -e
    
  • 자동 갱신 명령 추가:

    1
    
      0 0 * * * certbot renew --quiet && systemctl restart nodejs
    
    • 매일 자정에 인증서를 갱신하고 Node.js 서버를 재시작합니다.
This post is licensed under CC BY 4.0 by the author.