AWS

[AWS] EC2, Nginx, Let's Encrypt를 통한 HTTPS 적용 (무료 도메인)

테런 2024. 6. 12. 13:56
Overview
  • AWS EC2, Nginx, Let's Encrypt 그리고 무료 도메인을 통한 HTTPS 적용을 진행해보겠습니다.

 

Prerequisite
  • AWS EC2 생성
    • Public IP 할당, Inbound Port는 22, 80, 443 오픈

 

Nginx 설치
// EC2에 접속
$ ssh -i {*-key.pem} ubuntu@{ip}

// Nginx 설치
$ sudo apt update
$ sudo apt install nginx

// 상태 확인 (active)
$ sudo systemctl status nginx

// 웹 브라우저로 접속해서 확인 > http://{Public IP}
// 아래 그림처럼 접속이 된다면 성공

// Default Page HTML
$ cat /var/www/html/index.nginx-debian.html

// Nginx 삭제
$ sudo apt-get remove --purge nginx nginx-full nginx-common

Nginx Default Page

 

Free Wildcard DNS 서비스
개발을 진행하다보면 HTTPS 적용을 해야하는 상황이 많이 발생합니다. 개발 단계에서는 도메인이 없는 경우가 많아서 HTTP로만 진행하는 경우가 많습니다. HTTPS에서만 동작하는 것이 기본인 오픈소스들이 많고, 상용에서도 HTTPS가 필수적으로 적용되어야 하기 때문에 개발 단계에서도 HTTPS를 적용해서 진행하는 것이 효율적입니다.

HTTPS를 적용하기 위해서 전제 조건은 도메인이 있어야 합니다. 도메인을 구매할수도 있지만, 개발 환경에서는 Free Wildcard DNS 서비스를 통해서 무료 도메인으로 진행할 수 있습니다.
* Free Wildcard DNS 서비스
1. nip.io
2. sslip.io
와일드카드(Wildcard) 인증서란, 도메인(FQDN) 의 하위 서브 도메인 호스트에 대해서 무제한 적용할수 있는 SSL 인증서 타입입니다. 즉, 서브 도메인에 대해서는 무제한입니다. *.{Public IP}.nip.io

예를 들어, 웹 브라우저에서 아래처럼 입력한 후 접속해보시길 바랍니다.

abc.{Public IP}.nip.io
dev.{Public IP}.nip.io
dev.argocd.{Public IP}.nip.io
dev.gitlab.{Public IP}.nip.io

 

HTTPS 적용 (Let's Encrypt)
웹 서버와 도메인이 준비됐으니 HTTPS 적용하도록 하겠습니다.
// Certbot 설치
$ sudo su -
$ snap install --classic certbot
$ ln -s /snap/bin/certbot /usr/bin/certbot

// Key 생성
$ certbot --nginx -d {Public IP}.nip.io -d www.{Public IP}.nip.io

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): 이메일 입력
                                                                            
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf. You must agree in
order to register with the ACME server. Do you agree?
(Y)es/(N)o: Y

Would you be willing, once your first certificate is successfully issued, to    
share your email address with the Electronic Frontier Foundation, a founding    
partner of the Let's Encrypt project and the non-profit organization that       
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
(Y)es/(N)o: Y
Account registered.
Requesting a certificate for {Public IP}.nip.io and www.{Public IP}.nip.io

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/{Public IP}.nip.io/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/{Public IP}.nip.io/privkey.pem
This certificate expires on 2024-09-10.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for {Public IP}.nip.io to /etc/nginx/sites-enabled/default
Successfully deployed certificate for www.{Public IP}.nip.io to /etc/nginx/sites-enabled/default
Congratulations! You have successfully enabled HTTPS on https://{Public IP}.nip.io and https://www.{Public IP}.nip.io
We were unable to subscribe you the EFF mailing list because your e-mail address appears to be invalid. You can try again later by visiting https://act.eff.org.
 
// 인증서 확인
$ certbot certificates
Found the following certs:
  Certificate Name: {Public IP}.nip.io
    Serial Number: 32626a227986ca703a59194081d708c7e70
    Key Type: ECDSA
    Domains: {Public IP}.nip.io www.{Public IP}.nip.io
    Expiry Date: 2024-09-10 03:01:08+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/{Public IP}.nip.io/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/{Public IP}.nip.io/privkey.pem
    
// 성공적으로 Key가 생성되었는지 확인
$ ls /etc/letsencrypt/live/{Public IP}.nip.io
README  cert.pem  chain.pem  fullchain.pem  privkey.pem

// Certbot이 자동으로 Nginx에 HTTPS 설정
$ cat /etc/nginx/sites-available/default

// 웹 브라우저에서 확인
https://{Public IP}.nip.io
https://www.{Public IP}.nip.io

 

 

HTTPS 인증서 (Let's Encrypt) 자동 갱신
Let's Encrypt는 기본적으로 3개월 마다 갱신을 해줘야 합니다. 번거로우니 자동 갱신하도록 설정하겠습니다.
// Root 말고 User에서 진행
// Certbot 인증서 갱신 명령어 테스트
$ sudo certbot renew --renew-hook "sudo systemctl restart nginx" --dry-run

// crontab 수정
$ crontab -e

// 매달 1일 자정에 인증서 갱신을 시도. 갱신 후 nginx 재시작하는 명령어 저장
0 0 1 * * sudo certbot renew --renew-hook "sudo systemctl restart nginx"

// 테스트 (로그 확인) - crontab 시간을 조정해서 스크립트가 잘 동작하는지 테스트
$ tail -f /var/log/syslog | grep CRON