본문 바로가기

Dev.BackEnd/Spring & Spring Boot

http를 https로 변경할 수 있도록 무료 ssl 인증서 발급 (certbot설치)

목차

    Certbot이란?

    Certbot Let's encrypt를 이용해서 ssl인증서를 발급, 갱신할 수 있게 도와주는 무료, 오픈소스 툴이다.

     

    1. 설치하기

    nginx는 설치했다고 가정한다.

     

     

    1-1.apt update

    sudo apt-get update

     

    1-2. NGINX HTTP에 대한 방화벽을 해제

    sudo ufw allow 'Nginx HTTP'

     

    1-3. nginx 설정파일의 server_name을 도메인으로 변경한다.

    server { # 80번 포트로 서버 오픈 (ipv4)
      listen 80;
    
      server_name domain_name.com www.domain_name.com;                      
      # 엑세스 로그, 오류 로그를 남길 파일 경로
      # 로그를 남기지 않을경우, 삭제
      access_log /var/log/nginx/access.log;
      error_log /var/log/nginx/error.log;
    
      # /images 경로로 요청이 들어왔을 때 연결되는 경로
        location / {
            proxy_pass http://192.168.0.3:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            #proxy_redirect http://192.168.0.3:8080;
        }
    
          location /data/ {
            alias /opt/selfboardproject/data/;
            try_files $uri $uri/ =404;
          }
    }

     

    1-4. NGINX를 재실행

    sudo systemctl restart nginx

     

    1-5. Certbot을 설치

    sudo add-apt-repository ppa:certbot/certbot
    sudo apt-get install python-certbot-nginx

     

     

    1-6. Certbot으로 nginx에 설정한 도메인에 인증서를 발급

     

    sudo certbot --nginx -d www.domain_name.com -d domain_name.com

     

    설치도중 이런게 나왔을 땐
    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

     

    2번을 해주고 설치를 했다.

     

    congratulations! You have successfully enabled https://laure-date.shop and
    https://www.laure-date.shop

     

    2번을 선택하면 nginx 설정파일이 아래와같이 자동으로 수정되는데 설치할 때 2번 redirect를 선택했기 때문이다.
    해석해보자면 도메인으로 접속할 경우 https. 443포트로 리다이렉트 된다는 내용

     

    server { # 80번 포트로 서버 오픈 (ipv4)
      server_name domain_name.com www.domain_name.com;
      access_log /var/log/nginx/access.log;
      error_log /var/log/nginx/error.log;
    
        location / {
            proxy_pass http://192.168.0.3:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    
          location /data/ {
            alias /opt/selfboardproject/data/;
            try_files $uri $uri/ =404;
          }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/laure-date.shop/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/laure-date.shop/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 = www.domain_name.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = domain_name.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
      listen 80;
      server_name domain_name.com www.domain_name.com;
        return 404; # managed by Certbot
    }

    2. 미래에 인증서 갱신을 해주기 위해 "certonly" 옵션을 주고 다시 한번 실행

    certbot certonly --nginx

     

    3. 자동갱신

    유효 기간이 3개월이므로 3개월 마다 자동 갱신을 해주어야합니다.

    만료 날짜에 신경쓰기 싫을때는 매일 갱신하도록 crontab에 다음과 같이 추가합니다.

     

    Let’s Encrypt의 인증서는 90일동안 유효하고 만료일 30일 이전에 갱신하는 것을 권장한다. certbot renew 명령을 수행하면 만료일이 30일 이내일 인증서만을 갱신하기 때문에 renew명령을 메일 수행해도 된다.

    sudo crontab -e
    
    #매달 1일 자정에 갱신 시도, 갱신 후 nginx 재시작 */
    0 0 1 * * certbot renew --renew-hook="sudo systemctl restart nginx"
    
    /* 크론탭 갱신 */
    sudo service cron restart

     

     

     

    나는 ssl인증서를 발급받아 도메인을 입력후 https://도메인.com으로 로 입력하여 내가 개발한 프로젝트가 뜨는데까지 3일이 걸렸다.

    분명 인증서는 발급되었다고 나오고, 443포트는 열었고, nginx설정도 제대로 한것 같은데, https://www.ssllabs.com/ssltest/index.html  확인하면 인증이 안되었다고 나오고, https로만 가면 404페이지가 뜨거나 

     

     

    이런식으로 계속 나왔다.

     

    결론부터 이야기 하면 포트포워딩으로 해결했다.

     

    당시 nginx는 80포트 들어오면 내부ip:8080포트로 리다이렉트 되고 있었다.

    하지만 이제 https로 nginx가 설정되었기 때문에 외부포트로 443을 받아 https:의 443포트로 갈 수 있도록 내부포트도 443으로 열어놔야 192.168.0.3:8080으로 리다이렉트가 되는 듯 했다. 이걸 깨닫기 까지 3일이나 걸렸고 3일동안 nginx 설정만 chatgpt에 물어보며 수백번 수정, ssl다시 설치 및 톰캣 8080에서 443으로 변경, 8443으로 변경, 포트포워딩 수정등 많은 삽질 끝에 결국 성공은 했다.

     

     

     

     

    참고:

     

    https://velog.io/@kimmjieun/nginx%EC%97%90-SSLHTTPS-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0

     

     

    https://millo-l.github.io/Ubuntu-1804%EC%97%90%EC%84%9C-letsencrypt-HTTPS-%ED%99%98%EA%B2%BD%EA%B5%AC%EC%B6%95/