nginx 우분투에서 무료로 ssl 적용하기

장후후·2021년 7월 25일
0

nginx

목록 보기
1/1
post-thumbnail

정적 웹페이지 배포용 웹서버로 nginx를 많이 채택하는데,
기본은 http로 되어있기 때문에, 이 포스팅에서는 https로 적용하는 방법을 알려드리겠습니다.

주의
도메인은 사전에 설정되어 있어야 합니다!

1. certbot 설치 및 nginx용 certbot설치

저장소 세팅은

Ubuntu 16.04(LTS), Ubuntu 18.04(LTS)

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository universe
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update

Ubuntu 20.04(LTS)

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository universe
$ sudo apt-get update

certbot 설치

$ sudo apt-get update
$ sudo apt-get install certbot python3-certbot-nginx 

2. Nginx 세팅

이 예시에서 사용한 Nginx 버젼은 nginx/1.10.3 (Ubuntu) 입니다.

아래 명령어로 default 파일 편집을 킵니다.

$ sudo vim /etc/nginx/site-avaliable/default
#configuration of the server

server {

    listen      80;
    listen [::]:80;
    # Put your domain next to server_name
    server_name sample.com;

...

}

server_name만 https를 사용할 도메인으로 수정하여 저장합시다~!

저장 후, nginx 설정 파일 검증과 service 재시작을 해줍니다.

$ sudo nginx -t 
$ sudo service nginx reload

3. SSL 인증 추가 및 인증서 자동갱신

이제 certbot을 사용해서 도메인에 인증서를 발급해보겠습니다.

$ sudo certbot --nginx -d example.com -d www.example.com

명령어 실행 후, http 요청이 들어오면 redirect 할 것인지에 대한 화면이 표시됩니다.
2번을 누르면 리다이렉트 설정을 진행할 수 있습니다.

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

certbot으로 추가한 인증서는 만료기한이 90일이기 때문에 아래와 같이, 자동갱신을 설정해 놓습니다.

$ sudo certbot renew --dry-run

참고자료

https://jp-hosting.jp/nginx-lets-encrypt%EB%A5%BC-%ED%86%B5%ED%95%B4-nginx%EC%97%90%EC%84%9C-%EB%AC%B4%EB%A3%8C%EB%A1%9C-https-%EC%84%A4%EC%A0%95%ED%95%98%EA%B8%B0/

https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04

profile
Backend Developer, DevOPS Engineer, IIoT, IoT

0개의 댓글