백엔드

[SSL] Route53 + Certbot 와일드카드 인증서 발급

parkjb 2025. 1. 23. 14:41

플러그인 설치 및 Credential 설정

Certbot Route53 플러그인 설치

sudo apt-get update
sudo apt-get install python3-certbot-dns-route53

AWS IAM 사용자 생성

  1. AWS 콘솔의 IAM 서비스에서 새로운 사용자 생성하기 (ex. certbot)
  2. 액세스 키 생성
  3. 권한 설정 (AmazonRoute53FullAccess 권한)

AWS 자격 증명 파일 생성

mkdir -p ~/.aws
vi ~/.aws/credentials

~/.aws/credentials 에 발급받은 ACCESS_KEY의 ID와 SECRET_KEY를 아래와 같이 입력한다.

[default]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

보안 권한 설정

chmod 600 ~/.aws/credentials

인증서 발급

기존 인증서 삭제

혹시 기존 인증서가 있다면 삭제하자. (안해도 되지만.. clean하게..)

sudo certbot delete --cert-name parkjb.com

새로운 인증서 발급

sudo certbot certonly \
  --dns-route53 \
  -d parkjb.com \
  -d '*.parkjb.com'

certonly: 인증서만 발급하고, 웹 서버 설정은 변경하지 않는 옵션.. certonly 없이 자동으로 한다면 설정 올바르게 되었는지 확인해주기

에러?

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for parkjb.com and *.parkjb.com
Unable to locate credentials
To use certbot-dns-route53, configure credentials as described at https://boto3.readthedocs.io/en/latest/guide/configuration.html#best-practices-for-configuring-credentials and add the necessary permissions for Route53 access.
Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile /var/log/letsencrypt/letsencrypt.log or re-run Certbot with -v for more details.

이런 에러가 뜬다면, Credential 정보를 못찾은 것이다.
~/.aws 폴더에 들어있는 Credential이 사용자의 홈 디렉토리에 있어서 발생하는 문제이다.
이럴때는 root에 ~/.aws 폴더를 옮겨주거나, 아래의 환경변수를 설정하고 다시 실행한다.

AWS_ACCESS_KEY_ID^API 참고
The access key for your AWS account.

AWS_SECRET_ACCESS_KEY^API 참고
The secret key for your AWS account.

결과

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for parkjb.com and *.parkjb.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for parkjb.com and *.parkjb.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/parkjb.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/parkjb.com/privkey.pem
This certificate expires on 2025-02-21.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

굳.

마무리 설정 및 테스트

웹 서버 설정

server {
    listen 443 ssl;
    server_name parkjb.com *.parkjb.com;

    ssl_certificate /etc/letsencrypt/live/parkjb.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/parkjb.com/privkey.pem;

    # 나머지 설정...
}

이런식으로 ssl_certificate 경로 잡아주면 된다.
본인의 경우 이미 설정해둔 적이 있어서 Pass
설정 바꾸면 reload 진행할것

sudo systemctl reload nginx

자동 갱신을 위한 웹 서버 재시작 설정

sudo certbot renew --deploy-hook "systemctl reload nginx"

/etc/letsencrypt/renewal/parkjb.com.conf 파일에서 [renewalparams] nginx reloading 할 수 있도록 deploy-hook을 아래와 같이 추가해주자

deploy_hook = systemctl reload nginx

테스트

sudo certbot renew --dry-run

--dry-run은 실제 적용하지 않고 되는지 시뮬레이션하는 옵션