網路活動

網站取得letsencrypt免費SSL憑證教學

一.前言

為因應大眾對個人資料保護要求、及避免資料傳輸過程被有心人士攔截利用,目前各大網站在傳輸敏感性資料時,大都已採用HTTPS的加密連線,以保護資料傳遞的安全。雖然僅供HTTPS加密所使用的憑證,每年所需的費用不高,但對於一般流量不大的網站,長期下來也是一筆負擔。因此,在兼顧資料安全及金錢負擔的狀況下,Let's Encrypt所提供的免費SSL憑證即是一個不錯的選擇。本篇文章,會教你如何手動為自己的網站申請SSL憑證及基本設定。

二.示範說明

  1. 網域名稱: tech.idv.tw
  2. 作業系統: FreeBSD v10.2
  3. 網頁伺服器: Nginx v1.8.1
  1. Let's Encrypt官網: https://letsencrypt.org/
  2. 安裝方法: https://letsencrypt.org/getting-started/
  3. 詳細手冊: https://letsencrypt.readthedocs.org/en/latest/index.html
  1. Qualys SSL Labs: https://www.ssllabs.com/ssltest/
  2. SSL Shopper: https://www.sslshopper.com/ssl-checker.html
  3. SSLBUYER: https://www.sslbuyer.com/
  1. Let's Encrypt所提供的憑證有效期限僅有三個月,使用者可以每兩個月使用SCRIPT自動更新憑證。
  2. Let's Encrypt會限制每個網域名稱(或IP)可以申請的憑證數量,詳細的限制,請參考
    https://community.letsencrypt.org/t/rate-limits-for-lets-encrypt/6769

三.申請Let's Encrypt憑證

  1. 安裝Git指令
    使用port安裝:
    # cd /usr/ports/devel/git/ && make install clean
    使用package安裝:
    # pkg install git
  2. 從GitHub下載Let's Encrypt專案所有的程式碼
    # git clone https://github.com/letsencrypt/letsencrypt
    備註:Git指令會在你目前執行的目錄中,產生一個「letsencrypt」目錄,存放Let's Encrypt專案的所有程式碼
  3. 向Let's Encrypt申請SSL憑證
    # cd letsencrypt
    # ./letsencrypt-auto --debug certonly --webroot --email xxx@xxx.com.tw -w /usr/local/www/tech.idv.tw -d tech.idv.tw -d www.tech.idv.tw
    說明:
    1. 目前在FreeBSD的平台執行letsencrypt-auto指令時,須多加上「--debug」參數
    2. 「--email xxx@xxx.com.tw」請填上你的聯絡信箱
    3. 「-w /usr/local/www/tech.idv.tw」是指定你的網站存放的目錄
    4. 「-d tech.idv.tw」是指你要申請的網域名稱
    5. 「--agree-tos」加上這選項,表示直接同意該服務條款,不會跳出畫面讓你選擇
    6. 「--key-rsa-size 4096」加上這選項,可以指定RSA KEY的長度,預設是2048
    7. 申請憑證時,Let's Encrypt會在網站的目錄下產生一個「.well-known」的目錄並寫入驗證檔案,去驗證申請者是否擁有該網域名稱?因此,須將網站的80、443port開啟,讓Let's Encrypt可以正常連線讀取驗證檔案(驗證完檔案即刪除)
    8. 完成驗證後,會將該網站的憑證檔案放在/etc/letsencrypt/live/tech.idv.tw目錄下(cert.pem及chain.pem是Apache使用;fullchain.pem及privkey.pem是Nginx使用)






四、 設定NGINX及連線測試

  1. 編輯tech.idv.tw網站的設定檔,加入SSL憑證的設定
    # vi /usr/local/etc/nginx/nginx.conf
    server {
    listen 80;
    server_name tech.idv.tw www.tech.idv.tw;
    add_header Strict-Transport-Security "max-age=31536000";  #時間設定為365天,以秒為單位
    return 301 https://$server_name$request_uri;
    }

    server {
    listen 443 ssl;
    server_name tech.idv.tw www.tech.idv.tw;
    add_header Strict-Transport-Security "max-age=31536000";  #時間設定為365天,以秒為單位

            ### 設定憑證的路徑
    ssl_certificate /etc/letsencrypt/live/tech.idv.tw/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/tech.idv.tw/privkey.pem;

            ### 設定支援的協定及Cipher
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDH+AESGCM:EDCH+AES256:ECDH+AES128:!MD5:!aNULL;
    ssl_prefer_server_ciphers on;

            ### 啟用SSL快取
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1h;

            ### 啟用OCSP stapling
    ssl_stapling on;
    ssl_stapling_verify on;

            ... ...
    }
    說明:
    1. 第一個server{}是強制將HTTP的連線轉至HTTPS加密連線
    2. 第二個server{}中設定SSL相關的所有設定,包含憑證位置、支援的協定…等
    3. 加了「add_header Strict-Transport-Security」,之後檢測SSL憑證時,可以獲得「A+」等級。詳細說明,請參考https://developer.mozilla.org/en-US/docs/Web/Security/HTTP_strict_transport_security

  2. 重新啟動NGINX
    # /usr/local/etc/rc.d/nginx restart

  3. 開啟瀏覽器測試SSL連線

    點擊「綠色鎖頭」,即可看到該憑證內容

線上測試SSL憑證

  1. 使用Qualys SSL Labs線上檢測(https://www.ssllabs.com/ssltest/
    輸入你要檢測的網址:


    SSL憑證檢測結果:


  2. 使用SSL Shopper線上檢測(https://www.sslshopper.com/ssl-checker.html


  3. 使用SSLBUYER線上檢測(https://www.sslbuyer.com/
    點選【SSL工具】


    點選【開始使用】


    輸入你要檢測的網域名稱後,按【send】


    顯示檢測的結果

設定每兩個月自動更新憑證

  1. 新增一個SCRIPT檔
    # ~/renew-ssl.sh
    #!/bin/sh
    if ! /root/letsencrypt/letsencrypt-auto certonly --renew-by-default --agree-tos --webroot --email xxx@xxx.com.tw -w /usr/local/www/tech.idv.tw -d tech.idv.tw -d www.tech.idv.tw > /var/log/letsencrypt/renew.log 2>&1 ; then
    echo Automated renewal failed:
    cat /var/log/letsencrypt/renew.log
    exit 1
    fi
    /usr/local/etc/rc.d/nginx reload >> /var/log/letsencrypt/renew.log 2>&1

  2. 修改執行權限
    # chmod u+rwx,go-rwx ~/renew-ssl.sh

  3. 設定每兩個月自動執行一次
    # crontab -e
    30 4 1 */2 * /root/renew-ssl.sh
    備註:設定每兩個月的一號凌晨四點半更新憑證