nginx 서버에 letsencrypt를 이용하여 SSL 적용하기

janghoosa·2022년 6월 29일
0

Infra

목록 보기
2/4
post-thumbnail

http를 사용하면 보안에 대한 이슈가 많이 생겨 요즘은 SSL을 적용시켜 https를 사용하는 것은 필수이다. FE 및 BE에 각각 적용을 시키는 것보다 앞단의 nginx에서 SSL을 처리를 해주면 더 간단하게 적용 할 수 있을 거 같아 작업을 해보았다.

필요한 것들

  • nginx가 설치된 서버
  • 서버에 도메인이 연결되어있어야함

작업환경

  • NHN Cloud Instance (서버)
  • NHN Cloud DNS Plus (DNS 연동)
  • Ubuntu Server 20.04 LTS
  • nginx 1.18.0 (Ubuntu)

작업시작

letsencrypt란?

Let's Encrypt는 사용자에게 무료로 TLS 인증서를 발급해주는 비영리기관이다.
이 글에서는 https://letsencrypt.org/ko/getting-started 에서 제공하는 Shell Access 권한이 있을 경우에 사용하는 certbot client를 이용하여 작업을 하였다.

1. certbot 설치

certbot은 자동으로 인증서를 발급받고 관리 및 갱신해주는 프로그램이다.
https://certbot.eff.org/instructions?ws=nginx&os=ubuntufocal 에서 nginx와 ubuntu를 선택하여 설치방법을 따라하였다.

certbot 사이트에 나와있는 설치방법

  1. SSH into the server
  2. Install snapd
  3. Ensure that your version of snapd is up to date
  4. Remove certbot-auto and any Certbot OS packages
  5. Install Certbot
  6. Prepare the Certbot command
  7. Choose how you'd like to run Certbot
  8. Test automatic renewal
  9. Confirm that Certbot worked

1.1 pem 키를 이용하여 ssh로 서버에 접속한다.

1.2 snapd를 설치한다.

sudo apt update
sudo apt-get install snapd

1.3 snapd를 최신버전으로 업데이트 한다.

sudo snap install core; sudo snap refresh core

1.4 다른 certbot 패키지들이 있는지 확인하고 있다면 제거해준다.

sudo apt-get remove certbot

1.5 certbot 설치

sudo snap install --classic certbot

1.6 certbot 커맨드를 이용하기 위해 로컬 폴더에 링크

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

1.7 이후의 과정은 아래의 과정을 진행 후 다시 작업

2. nginx 설정

2.1 nginx conf 파일 수정

ubuntu 기본 설정을 따라 nginx 설정파일을 연다.

sudo vi /etc/nginx/sites-available/default

server_name의 example.com을 자신의 도메인에 맞게 설정하면 된다.
이 서버는 FE의 배포 서버이므로 아래와 같이 location을 설정하였다.

server {
  listen        80;
  server_name   example.com;
  location / {
    root        /home/ubuntu/Project/Front/build;
    index       index.html index.htm;
    try_files   $uri /index.html;
  }
}

위와 같이 default 파일을 변경한 후 :wq를 사용하여 저장한다.

2.2 certbot 실행

sudo certbot --nginx

위 명령어를 실행하면 이메일을 적어주는 것이 나온 후 약관을 묻는다.
이메일을 적어주고 동의하자.

그 이후 자신이 사용할 도메인을 선택하거나 입력해주면 작업이 완료된다.
Congratulations! 가 출력되면 https가 적용된 것이다.

2.3 nginx 재실행 및 redirect 추가

sudo systemctl restart nginx

nginx를 재시작해주면 https가 잘 작동하는 것을 볼 수 있다.

원래는 nginx 설정내에서 80포트를 443포트로 redirect 해주는 코드가 필요하지만 certbot이 알아서 default파일을 아래와 같이 바꾸어 놓으니 따로 작업을 안해주어도 된다.

# sudo vi /etc/nginx/sites-available/default
server {
  server_name   example.com;
  location / {
    root        /home/ubuntu/Project/Front/build;
    index       index.html index.htm;
    try_files   $uri /index.html;
  }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/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

}
server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  listen        80;
  server_name   example.com;
    return 404; # managed by Certbot
}

이번 포스트에서는 FE 배포 서버를 기준으로 하였으나 BE API 서버는 nginx의 프록시 서버를 이용하여 동일한 과정을 진행하면 가능하다.

잘못된 내용이 있으면 댓글에 남겨주시면 감사하겠습니다🥺

profile
백엔드 개발자 지망생입니다 :)

0개의 댓글