Server Operation

무료 SSL 적용하기

iKay 2019. 7. 16. 00:41
반응형

Ubuntu 18.04 환경에 certi-bot, nginx을 이용해 무료로 SSL을 설정할 수 있다. 물론 도메인이 필요하다. 나는 freenom 이라는 곳에서 무료로 도메인을 이용하는 중이다.

 

1. python-certbot-nginx 설치

 

아래와 같이 python-certnot-nginx 를 설치한다

// repo에 certbot 등록
$ sudo add-apt-repository ppa:certbot/certbot

// update
$ sudo apt-get update

// python-certbot-nginx 설치
$ sudo apt-get install python-certbot-nginx

 

2. nginx 에서 server 설정

 

며칠 전 서버에 jenkins를 설치한적이 있다. 이 jenkins를 SSL 적용시켜보겠다. SSL 설정하기 앞서, 아래와 같이 자신의 상황에 맞게 nginx를 설정해준다. 아래와 같은 경우는 외부에서 jenkins.ikay.cf:80 로 접속 하는 경우, 192.168.0.101:8881로 리버스 프록시 되게 설정한 상황이다. 

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

...

server {
  listen 80;

  server_name jenkins.ikay.cf; // 접속 주소

  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://192.168.0.101:8881; // reverse proxy할 주소

  }
}

...

 

3. SSL 인증서 얻고 세팅하기

 

아래와 같이 certbot에 --nginx, -d {도메인주소} 옵션을 주고 실행시키면 자동으로 인증서를 저장한다. 그리고 중간에 http를 모두 https로 리다이렉트 시킬 것인지 묻는데, 모든 http 요청을 https로 리다이렉트 시키려면 2번을 선택한다. 

 

$ sudo certbot --nginx -d jenkins.ikay.cf
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for jenkins.ikay.cf
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/default

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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// 적힌대로 모든 80 port에 대한 요청을 SSL이 적용된 443 port으로 redirect 시키기 위해 2 번을 선택했다.  
// 별 이유가 없다는 2 번을 선택 하자.
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/default

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://jenkins.ikay.cf

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=api.ikay.cf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/jenkins.ikay.cf/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/jenkins.ikay.cf/privkey.pem
   Your cert will expire on 2019-10-14. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. 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

 

4. nginx 에서 server 설정 변경사항 확인

 

다시 nginx 서버 설정 파일을 열어보면, 아래와 같이 자동으로 변경돼 있을 것이다. 

// $ sudo vim /etc/nginx/sites-available/default

server {

  server_name jenkins.ikay.cf;

  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://192.168.0.101:8080;
  }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/jenkins.ikay.cf-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/jenkins.ikay.cf-0001/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 = jenkins.ikay.cf) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  listen 80;

  server_name jenkins.ikay.cf;
    return 404; # managed by Certbot
}

 

5. SSL 인증서 자동갱신

 

다음 명령어로 인증서를 renew할 수 있다.  하지만 인증서가 아직 만료(expires) 되려면 아직 기간이 조금 남았기 때문에 당장 renew 할 수 없다. 만료되기 30일 전부터 재갱신 시킬 수 있는 것으로 알고 있다.

$ sudo certbot renew

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/jenkins.ikay.cf-0001.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal

The following certs are not due for renewal yet:
  /etc/letsencrypt/live/jenkins.ikay.cf-0001/fullchain.pem expires on 2019-10-13 (skipped)
  /etc/letsencrypt/live/jenkins.ikay.cf/fullchain.pem expires on 2019-10-13 (skipped)
No renewals were attempted.

 

 

인증서를 만들고 renew하지 않을시 90일 정도 뒤엔 인증서가 만료 된다. 그래서 잊기 않고 renew하려면 crontab에 renew 하는 명령어를 등록해주면 좋다. 하지만 이미 인증서를 만들 때, crontab에 인증서를 renew 하는 명령어가 입력돼 있으므로 별 작업 할 것이 없다. 한 번 확인만 해보자. 참고로 아래는 12시간마다 실행되는 cron job 이다.

// $ vi /etc/cron.d/certbot

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

 

 6. SSL이 적용됐는지 확인

이번에 jenkins.ikay.cf 라는 웹 페이지가 nginx를 통해 SSL을 사용하도록 작업했다. 적용됐는지 확인하기 위해 웹 브라우저에 http://jenkins.ikay.cf 라고 입력하고 접속해보자.

 

https://jenkins.ikay.cf 로 redirect 되고 SSL도 정상적으로 적용됐음을 확인할 수 있다.

 

7. 결론

certbot, nginx를 통해 무료로 쉽게 SSL을 적용해봤다. SSL이 만료되는 기간은 90로 비교적 짧으나, crontab에 SSL을 주기적으로 갱신하도록 등록했기 때문에 SSL이 만료될 걱정을 할 필요가 없다.

반응형