KO EN

certbot으로 HTTPS설정하기 (Nginx)

by Lovefield

Server /

안녕하세요, Lovefield입니다.

이번 글은 Certbot으로 HTTPS를 설정하는 글입니다. 블로그에 HTTPS를 적용한 지 꽤 오래 되었지만, 글을 쓰는 건 처음이네요.

환경

  • Ubuntu 22.04
  • Nginx

우선 Certbot을 설치하기 위해서 snap이라는 것을 사용하더군요. Ubuntu에 Snap이 없는 경우 설치를 진행해 주셔야 합니다. 그다음 최신 버전을 유지하기 위해 다음 명령어를 사용해줍니다.

sudo snap install core && sudo snap refresh core

Snap이 정상적으로 설치가 되었거나 최신버전으로 업데이트되었다면 Certbot을 설치합니다.

>sudo snap install –classic certbot
certbot 1.32.1 from Certbot Project (certbot-eff✓) installed

위와 같은 설치 완료 안내가 나왔다면 Certbot 명령어를 사용하기 위해서 다음 명령어를 사용해줍니다.

sudo ln -s /snap/bin/certbot /usr/bin/certbot

Certbot 명령어가 정상적으로 동작하는지 확인합니다.

>certbot –version
certbot 1.32.2

버전 정보가 잘 나왔다면 Certbot를 사용할 준비는 끝났습니다. 다음 명령어를 사용하기 전에 Nginx의 config에 80 포트를 기준으로 하는 server 블록이 있어야 합니다.

server {
	listen 80;
	server_name dico.me;

	location / {...}
}

HTTPS를 설정하기 위해 다음 명령어를 실행합니다.

certbot --nginx

첫 실행일 경우 Email을 묻고 서비스 약관과 사용약관에 동의해야 합니다.

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

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

Which names would you like to activate HTTPS for?
We recommend selecting either all domains, or all domains in a VirtualHost/server block.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: dico.me
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
Requesting a certificate for dico.me

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/dico.me/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/dico.me/privkey.pem
This certificate expires on 2023-03-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 dico.me to /etc/nginx/sites-enabled/default
Congratulations! You have successfully enabled HTTPS on https://dico.me

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

위의 과정이 끝나게 되면 Nginx의 설정이 변경되어 있습니다. 이 부분을 그대로 두어도 되지만 저는 개인적으로 깔끔하게 정리하는 편입니다.

server {
	listen 80;
	server_name dico.me;
	return 301 https://dico.me
}

server {
	listen 443 ssl http2;
	server_name dico.me;

ssl_certificate /etc/letsencrypt/live/dico.me/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/dico.me/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

location / {...}	
}

위와 같이 Nginx 설정을 고쳐줍니다. 그러면 사이트의 HTTPS 설정이 끝났는데요. 해당 사항을 적용하기 위해 Nginx를 재시작 해줍니다.

sudo service nginx restart

Nginx를 재시작하고 나면 https://dico.me으로 잘 접속되는 걸 확인 할 수 있습니다. HTTPS 설정이 잘 되었지만, 추가로 해줘야 하는 작업이 있습니다. 바로 인증서의 업데이트 자동화입니다. 인증서는 각각 자신들만의 유효기간을 가지고 있는데요. 이 유효기간이 지나면 인증서의 유효성이 상실되어 브라우저가 사이트에 접근하는 경우 경고를 띄웁니다. 이와 같은 상황을 방지하기 위해 자동으로 인증서를 업데이트하도록 설정해야 하는 것이지요.

sudo certbot renew --dry-run

위의 명령어를 사용하면 Ubuntu의 cron job에 등록이 됩니다.

다음 명령어를 통해 잘 등록이 되었는지 확인합니다.

sudo certbot renew --dry-run

항목중 ACTIVATES 부분에 snap.certbot.renew.service가 존재한다면 정상적으로 잘 등록이 된 겁니다.

이상 HTTPS를 설정하는 글이었습니다.

Author

Lovefield

Lovefield

Web Front-End developer

하고싶은게 많고, 나만의 서비스를 만들고 싶은 변태스러운 개발자입니다.

로그인

디코에 오신 것을 환영해요!
전문가들의 수많은 아티클 창고 🤓