상황

  • NGINX를 reverse proxy로 하여 백엔드&프론트가 80포트로 운영되고있음
  • 가비아에서 500원짜리 도메인을 사서 적용해두었음
  • 라즈베리파이 서버 (우분투와 매우 유사)

Certbot 설치

Lets Encrypt는 웹사이트를 위한 인증을 무료로 해주는 인증기관이다.

Certbot은 LetsEncrypt 인증서를 자동으로 발급/갱신해주는 봇 프로그램이다.

 

다음 명령을 통해서 Certbot을 설치하였다.

sudo apt install certbot

 

인증절차 수행 및 인증서 발급

HTTPS를 적용하기 위해서 인증 기관으로부터 인증을 받아야한다.

인증에 성공하면 인증서를 발급해준다.


인증 절차에는 여러가지 방법이 있다.
webroot 방식, stand alone 방식, dns 방식이 그것인데, 나는 webroot 방식으로 수행했다.

 

webroot 방식은

실제 돌아가고 있는 웹 서버에 특정 파일 쓰기 작업을 통해 인증하는 방식이다.

즉, 접근할 수 있는 특정 디렉터리를 제공해서 접근이 가능한지 확인하는 방식이다.

 

(자세한 내용은 포스트 하단 참고 1, 2번 링크)

 

1

80포트로 운영중인 nginx의 설정파일에
아래와 같은 location 설정을 추가한다.

location ^~ /.well-known/acme-challenge/ {
  default_type "text/plain";
  root /var/www/letsencrypt;
}

 

2

/var/www/letsencrypt/.well-known/acme-challenge 디렉토리 경로를 만든다.

mkdir -p /var/www/letsencrypt
cd /var/www/letsencrypt
mkdir -p .well-known/acme-challenge
  • mkdir p 옵션 : 존재하지 않는 중간 디렉토리 자동생성

앞서 webroot 방식은 접근할 수 있는 특정 디렉터리를 제공해서 접근이 가능한지 확인하는 방식이라고 했는데, 그 디렉터리가 /var/www/letsencrypt/.well-known/acme-challenge 인 것이다.

80 포트 웹 요청을 통해서 이 접근이 가능한지를 확인하는 것.

 

3

NGINX를 reload한다.

 

4

인증 과정(인증서 발급)을 시작한다.

sudo certbot certonly --webroot

이 명령이 시작되면 이메일주소도 쓰라고하고 도메인주소도 쓰라고 하니 잘 써주면 된다.


인증이 성공적으로 끝나면 다음과 같이 인증서가 잘 발급되었다고 나온다.

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/myservername/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/myservername/privkey.pem
   Your cert will expire on 2021-10-25. 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"
 - 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

 

5

발급받은 인증서(fullchain.pem, privkey.pem)를 사용하도록 NGINX 설정파일을 수정한다.

새로 추가되거나 변경된 부분에는 문장 끝에 #을 표시해두었다.

### footprint.conf ###
# http 접속시 https 로 리다이렉트
server { #
  listen 80; #
  return 301 https://$host$request_uri; #
} #

server {
  listen 443; #
  server_name footprint;

  ssl on; #
  # 앞에서 생성한, 지정 도메인에 대한 fullchain.pem 경로
  ssl_certificate /etc/letsencrypt/live/seoultechfootprint.shop/fullchain.pem; #
  # 앞에서 생성한, 지정 도메인에 대한 privatekey.pem 경로
  ssl_certificate_key /etc/letsencrypt/live/seoultechfootprint.shop/privkey.pem; #

  location /api {
    proxy_pass http://localhost:8080;
  }

  location / {
    proxy_pass http://localhost:3000;
  }

  location ^~ /.well-known/acme-challenge/ {
    default_type "text/plain";
    root /var/www/letsencrypt;
  }
}

 

6

CORS, 외부 API 정보를 조정해줬다.

 

 ~ 끝 ~

 


참고

1. webroot, stand alone, dns 방식

https://kscory.com/dev/nginx/https

 

2. Letsencryipt webroot 방식 인증

https://xxcv.tistory.com/6

 

3. 인증서발급 완료 후 nginx conf 파일 수정시 참고

https://jackerlab.com/nginx-https-lets-encrypt/

https://soyoung-new-challenge.tistory.com/116

+ Recent posts