AWS

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

테런 2024. 6. 12. 17:07
Overview
  • AWS EC2, Nginx, Let's Encrypt 그리고 무료 도메인을 통한 HTTPS 적용을 진행했었습니다. (링크)
  • 이번 글에서는 유료 도메인(가비아 도메인) 구입을 통해 HTTPS 적용을 진행하도록 하겠습니다.

 

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

 

도메인 Flow
  • 웹 브라우저 > 도메인 입력 > 가비아 네임서버 > AWS Route 53 > A 레코드(IP) > EC2 서버

 

가비아 도메인 구입

1. 가비아 접속 (링크)
2. 회원가입 및 로그인
3. 구매 원하는 도메인을 검색해주세요.
4. store나 site 같은 경우는 각각 1년에 500원, 1900원 정도로 매우 저렴합니다. 최소 구매 가능이 1000원 이상이라서 저는 site로 선택했습니다.

 

My가비아에 구입한 도메인을 확인합니다.

 

AWS Route 53 생성

AWS > Route 53 검색 > Route 53 대시보드 > DNS 관리 > 호스팅 영역 생성

 

가비아에서 구입한 도메인 입력하고 생성

 

호스팅 영역 생성 완료

 

레코드 생성 > 레코드 이름 비워두기 > 레코드 유형 A > 값에는 EC2의 Public IP 입력 후 생성

 

taron.site 유형 NS(네임서버)에 값/트래픽 라우팅 대상을 확인합니다. 이 4줄을 가비아 도메인 네임서버에 등록해줄 것 입니다.

 

가비아에서 네임서버 설정으로 들어갑니다.

 

1~4차까지 AWS Route 53의 taron.site 유형 NS(네임서버)에 값/트래픽 라우팅 대상 4줄을 각각 입력합니다.
여기서 주의 사항은 맨 뒤에 .을 제거하고 입력해야 합니다.

* 네임서버 정보변경은 루트서버 갱신 및 네임서버 캐시에 따라, 적용되는데 1~2일 정도 소요될 수 있습니다.
* 처리되는 시간 동안 홈페이지 연결이 어려울 수 있습니다.
* 네임서버 정보 변경의 최종 결과는 고객의 메일로 발송되며, 도메인 정보조회에서 적용된 내용을 확인할 수 있습니다.
* 일시적인 통신 장애가 발생하거나, 이전과 동일한 값을 입력한 후 재설정한 경우 실패로 처리됩니다.

라고 적혀있는 것을 확인할 수 있는데, 저는 5분 정도 후에 도메인이 정상적으로 동작하였습니다.

그럼 모든 도메인 등록 절차를 마무리했으니 확인해보도록 하겠습니다. 

 

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

 

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

// Key 생성
$ certbot --nginx -d taron.site

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 taron.site

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/taron.site/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/taron.site/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 taron.site to /etc/nginx/sites-enabled/default
Congratulations! You have successfully enabled HTTPS on https://taron.site
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: taron.site
    Serial Number: 3b33c603c2ca5cf997c36b86301efe61471
    Key Type: ECDSA
    Domains: taron.site
    Expiry Date: 2024-09-10 06:53:01+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/taron.site/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/taron.site/privkey.pem
    
// 성공적으로 Key가 생성되었는지 확인
$ ls /etc/letsencrypt/live/taron.site
README  cert.pem  chain.pem  fullchain.pem  privkey.pem

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

// 웹 브라우저에서 확인
https://taron.site

 

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

 

추가) Nginx를 리버스 프록시(Reverse Proxy)로 사용하기

AWS Route 53에서 위와 같이 레코드를 생성해줍니다.
이후엔, 제가 이전에 썼던 글 [Server] Nginx 리버스 프록시(Reverse Proxy) 개념 및 구축을 참고해서 적용해보시길 바랍니다. 감사합니다.