Certbot 으로 Let’s encrypted 무료 https SSL 적용 - AWS Node.js

2024. 8. 3. 02:08·Cloud

준비물

  • domain (가비아에서 도메인 구매)
    • domain, www.domain (두 개 설정 필요)
  • Lightsail server 
    • AWS 가입후 lightsail 에서 instance 생성
    • 5$, $7, $12 는 3개월 무료

SFTP 접속

  • Lightsail 에서 console로 접속
  • 키 파일을 다운받아 putty 로 접속도 가능 (putty gen 으로 private 키를 생성하고 접속해야 함)

Nginx  설치

sudo apt update
sudo apt install nginx

conf.d 파일에 도메인 설정 추가 (site-available 에 추가함)

  • sites-available/sites-enabled 는 더이상 사용되지 않는 Nginx 설정 방법이라고 한다. 이 포스팅에서는 conf.d 에 Nginx 설정 파일을 만들고 관리한다.
  • 만약 sites-available과 sites-enabled에 기본 설정 파일이 있다면 제거하자. 또한 conf.d에 기본으로 default.conf 파일이 존재하면, 그 파일을 수정해서 설정하자.
  • Node.js 를 서버로 사용했고 3000 port 를 이용함
// 파일 생성
sudo nano /etc/nginx/sites-available/yourdomain.conf

// 아래 내용 추가
server {
    listen 80;
    server_name domain www.domain;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ~ /.well-known/acme-challenge {
        allow all;
    }

    # Redirect all HTTP requests to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name domain www.domain;

    ssl_certificate /etc/letsencrypt/live/domain/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain/privkey.pem;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

설정 파일 활성화

sudo ln -s /etc/nginx/sites-available/yourdomain.conf /etc/nginx/sites-enabled/

// Nginx 설정 테스트 및 재시작:
sudo nginx -t
sudo systemctl restart nginx

Certbot Nginx 플러그인 설치

sudo apt-get install python3-certbot-nginx

// 인증서 갱신
sudo certbot --nginx

// 만약 실행이 안된다면
// sudo reboot 로 서버 재 실행 그래도 안되면 apache2 중지 또는 삭제 필요

Node.js Server 에 ssl option 값 설정 (사용하는 서버에 ssl 값 추가)

// HTTPS 옵션 설정
const options = {
  key: fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/privkey.pem'),
  cert: fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/fullchain.pem')
};

SSL 자동 갱신 Crontab 설정

sudo crontab -e

// 맨 밑에 아래 줄 추가
0 0 * * * certbot renew --post-hook "sudo service nginx reload"

 

저작자표시 (새창열림)

'Cloud' 카테고리의 다른 글

Teams 휴가 시스템 구축 - Microsoft 365 (Automate 자동화, Power Apps 연동)  (1) 2025.07.10
[GCP] Bastion Host VM 서버 구축 (Proxy, OS Login)  (0) 2025.04.27
[GCP] Google Cloud Armor 적용 (feat. Custom Error page - Cloud Storage)  (0) 2025.03.23
'Cloud' 카테고리의 다른 글
  • Teams 휴가 시스템 구축 - Microsoft 365 (Automate 자동화, Power Apps 연동)
  • [GCP] Bastion Host VM 서버 구축 (Proxy, OS Login)
  • [GCP] Google Cloud Armor 적용 (feat. Custom Error page - Cloud Storage)
coder_
coder_
어차피 할거 즐기자
  • coder_
    Good coder
    coder_
  • 전체
    오늘
    어제
    • 분류 전체보기 (41)
      • 인공지능 [AI] (3)
      • 모의해킹 (3)
      • Git (2)
      • Cloud (4)
      • 컴퓨터 과학 [CS] (3)
      • 데이터베이스 [DB] (2)
      • Nojde.js (4)
      • Javascript (3)
      • Django (4)
      • Code up (11)
        • 코드업 기초 100제 (9)
        • 단순 반복문 (2)
      • 백준 baek_joon (1)
        • 단계별 문제풀이 (1)
      • Web Scraping (1)
  • 블로그 메뉴

    • 글쓰기
    • 관리
  • 링크

    • GitHub
  • 공지사항

  • 인기 글

  • 태그

    모의해킹
    기초100제
    기초 100제
    파이썬
    Coding Test
    ubuntu 24.04
    코드업
    ubuntu
    nodejs
    MariaDB
    Bastion host
    Linux
    code up
    llama3.1
    Python
    Lightsail
    GCP
    php
    node.js
    hugging face
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
coder_
Certbot 으로 Let’s encrypted 무료 https SSL 적용 - AWS Node.js
상단으로

티스토리툴바