Nas Issue

certbot 으로 let's encrypt 인증서 발급

작성자 정보

  • 관리자 작성
  • 작성일

컨텐츠 정보

본문

 certbot 을 통해 let's encrypt 인증서를 발급 받아보자.
현재로서는 가장 간단한 방법인 것 같다. 공식 사이트 가이드를 따라 진행한다. 아래 페이지를 참고했다.


certbot 가이드: https://certbot.eff.org/instructions?ws=nginx&os=centosrhel7 (nginx, centos7)

snapd 가이드: https://snapcraft.io/docs/installing-snap-on-centos


준비


외부에서 접근 가능한 ip

도메인 (여기서는 필자가 소유하고 있는 crudewebtools.com 도메인을 사용한다.)

서버

열린 http(80) 포트 (인증서 발급에 필요), 열린 https(443) 포트 (인증서 발급 받고 사용할 것)

환경

CentOS 7.9

Nginx 1.20.2

참고

본문의 명령들은 모두 root 권한으로 실행하였다. root 가 아닌 경우 일부 sudo 로 실행해야 하는 경우가 있다.




1. Nginx 설치

인증서를 nginx 를 통해 등록하려고 한다. 설치되어 있지 않아서 먼저 설치한다. 이미 설치되어 있다면 생략한다.


nginx 설치는 https://www.nginx.com/resources/wiki/start/topics/tutorials/install/ 를 참고하여 진행하였다.


먼저 repo 를 등록한다. /etc/yum.repos.d/ 디렉토리에 nginx.repo 를 만들고 아래와 같이 작성한다.


[nginx]

name=nginx repo

baseurl=https://nginx.org/packages/centos/7/$basearch/

gpgcheck=0

enabled=1

 

이제 설치하자.


# yum -y install nginx

 


설치가 되었으면 확인해 보자. 1.20.2 로 설치가 되었다.


# nginx -v

nginx version: nginx/1.20.2

 

2. nginx 구동

외부에서 도메인으로 접근 가능한 상태여야 한다. 서버의 ip 에 대한 도메인 등록은 이미 되어 있다고 가정하고 진행한다.


nginx 를 시작해서 접근 가능한지 확인해 보자.


# systemctl status nginx

 

http 로 접근해 보자 http://crudewebtools.com

접근해 보면 nginx 가 구동중임을 알 수 있다. 인증서가 없기 때문에 경고 표시가 나온다.

3695730634_gLS148P3_0898351b0a208c88fdc53f653b17ad3bbb6232b9.png


3. snapd 설치

certbot 설치 페이지에서는 snapd 로 설치하는 것을 권장하고 있다. 먼저 snapd 를 설치해 보자.

먼저 snapd 가 centos 기본 저장소에 없기 때문에, epel-release 설치가 필요하다.


# yum -y install epel-release

 

이제 snapd 를 설치하자.

# yum -y install snapd

 

이제 systemd 에 등록하자.

# systemctl enable --now snapd.socket

Created symlink from /etc/systemd/system/sockets.target.wants/snapd.socket to /usr/lib/systemd/system/snapd.socket.

 

잘 되었는지 확인. enabled 로 되어 있는 것을 볼 수 있다.


# systemctl list-unit-files | grep snapd.socket

snapd.socket                                  enabled

 

공식 페이지에는 classic snap support 를 위해서 아래와 같이 링크를 만들라고 하고 있다. 정확히 어떤 의미인지는 모르겠는데, 나중에 --classic 옵션을 사용하게 되기 때문에 추가해 두도록 하였다.


# ln -s /var/lib/snapd/snap /snap

 

아직 snapd 데몬이 뜨지 않았기 때문에 띄워준다.


# systemctl start snapd

 

 

3. certbot 설치

먼저 snapd 를 최신화하라고 한다.

# sudo snap install core; sudo snap refresh core

 

완료되면 확인해보자

# snap version

snap    2.54.2-1.el7

snapd   2.54.2-1.el7

series  16

centos  7

kernel  3.10.0-1127.19.1.el7.x86_64

 

이제 certbot 을 설치하자

# sudo snap install --classic certbot

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

 

certbot 명령어를 사용하기 위해 심볼릭링크를 걸자.

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

 

확인해 보자.

# certbot --version

certbot 1.22.0

 

잘 된다.


4. 인증서 발급

 

발급을 받아보자.


certonly 옵션은 인증서만 발급받는다는 의미이다. 이 옵션이 없으면 자동으로 nginx 설정을 수정해 준다고 하는데, 어떻게 바꿔주는 지 정확하게 인지하고 있는 것이 아니라면 사용하지 않는 것이 나을 것 같다. 필자의 경우 nginx 설정을 직접 해 줄 것이기 때문에 certonly 옵션을 추가한다.

nginx 에 설정할 것이기 때문에 --nginx 옵션을 추가한다.

 

도메인이 crudewebtools.com 이니 이것도 정확하게 써 준다.

# certbot certonly --nginx -d crudewebtools.com

3695730634_70Jp4Yul_664cad216e63d51001f74750ee52eb027def9832.png


이메일을 요구한다. 적어준다. 

약관 동의 요구에는 y 로 동의하고, 이메일을 통해 소식을 전달해 준다는 것에 대해서는 거절하였다.


인증서는 /etc/letsencrypt/live 아래 위치한다. 정상적으로 발급되었는지 확인해 보자


# ls -l /etc/letsencrypt/live/crudewebtools.com/

total 4

lrwxrwxrwx 1 root root  41 Jan 30 00:09 cert.pem -> ../../archive/crudewebtools.com/cert1.pem

lrwxrwxrwx 1 root root  42 Jan 30 00:09 chain.pem -> ../../archive/crudewebtools.com/chain1.pem

lrwxrwxrwx 1 root root  46 Jan 30 00:09 fullchain.pem -> ../../archive/crudewebtools.com/fullchain1.pem

lrwxrwxrwx 1 root root  44 Jan 30 00:09 privkey.pem -> ../../archive/crudewebtools.com/privkey1.pem

-rw-r--r-- 1 root root 692 Jan 30 00:09 README

 

5. nginx 에 인증서 적용

위에서 발급 받은 인증서를 nginx 에 적용해 보자. 테스트를 위한 것이라 간단하게 /etc/nginx/conf.d/default.conf 를 고쳐보았다. 이 때, nginx 의 경우 꼭 fullchain.pem 을 사용해야 한다.


server {

    listen       80;

    listen       443 ssl;

    server_name  crudewebtools.com;

    ssl_certificate     /etc/letsencrypt/live/crudewebtools.com/fullchain.pem;

    ssl_certificate_key /etc/letsencrypt/live/crudewebtools.com/privkey.pem;

    location / {

        root   /usr/share/nginx/html;

        index  index.html index.htm;

    }

    location = /50x.html {

        root   /usr/share/nginx/html;

    }

}

 

 

nginx 를 reload 하자.

# nginx -s reload

 

이제 https 로 접근해 보자. https://crudewebtools.com

3695730634_U76BkFao_b8fd9cca763078cc29eeba80167c0a8477598a28.png

 

인증서가 적용되었다.


6. 인증서 갱신에 대한 정보

원래 let's encrypt 에서 발급하는 인증서는 유효기간이 3개월이기 때문에 주기적으로 갱신을 해 주어야 한다. 

certbot 의 경우에도 갱신을 위한 명령어가 존재한다.

그런데, 위 과정을 거쳐 certbot 을 통해 인증서를 발급 받은 경우, 자동으로 만료되기 20 일 전 쯤 갱신을 해준다.

해당 예약 명령은 systemd.timer 에 등록되어 있다.


확인해 보자.


# systemctl list-timers

NEXT                         LEFT       LAST                         PASSED       UNIT                         ACTIVATES

Sun 2022-01-30 01:32:00 KST  49min left n/a                          n/a          snap.certbot.renew.timer     snap.certbot.renew.service

Sun 2022-01-30 22:55:40 KST  22h left   Sat 2022-01-29 22:55:40 KST  1h 46min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service

2 timers listed.

Pass --all to see loaded but inactive timers, too.

 

 

서버에 별 문제가 생기지 않는다면 자동으로 알아서 계속 갱신해 줄 것이다.


출처 : https://bitgadak.tistory.com/6

관련자료

댓글 0
등록된 댓글이 없습니다.
전체 7 / 1 페이지

Favorites


최근글


새댓글


  • 댓글이 없습니다.
알림 0