ssl

  • certbot을 이용해 인증
    • 무료
    • 인증서 유효기간 90일
  • http를 https로 접속이 가능해진다.
    • 인증된 페이지가 된다.
  • 해당 환경은 ubuntu16.04로 진행하였다.

기본설치

  • 포트 열기
    • 80번과 추가적으로 ssl을 위해 443번을 열자
    • ncloud는 acg로 추가
  • repository 추가
    • sudo add-apt-repository ppa:certbot/certbot
  • update
    • sudo apt-get update
  • 설치
    • sudo apt-get install certbot
    • sudo apt-get install python-certbot-nginx

certbot 인증하기

  • certbot 설정
    • sudo certbot --nginx -d example.com
    • 초기에 하면 이메일도 작성하라고 나옴
    • 1(no redirect)과 2(redirect)를 선택 가능
      • 2를 하면 nginx 설정에 알아서 추가 된다.
  • 인증 확인
    • sudo ls /etc/letsencrypt/live/example.com
  • 인증서 남은기간
    • certbot certificates
  • nginx재시작
    • service nginx reload

nginx의 설정 설명

  • 위에서 2번을 선택 하면 nginx 설정 파일에 알아서 추가된다.
    • 기존이 한개의 server만 사용했다면 ssl로 인해 server가 두개가 만들어진다.
  • 443포트와 80번 포트로 두개로 나뉘게 된다.
    • 443에서 실제로 https가 연결
      • 이를 위해 cerbot으로 위에서 만든 인증키들이 추가됨
    • 80은 http
      • 리다이렉트 되도록 수정됨
server{
    ##### Certbot이 생성 https로 해당하는 443포트로 연결
    listen 443 ssl; 
    #####
    server_name loa.deokisys.site;
    root /var/www/foundark;

    ##### Certbot이 생성한 키들
    ssl_certificate /etc/letsencrypt/live/도메인/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/도메인/privkey.pem; 
    include /etc/letsencrypt/options-ssl-nginx.conf; 
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
    ##### 

    ###### 기존 설정한 부분으로 서비스되는 빌드된 html or proxy_pass를 작성하면됨
   index index.html index.htm index.nginx-debian.html;


    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
      expires 1h;
    }

    location ~* \.(?:css|js)$ {
      try_files $uri =404;
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }
    location ~ ^.+\..+$ {
      try_files $uri =404;
    }
    location / {
        try_files $uri $uri/ /index.html;
    }
    ######
    
}
###### Certbot이 생성 http로 해당하는 80번 포트를 접속 못하게 
server {
    if ($host = loa.deokisys.site) {
        return 301 https://$host$request_uri;
    } 
   listen 80;
   server_name loa.deokisys.site;
    return 404; 
}
#####

인증서 자동갱신

  • 90일동안만 유지되므로 certbot renew를 주기적으로 해줘야 한다.
  • crontab을 활용하여 이를 해결한다.
    • 15 3 1 * * certbot renew --quiet --renew-hook "/etc/init.d/nginx reload"
      • 매달 3시 15분에 실행
      • 한달마다 주기적으로 진행하도록 하였다.

기타 안되는부분

  • certbot 설정 안되는것
    • sudo certbot certonly --webroot -w /var/www/letsencrypt -d example.com --agree-tos -m ex@example.com
    • certonly는 직접 nginx설정을 수정하는 명령어
    • –webroot는 인증키를 저장하는 위치가 된다.(위에서 만든 폴더로 저장됨)
    • –agree-tos는 처음 인증할때 필요한 이메일 정보를 바로 작성하는 명령어
    • 이걸로 하면 인증이 안됨…

출처

  • https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04
    • 여러 버전에 맞게 설명이 되어있음
  • https://nhj12311.tistory.com/250
  • https://vlee.kr/3440
    • 설치법
  • https://velog.io/@curiosity806/nginx-Lets-Encrypt-SSL-%EC%9D%B8%EC%A6%9D%EC%84%9C-%EB%B0%9C%EA%B8%89
    • nginx설정부분 설명
    • 인증서 자동 갱신