Skip to content

SSL 인증서 발급 & HTTPS 세팅

Jaemin Choi edited this page Mar 8, 2021 · 4 revisions

Certbot을 이용한 인증서 발급 🔐

Nginx 상태 확인

다음 명령어로 Nginx가 정상적으로 동작 중인지 확인합니다.

$ sudo nginx -t

아래와 같이 출력된다면 정상임을 뜻합니다. 다른 메시지가 출력된다면 세팅을 점검해주세요.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Certbot 설치

Certbot이 설치되어있지 않다면 다음 명령어로 설치합니다.
apt에 있는 Certbot은 업데이트가 늦어 직접 repo를 등록하여 설치합니다.

$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt install -y python-certbot-nginx

SSL 인증서 발급

다음 명령어로 인증서를 발급받습니다.

$ sudo certbot certonly --nginx

출력되는 메시지에 맞는 옵션을 선택하여 발급을 진행합니다.
(옵션 설명 추가 예정)

발급에 성공했다는 메시지가 출력되면, 다음 경로에 인증서가 발급되었는지 확인합니다: /etc/letsencrypt/live/npc.skku.edu

$ sudo ls /etc/letsencrypt/live/npc.skku.edu

cert.pem, chain.pem, fullchain.pem, privkey.pem 4개의 파일이 있으면 정상입니다.


도커 컨테이너에 인증서 적용 🐳

oj 서버는 다음 경로에 있는 인증서를 인식합니다: /home/dotoleeoak/plz/data/backend/ssl
해당 경로로 이동하면 server.crtserver.key 두 파일을 확인할 수 있습니다.
앞서 발급받은 인증서를 다음 명령어를 통해 이 경로로 복사합니다.

$ sudo cp /etc/letsencrypt/live/npc.skku.edu/cert.pem /home/dotoleeoak/plz/data/backend/ssl/server.crt
$ sudo cp /etc/letsencrypt/live/npc.skku.edu/privkey.pem /home/dotoleeoak/plz/data/backend/ssl/server.key

변경된 인증서 내용을 도커 컨테이너에 적용합니다.

$ docker exec -it coding-platform sh -c "cd /app/deploy; supervisorctl restart nginx"

HTTP -> HTTPS redirection

강제로 HTTPS로 접속시키려면, docker-compose.yml 파일에서 FORCE_HTTPS=1의 주석을 해제합니다. (# 제거)
이후, 다음 명령어로 도커 컨테이너에 해당 내용을 적용합니다.

$ docker-compose up -d

인증서 갱신 ⌛

인증서의 유효기간은 90일이기 때문에, 일정 기간마다 갱신해야 합니다.
앞서 'SSL 인증서 발급' 절차부터 반복하여 갱신할 수 있습니다.
(shell script 이용한 갱신 구현 예정)


Reference

Certbot Instructions(Nginx on Ubuntu 18.04)
OnlineJudge 공식 문서 중 'HTTPS related issues'
Ubuntu Nginx 환경에서 CertBot을 사용하여 https 사용하기