Post

Ubuntu에 letsencrypt를 통해 https를 세팅해보자

목차


개요

https를 사용하기 위해서는 인증서가 필요하다. 그러나 공인되지 않은 인증서의 경우 브라우저에서 경고를 띄우기 때문에 공인된 인증서를 사용해야 한다. letsencrypt를 통해 공인인증서로 https를 세팅해보자.

필자가 기술하는 방법은 letsencrypt로 도메인의 와일드카드 인증서를 발급하는 방법이다.

letsencrypt는 무료 인증서지만 인증서 기간이 90일이므로 90일마다 갱신해주어야 한다.

설치

1
2
3
apt-get update
apt-get install -y letsencrypt
apt-get install -y certbot

아래 명령어로 certbot 이 정상적으로 설치되었는지 확인한다.

1
certbot --version

인증서 발급

명령어의 your_domain.com 값을 실제 도메인 값으로 변경한다.

-d 옵션을 통해 인증서를 발급받을 도메인을 지정할 수 있다.
-d 옵션을 여러번 사용하여 여러 도메인에 대한 인증서를 발급받을 수 있다.

도메인 와일드카드 인증서를 받급받기 위해서는 다음과 같이 설정하면 된다. 쉘 스크립트로 만들어두는 것을 추천한다.

1
2
3
certbot certonly --manual --preferred-challenges dns
-d your_domain.com \
-d *.your_domain.com

처음 진행한다면 이메일을 기입하고 몇가지 정책에 대한 동의를 해야 한다.

이메일 입력
1
2
3
4
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): your_email@email.com
정책 동의

예스멘이 되어주자.

1
2
3
4
5
6
7
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A
1
2
3
4
5
6
7
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing 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
1
2
3
4
5
6
7
8
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
도메인 인증

이후 아래와 같은 메시지가 나온다.

1
2
3
4
5
6
7
8
9
10
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.your_domain.com with the following value:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

이제 당신이 도메인을 구입한 곳으로 가서 TXT 필드로 _acme-challenge.your_domain.com 도메인에 대해 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 값을 입력해주시면 되시겠다.

저장하고 Enter 키를 누르면 또 토큰을 입력하라는 메시지가 나온다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.your_domain.com with the following value:

yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

Before continuing, verify the record is deployed.
(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet. Note that you might be
asked to create multiple distinct TXT records with the same name. This is
permitted by DNS standards.)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

아까와 같은 _acme-challenge.your_domain.com 도메인에 대해 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 값을 입력하라고 한다. 기존의 값을 삭제하지 말고, 아래에 값을 추가하여 토큰 값을 입력해준다.

yyyy.. 토큰을 입력해준 뒤 도메인을 저장하고

구글 도메인을 사용하여 txt 필드에 입력

쉘을 하나 더 열어서 nslookup으로 TXT필드가 정상 출력되는지 확인해보자.

nslookup으로 txt 필드를 확인하는 방법은 아래와 같다.

nslookup -q=txt <your_domain>

1
2
3
4
5
6
7
8
9
# nslookup -q=txt _acme-challenge.your_domain.com
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
_acme-challenge.your_domain.com text = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
_acme-challenge.your_domain.com	text = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"

Authoritative answers can be found from:

잘 적용된 것 같다.

이후 Enter 키를 눌러 인증서 발급을 진행한다. 아래 메시지가 뜨면 인증서가 정상적으로 발급된 것이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/your_domain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/your_domain.com/privkey.pem
   Your cert will expire on 2023-08-28. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - 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

오류가 발생한다면 자신이 토큰 값을 잘못 붙여넣은 것은 아닌지 확인해보자.

인증서 적용

정상적으로 발급이 완료되었다면 /etc/letsencrypt/ 에 인증서가 생성된다.
아래와 같이 인증서가 생성된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/etc/letsencrypt/
  - live/
    - your_domain.com/
      - cert.pem
      - chain.pem
      - fullchain.pem
      - privkey.pem
  - archive/
    - your_domain.com/
      - cert1.pem
      - chain1.pem
      - fullchain1.pem
      - privkey1.pem
  - renewal/
    - your_domain.com.conf

archive 의 fullchain.pem 파일이 인증서, privkey.pem 파일이 개인키이다.

해당 두 파일을 가져다 인증서 및 개인키로 설정해 사용하면 된다.

필자는 해당 두개의 파일을 따로 저장하였다.

1
2
cp -r fullchain1.pem /path/to/your_domain.com.crt
cp -r privkey1.pem /path/to/your_domain.com.key

nginx를 사용한다면 아래와 같이 설정해주면 된다.

1
2
ssl_certificate      /path/to/domain.com.crt;      #인증서 경로
ssl_certificate_key  /path/to/domain.com.key;      #인증서 키 경로
This post is licensed under CC BY 4.0 by the author.