본문 바로가기
IT/Deploy

Docker-Compose를 이용하여 Nginx와 Certbot으로 HTTPS 활성화하기

by Cyber_ 2024. 4. 16.

          서론

          Docker와 Docker-Compose를 이용하여 Nginx와 Certbot을 구성하는 방법은 웹 애플리케이션의 배포 및 관리를 간소화합니다. 이 방법을 사용하면, SSL/TLS 인증서의 설치와 갱신을 자동화하여, 보다 안전하게 웹 애플리케이션을 운영할 수 있습니다. 본 글에서는 Docker-Compose를 사용하여 Nginx 서버에 Certbot을 설치하고 HTTPS를 활성화하는 과정을 단계별로 설명하겠습니다.

          필요 조건

          • Docker 및 Docker-Compose가 설치된 시스템
          • 유효한 도메인 이름과 해당 도메인의 DNS 설정이 완료된 상태

          Docker-Compose 파일 설정

          먼저, docker-compose.yml 파일을 생성하고 Nginx와 Certbot 서비스를 정의합니다. 아래는 간단한 예제입니다:

          version: '3'
          
          services:
            nginx:
              image: nginx:latest
              container_name: nginx
              ports:
                - "80:80"
                - "443:443"
              volumes:
                - ./data/nginx:/etc/nginx/conf.d
                - ./data/certbot/conf:/etc/letsencrypt
                - ./data/certbot/www:/var/www/certbot
              depends_on:
                - certbot
          
            certbot:
              image: certbot/certbot
              container_name: certbot
              volumes:
                - ./data/certbot/conf:/etc/letsencrypt
                - ./data/certbot/www:/var/www/certbot
              entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"

           

          Nginx 설정 파일(default.conf)을 ./data/nginx 디렉토리에 생성합니다. 이 파일은 HTTP 요청을 HTTPS로 리다이렉트하고, HTTPS 요청을 처리하는 설정을 포함합니다.

          server {
              listen 80;
              server_name yourdomain.com www.yourdomain.com;
              location / {
                  return 301 https://$host$request_uri;
              }
          
              location /.well-known/acme-challenge/ {
                  root /var/www/certbot;
              }
          }
          
          server {
              listen 443 ssl;
              server_name yourdomain.com www.yourdomain.com;
          
              ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
              ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
              include /etc/letsencrypt/options-ssl-nginx.conf; # Managed by Certbot
              ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # Managed by Certbot
          
              location / {
                  root /usr/share/nginx/html;
                  index index.html;
              }
          }

          인증서 발급

          Certbot 컨테이너를 처음 실행할 때, 다음 명령을 사용하여 인증서를 수동으로 발급받습니다:

          docker-compose run --rm certbot certbot certonly --webroot --webroot-path=/var/www/certbot --email your-email@example.com --agree-tos --no-eff-email --staging --expand -d yourdomain.com -d www.yourdomain.com

          SSL 인증서 갱신 자동화

          Docker-Compose 파일에서 Certbot 서비스의 entrypoint는 이미 SSL 인증서의 자동 갱신을 처리하도록 설정되어 있습니다. 이 설정은 12시간마다 인증서 갱신을 시도합니다.

          결론

          Docker-Compose를 사용하여 Nginx와 Certbot을 설정하는 방법은 웹 애플리케이션의 배포를 간편하게 하고, SSL/TLS 인증서 관리를 자동화합니다. 이 구성을 통해 보안이 강화된 웹 애플리케이션을 쉽게 운영할 수 있습니다.