내맘대로 개발 일기/뽀모로 Do!

[인프라 구축] Let’s Encrypt, SSL/TLS 인증서 발급하기

bu119 2024. 6. 16. 09:00
728x90
반응형

 

우리는 호스트 시스템에 Certbot을 활용하여 Let’s Encrypt SSL/TLS 인증서를 발급받을 것이다.

 

그 이유는,

우리는 토이 프로젝트이고 서버 자원을 최대한 아껴야 한다. 이렇게 하면 Docker 컨테이너에 의해 추가되는 오버헤드를 피하고, 시스템 자원을 절약할 수 있다. 따라서,  Certbot을 호스트 시스템에 직접 설치하는 것이 더 적합할 수 있다.

 

Docker로 실행을 권장하는 경우:

  • 다양한 서버 환경에서 일관된 설정이 필요할 때
  • DevOps 관행에 따라 컨테이너화된 환경을 선호할 때
  • 시스템 격리와 독립성이 중요한 경우
  • Certbot 설치 및 업그레이드의 간편함이 필요할 때

호스트 시스템에 직접 설치를 권장하는 경우:

  • Docker를 사용하지 않거나 Docker 사용이 복잡한 경우
  • 시스템 자원을 최소화하고자 할 때
  • 단일 서버에서 간단한 설정이 필요한 경우
  • 종속성 관리가 용이하고, Certbot이 서버의 다른 소프트웨어와 충돌할 가능성이 없을 때

 

1. 패키지 인덱스 업데이트하기

시스템이 최신 패키지 정보를 갖도록 업데이트한다.

sudo apt update

 

  • 패키지 인덱스는 시스템에서 사용할 수 있는 패키지와 그 버전에 대한 데이터베이스이다.
  • 업데이트를 하면 최신 소프트웨어 정보와 버전을 확보할 수 있다.

 

 

실습

ubuntu@ip-000-000-000-000:~$ sudo apt update

Hit:1 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy-updates InRelea   se
Hit:3 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy-backports InRel   ease
Hit:4 https://download.docker.com/linux/ubuntu jammy InRelease
Get:5 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB]
Fetched 129 kB in 1s (117 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
99 packages can be upgraded. Run 'apt list --upgradable' to see them.

 

2. Certbot 설치하기

Certbot은 Let's Encrypt SSL/TLS 인증서를 얻고 관리하는 도구이다.

apt를 통해 Certbot 도구를 시스템에 설치한다.

sudo apt install certbot -y
  • -y 옵션은 설치 중 묻는 질문에 자동으로 "예"라고 답한다.

 

실습

ubuntu@ip-000-000-000-000:~$ sudo apt install certbot

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  python3-acme python3-certbot python3-configargparse python3-icu python3-josepy
  python3-parsedatetime python3-requests-toolbelt python3-rfc3339
  python3-zope.component python3-zope.event python3-zope.hookable
Suggested packages:
  python-certbot-doc python3-certbot-apache python3-certbot-nginx
  python-acme-doc
The following NEW packages will be installed:
  certbot python3-acme python3-certbot python3-configargparse python3-icu
  python3-josepy python3-parsedatetime python3-requests-toolbelt python3-rfc3339
  python3-zope.component python3-zope.event python3-zope.hookable
0 upgraded, 12 newly installed, 0 to remove and 99 not upgraded.
Need to get 958 kB of archives.
After this operation, 4915 kB of additional disk space will be used.

Do you want to continue? [Y/n] Y

Get:1 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 python3-josepy all 1.10.0-1 [22.0 kB]
Get:2 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/main amd64 python3-requests-toolbelt all 0.9.1-1 [38.0 kB]
Get:3 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/main amd64 python3-rfc3339 all 1.1-3 [7110 B]
Get:4 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-acme all 1.21.0-1ubuntu0.1 [36.4 kB]
Get:5 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 python3-configargparse all 1.5.3-1 [26.9 kB]
Get:6 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 python3-parsedatetime all 2.6-2 [32.9 kB]
Get:7 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 python3-zope.hookable amd64 5.1.0-1build1 [11.6 kB]
Get:8 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 python3-zope.event all 4.4-3 [8180 B]
Get:9 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 python3-zope.component all 4.3.0-3 [38.3 kB]
Get:10 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 python3-certbot all 1.21.0-1build1 [175 kB]
Get:11 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/main amd64 python3-icu amd64 2.8.1-0ubuntu2 [540 kB]
Get:12 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 certbot all 1.21.0-1build1 [21.3 kB]
Fetched 958 kB in 0s (21.9 MB/s)
Preconfiguring packages ...
Selecting previously unselected package python3-josepy.
(Reading database ... 94226 files and directories currently installed.)
Preparing to unpack .../00-python3-josepy_1.10.0-1_all.deb ...
Unpacking python3-josepy (1.10.0-1) ...
Selecting previously unselected package python3-requests-toolbelt.
Preparing to unpack .../01-python3-requests-toolbelt_0.9.1-1_all.deb ...
Unpacking python3-requests-toolbelt (0.9.1-1) ...
Selecting previously unselected package python3-rfc3339.
Preparing to unpack .../02-python3-rfc3339_1.1-3_all.deb ...
Unpacking python3-rfc3339 (1.1-3) ...
Selecting previously unselected package python3-acme.
Preparing to unpack .../03-python3-acme_1.21.0-1ubuntu0.1_all.deb ...
Unpacking python3-acme (1.21.0-1ubuntu0.1) ...
Selecting previously unselected package python3-configargparse.
Preparing to unpack .../04-python3-configargparse_1.5.3-1_all.deb ...
Unpacking python3-configargparse (1.5.3-1) ...
Selecting previously unselected package python3-parsedatetime.
Preparing to unpack .../05-python3-parsedatetime_2.6-2_all.deb ...
Unpacking python3-parsedatetime (2.6-2) ...
Selecting previously unselected package python3-zope.hookable.
Preparing to unpack .../06-python3-zope.hookable_5.1.0-1build1_amd64.deb ...
Unpacking python3-zope.hookable (5.1.0-1build1) ...
Selecting previously unselected package python3-zope.event.
Preparing to unpack .../07-python3-zope.event_4.4-3_all.deb ...
Unpacking python3-zope.event (4.4-3) ...
Selecting previously unselected package python3-zope.component.
Preparing to unpack .../08-python3-zope.component_4.3.0-3_all.deb ...
Unpacking python3-zope.component (4.3.0-3) ...
Selecting previously unselected package python3-certbot.
Preparing to unpack .../09-python3-certbot_1.21.0-1build1_all.deb ...
Unpacking python3-certbot (1.21.0-1build1) ...
Selecting previously unselected package python3-icu.
Preparing to unpack .../10-python3-icu_2.8.1-0ubuntu2_amd64.deb ...
Unpacking python3-icu (2.8.1-0ubuntu2) ...
Selecting previously unselected package certbot.
Preparing to unpack .../11-certbot_1.21.0-1build1_all.deb ...
Unpacking certbot (1.21.0-1build1) ...
Setting up python3-configargparse (1.5.3-1) ...
Setting up python3-requests-toolbelt (0.9.1-1) ...
Setting up python3-parsedatetime (2.6-2) ...
Setting up python3-icu (2.8.1-0ubuntu2) ...
Setting up python3-zope.event (4.4-3) ...
Setting up python3-zope.hookable (5.1.0-1build1) ...
Setting up python3-josepy (1.10.0-1) ...
Setting up python3-rfc3339 (1.1-3) ...
Setting up python3-zope.component (4.3.0-3) ...
Setting up python3-acme (1.21.0-1ubuntu0.1) ...
Setting up python3-certbot (1.21.0-1build1) ...
Setting up certbot (1.21.0-1build1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning candidates...
Scanning linux images...

Restarting services...
Service restarts being deferred:
 /etc/needrestart/restart.d/dbus.service
 systemctl restart docker.service
 systemctl restart getty@tty1.service
 systemctl restart networkd-dispatcher.service
 systemctl restart systemd-logind.service
 systemctl restart unattended-upgrades.service

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

 

 

 

3. 도메인의 SSL/TLS 인증서 발급하기 - Let’s Encrypt

Certbot을 사용하여 도메인의 SSL/TLS 인증서를 얻는다.

# certbot을 사용하여 SSL 인증서 발급
sudo certbot certonly --standalone -d 도메인주소
  • certonly 옵션은 인증서만 얻고 설치하지 않음을 의미한다.
  • -d 옵션은 도메인 이름을 지정한다.

이 명령어를 통해

  • 자체 임시 웹 서버를 사용하여 도메인 소유권을 증명한다.
  • Let's Encrypt 서버와 통신하여 인증서를 얻는다.
  • 인증서와 키 파일을 시스템에 저장한다.
    • 일반적으로 /etc/letsencrypt/live/도메인/ 디렉토리에 저장된다.

 

명령어를 실행하면 이메일 주소를 입력하고, Let's Encrypt 서비스 약관에 동의하는 등의 절차가 진행된다.

 

실습

발급된 인증서는 /etc/letsencrypt/live/pomorodo.shop/ 경로에 저장된다.

ubuntu@ip-000-000-000-000:~$ sudo certbot certonly --standalone -d pomorodo.shop

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): tico240516@gmail.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: N

Account registered.
Requesting a certificate for pomorodo.shop

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/pomorodo.shop/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/pomorodo.shop/privkey.pem
This certificate expires on 2024-09-13.
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

 

추가 참고 사항

  • 권한: 소프트웨어 설치 및 인증서 획득에는 관리자 권한이 필요하므로 sudo 명령어가 필요하다.
  • DNS 레코드: 도메인(pomorodo.shop)이 명령어를 실행하는 서버로 올바르게 포워딩되어 있는지 확인해야 한다. 이는 도메인 검증 과정에 필요하다.
  • 방화벽: 방화벽이 도메인 검증이 원활하게 진행되도록 80번 포트(HTTP)와 443번 포트(HTTPS) 트래픽을 허용하는지 확인해야 한다.

이 단계를 따라가면 도메인에 대한 SSL/TLS 인증서를 성공적으로 얻을 수 있으며, 이를 통해 HTTPS로 웹사이트를 안전하게 보호할 수 있다.


참고 자료

https://www.kamatera.com/knowledgebase/how-to-secure-nginx-with-lets-encrypt/

728x90
반응형