번개애비의 라이프스톼일

nginx proxy서버에서 Letsencrypt 인증서를 받고 자동 리뉴얼 하기 (Centos 7기준) 본문

IT

nginx proxy서버에서 Letsencrypt 인증서를 받고 자동 리뉴얼 하기 (Centos 7기준)

번개애비 2019. 5. 23. 12:58
sudo yum install epel-release
sudo yum install certbot
sudo yum install python2-certbot-nginx

Letsencrypt 인증서를 발급받기 위해 EPEL과 Certbot을 설치합니다.

 

 

---------------------------------------------------------------------------

아래 내용이 귀찮으면 걍 이렇게 입력하면 알아서 certbot이 등록해줍니다.

sudo certbot --nginx

상기 명령어는 아래 절차를 모두 자동으로 진행해줍니다 :)

---------------------------------------------------------------------------

 

 

 

 

설치가 끝나면 SSL을 적용해야 하겠죠?

SSL적용전에 nginx를 잠시 종료합니다.

sudo service nginx stop
sudo certbot  --standalone -d example.com certonly

-d 다음에 도메인을 입력하시고 certonly옵션을 설정합니다.

certonly는 certbot이 apache나 nginx의 설정값을 건드리지 말고 인증만 받겠다는 옵션입니다.

 

 

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):

이메일을 입력해주시고

 

Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel:

A를 눌러주시고

 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o:

Y를 눌러주시면 됩니다.

 

- Congratulations! Your certificate and chain have been saved at:

Congratulations가 나타나면 성공한 것입니다!

 

 

nginx proxy에 적용하는 방법은 아래 예시를 참고하시면 됩니다.

server {
	listen  443 ssl http2;
        listen  [::]:443 ssl http2;
	server_name test.xn--299al5mjtpp8b.com;

	ssl_certificate /etc/letsencrypt/live/test.xn--299al5mjtpp8b.com/fullchain.pem; 
	ssl_certificate_key /etc/letsencrypt/live/test.xn--299al5mjtpp8b.com/privkey.pem;

        location / {
            proxy_redirect off;
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://10.89.49.177/;
        }
 }

 

 

자 이제 기분좋게 nginx을 다시 시작합니다.

sudo service nginx start

 

 

서비스에서 https가 정상적으로 작동되는지 인증서도 한번 확인하시길 바랍니다.

마지막으로 letsencrypt의 경우 만료기간이 90일 정도밖에 되지 않기 때문에 계속 리뉴얼을 해주어야 합니다.

자동화를 위해 Crontab을 사용해보겠습니다

crontab -e
0 3 * * 0 sudo certbot renew --standalone --pre-hook "service nginx stop" --post-hook "service nginx start"

한달에 한번 리뉴얼 요청을 등록해놓았습니다. 뒤에 보시는 --pre-hook과 --post-hook의 경우 앞의 실행문 전에 실행하고 실행뒤 실행하는 옵션입니다.

SSL인증서도 바뀌었으니 nginx도 껏다 켜야되기 때문에 위 옵션을 적용하였습니다.

 

마지막으로 아래 명령어를 이용하여 정상적으로 crontab에 등록되었는지 확인하면 됩니다.

crontab -l
Comments