0%

certbot 인증서 갱신 자동화 하기

목표

  1. cert bot을 이용하여 인증서를 발급받는다.
  2. docker를 사용해 인증서 갱신을 자동화한다.
  3. crontab을 이용해 주기적으로 nginx를 재시작한다.

자동화 이유

인증서는 3개월마다 갱신해야 합니다.

준비물

인증 받을 도메인과 자동화를 위한 docker를 준비합니다.

인증서 발급 && 자동 갱신

인증서 발급

도메인을 통해서 인증서 발급과 갱신 challenge를 진행하기 위해 nginx 설정을 먼저 합니다.

  • 도메인 설정
    도메인

  • app.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    server {
    listen 80;
    client_max_body_size 1024M;

    #cert bot
    location /.well-known/acme-challenge {
    allow all;
    root /var/www/certbot;
    }

    location / {
    return 301 https://$host$request_uri;
    }
    }

    자동화로 인증서를 갱신하기 위해서 web root방식을 사용합니다.

  • docker-compose

    1
    2
    3
    4
    5
    6
    7
    8
    nginx:
    build: ./docker/nginx
    volumes:
    - /etc/letsencrypt:/etc/letsencrypt
    - /var/www/certbot:/var/www/certbot
    ports:
    - 443:443
    - 80:80
  • nginx 실행

    1
    docker-compose up -d nginx
  • docker-compose

    1
    2
    3
    4
    5
    6
    7
    8
    certbot:
    image: certbot/certbot
    command: certonly --webroot --webroot-path=/var/www/certbot --email mail@gmail.com --agree-tos --no-eff-email -d a.domain.com
    volumes:
    - /etc/letsencrypt:/etc/letsencrypt
    - /var/www/certbot:/var/www/certbot
    depends_on:
    - nginx

    갱신 자동화에서도 docker를 사용하기 때문에 발급도 docker를 통해서 진행합니다.
    emaildomain을 본인의 정보로 변경합니다.

  • docker 실행

    1
    docker-compose up -d certbot
  • 발급 성공
    인증서 발급

인증서 갱신 자동화

기존의 cert bot 설정을 변경합니다. 2일마다 인증서를 갱신 시도하도록 설정했습니다.

  • docker-compose
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    certbot:
    image: certbot/certbot
    restart: unless-stopped
    # command: certonly --webroot --webroot-path=/var/www/certbot --email mail@gmail.com --agree-tos --no-eff-email -d a.domain.com
    volumes:
    - /etc/letsencrypt:/etc/letsencrypt
    - /var/www/certbot:/var/www/certbot
    depends_on:
    - nginx
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew --webroot -w /var/www/certbot; sleep 48h & wait $${!}; done;'"
  • docker 실행
    1
    docker-compose up -d certbot

nginx reload 자동화

위의 설정은 인증서 갱신을 자동화했지만, 새로운 인증서를 nginx에 적용해야 합니다. 이를 자동화하기 위해 crontab을 사용합니다.

  • cron.sh

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #!/bin/sh
    # Nginx 리로드를 위한 crontab 엔트리 추가
    echo "0 0 1 * * /usr/sbin/nginx -s reload" > /home/cron_test

    # Crontab 적용
    crontab /home/cron_test

    # crond 서비스 백그라운드 시작
    crond -l 2

    # nginx 포그라운드 실행
    nginx -g 'daemon off;'

    현재는 매월 1일에 reload하게 설정했습니다. 실행 주기는 cron 주기 설정을 참고하세요.

  • nginx Dockerfile

    1
    2
    3
    4
    5
    6
    7
    8
    FROM nginx:1.25.2-alpine

    # RUN apk add --no-cache crond
    COPY cron.sh /home/cron.sh
    RUN chmod +x /home/cron.sh

    # 컨테이너 시작 시 start.sh 스크립트 실행
    CMD ["/home/cron.sh"]
  • nginx 재실행

    1
    docker-compose up -d nginx

적용 확인

nginx 도커 컨테이너 접속

  • nginx 컨테이너 ID 확인
    1
    docker ps
    docker ps
  • 컨테이너 접속
    1
    docker exec -it <container_id> sh

    인증서 확인

  • 인증서 확인
    1
    ls /etc/letsencrypt/live
    인증서

crontab 확인

  • crontab 확인

    1
    crontab -l

    crontab

    적용이 잘 됐습니다.