본문 바로가기
Django

Django 평생 무료로 https 서버 설정하는 방법

by 뷰티풀스택 2023. 4. 7.
반응형

이전 글에서 Django를 서버에 80포트와 도메인 주소로 성공적으로 세팅을 완료하였다면 이제는 마지막으로 보안설정이 되어 있는 https 443 포트로 서비스 될 수 있도록 설정하는 것만 남았다. Let's Encrypt를 활용하는 것인데 기본적으로 3개월정도만 무료인데 약간의 트릭을 써서 평생 무료로 설정할 수 있는 팁이 들어있으니 끝까지 정독하기를 바란다.

 

 

1. Certbot 설치

  • 설치하는 방법이 여러가지가 있는데 우분투의 snap을 이용하는 것이 단언하는데 가장 간단하고 깔끔하게 설치하는 방법이다. Nginx conf 파일을 세팅하라고 알려주는 글들은 정말 직접 해보고 사람들한테 알려주고 싶어서 글을 쓰는 건지 외국 사이트 그냥 복사하는 건지 궁금하다. 저런 글들때문에 너무 고생해서 이 글을 보는 개발자들은 다른데서 헤매지 말고 아래의 방법으로 시간을 단축하기를 바란다. 아래의 방법으로 하면 정말 10분도 안걸린다.
sudo snap install certbot --classic

certbot 2.5.0 from Certbot Project (certbot-eff✓) installed

 

2. Nginx와 연결

  • 다른 설정 파일 건들일 필요없이 certbot --nginx 한줄만 입력하면 모든 세팅이 자동으로 완료된다.
sudo certbot --nginx

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.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: N
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: abc.com (자신의 도메인주소)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 abc.com

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

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

 

3. 인증서 자동 갱신

  • crontab에 매일 00:00 시간에 인증서를 갱신하도록 한다. 인증서 기간이 남아 있으면 갱신을 하지 않으니 매일 작업을 하면 부하를 주지 않을까 걱정할 필요가 없다.
crontab -e (sudo 권한 없이 현재 user 권한으로 실행하면 됨)
  • 편집창이 열리면 아래의 내용 작성한다. certbot을 renew 하고 나서 nginx가 새로 갱신된 설정 파일을 다시 읽도록 하는 명령이라고 보면된다. 참고로, crontab은 실행화면이 보이지 않기 때문에 >> 를 사용해서 로그를 남겨두어야 편리하다. 
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
0 0 * * * sudo /snap/bin/certbot renew --post-hook "sudo service nginx reload" >> /home/ubuntu/logs/certbot.txt

 

반응형

댓글