본문 바로가기

Certificates, ssl, tls

Certificates and Let's encrypt

let's encrypt는 인증서를 발급받기 위한 기관 중 하나로 유일하게 무료로 제공됩니다.

 

 

let's encrypt 인증서를 발급받는 방법들은 여러가지이지만, 그 중에 가장 흔히 쓰이는 certbot을 이용하여 발급받아보도록 하겠습니다.

 

$ sudo apt update
$ sudo apt-get install certbot

 

인증서를 발급받기에는 다양한 방식이 존재한다.

 

  • Standalone: 해당 도메인의 인증을 위해 도메인의 작동을 멈추고 유효성을 확인해 인증서를 발급하는 방식이다. 이러한 방식은 동시에 여러 도메인을 발급받을 수 있지만, 도메인을 중간에 중단해야한다.
  • Webroot: 도메인내의 path에 유효성을 확인할 수 있는 파일을 업로드하여 인증서를 발급한다. 이러한 방식은 nginx를 중단시킬 필요가 없지만 하나의 파일에 하나의 도메인 인증서만 발급가능하다.
  • Web server: 웹서버나 nginx에서 인증을 실시하므로 발급이나 갱신을 위해 웹서버를 중단할 필요가 없다. 만약 web server를 통해 인증서를 설치하려면 "apt-get install python-certbot-nginx"를 통해 nginx용 인증서를 설치하면된다.
  • Dns: 우리가 할 방식은 dns 방식이다. 이는 domain provider에서 txt record를 validation하는 작업으로 가장 까다롭고 번거롭지만 가장 큰 장점인 와일드카드 인증이 가능하다는 점이다.

위와 같이 certbot을 설치했다면 다음 명령어를 통해 인증서를 설치하면 된다.

 

sudo certbot -d $SERVER_DOMAIN --agree-tos --register-unsafely-without-email --manual \
--preferred-challenges dns --manual-public-ip-logging-ok \
--force-renewal

 

-d 옵션은 도메인을 입력해주는 것이고, --agree-tos란 ACME Subscriber Agreement에 동의하는 것으로 리눅스 명령어의 -f의 옵션과 같이 yes를 자동으로 입력해주는 것이다.

 

--manual은 자동 갱신을 manual하게 하겠다는 의미이며, --preferred-challenges dns는 dns방식으로 발급받겠다는 의미이다. 보통 두개를 같이 쓰는 이유는 txt record를 manual로 바꿔 validation을 진행하기 때문이다.

 

--manual-public-ip-logging-ok은 공개적으로 ip를 logging해도 되는지 물어보는 것인데, 이는 --manual로 발급받으면, 갱신할 때 다음과 같은 ask가 오면서 인증서 갱신이 실패가 되는데 그걸 오케이 하겠다는 의미이다.

 

Are you OK with your IP being logged?


(You can set this with the --manual-public-ip-logging-ok flag). Skipping.

All renewal attempts failed. The following certs could not be renewed:

 

--force-renewal이란 인증서를 발급할 때 현재 존재하고 있는 인증서와 같은 도메인을 요청하겠다는 의미로 갱신할 때 쓰이는 옵션인데 새로 발급받을 때 써도 무관하다. 이렇게 쓴 이유는 발급과 갱신을 crontab으로 동시에 하기 위함이다. 이는 나중에 갱신관련 글에 올리겠다.

 

위와 같이 발급하면 아래와 같이 인증서들이 발급된다.

 

/etc/live/{doamin_name}/archive/cert1.pem
/etc/live/{doamin_name}/archive/chain1.pem
/etc/live/{doamin_name}/archive/fullchain1.pem
/etc/live/{doamin_name}/archive/privkey1.pem

/etc/live/{doamin_name}/live/cert.pem->../archive/cert1.pem
/etc/live/{doamin_name}/live/chain.pem->../archive/chain1.pem
/etc/live/{doamin_name}/live/fullchain.pem->../archive/fullchain1.pem
/etc/live/{doamin_name}/live/privkey.pem->../archive/privkey1.pem

 

실제 파일은 archive에 있고 갱신할때마다 파일에 있는 숫자가 올라가며 live폴더에 있는 파일들의 link가 바뀌게 되면서 사실상 live폴더내의 인증서를 사용하게 된다.

 

하지만 이러한 certbot은 os에 의존성이 심하므로, 보통 docker를 통해 설치하는 편이다.

 

docker run -it --rm --name certbot \
  -v '/etc/letsencrypt:/etc/letsencrypt' \
  -v '/var/lib/letsencrypt:/var/lib/letsencrypt' \
  certbot/certbot certonly -d '*.yourdomain.com' --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory

 

certonly는 설치하지말고 인증서만 발급받는다는 것이며, --server란 default는 https://acme-v02.api.letsencrypt.org/directory이며, ACME Directory Resource URI이다. 

 

만약 인증서를 삭제하고 싶다면

 

sudo certbot delete --cert-name example.com

 

docker를 통해 설치한 경우 certonly로 발급만 받았기 때문에 해당 volume으로 가서 인증서를 삭제하기만 하면된다.