Certbot를 활용한 Https

Hyor·2024년 9월 9일

개요

IDC 환경에 https로 접속하기 위한 인증서 발급 업무를 맡게되어 certbot를 활용하여 https 인증서를 받는 내용을 정리했습니다.

사용방법

  • nginx 를 실행하여 Certbot이 /.well-known/acme-challenge/ 경로로 요청할수 있게 설정
...
http {
    ...
    server {
        ...
        server_name [도메인]

        # Let's Encrypt 인증서 자동 갱신을 위한 설정
        location /.well-known/acme-challenge/ {
            root /var/www/certbot; # Certbot이 임시 파일을 저장할 위치
            allow all;
        }

        # 모든 HTTP 트래픽을 HTTPS로 리디렉션
        location / {
            return 301 https://$host$request_uri;
        }
    }
    ...
}
  • certbot를 실행하여 인증서 다운
version: '3.3'
services:
    certbot:
        image: certbot/certbot:latest
        container_name: cmd_certbot
        command: certonly --webroot --webroot-path=/var/www/certbot --email [이메일] --agree-tos --no-eff-email -d [도메인] [--dry-run]
        volumes:
            - /[path]/certbot/conf:/etc/letsencrypt:rw
            - /[path]/certbot/logs:/var/log/letsencrypt:rw
            - /[path]/certbot/data:/var/www/certbot:rw

docker-compose -f certbot.yml run --rm certbot

  • 다운받은 인증서를 nginx container 에 mount
version: '3.3'
services:
    nginx:
        image: nginx:alpine
        container_name: nginx
        ports:
            - '80:80'
            - '443:443'
        volumes:
            - /[path]/nginx.conf:/etc/nginx/nginx.conf:ro
            - /[path]/certbot/data:/var/www/certbot
            - /[path]/certbot/conf:/etc/nginx/ssl
        restart: always
  • [옵션] certbot은 6개월마다 인증서를 갱신하여하기 때문에 script를 추가하여 자동 갱신해줍니다.
profile
개발 노트

0개의 댓글