目錄表

申請設定 Let's Encrypt 免費 SSL 憑證(CentOS + Apache/Nginx)

因為 Google Chrome 自 2018/05/01 對於沒有合法的 SSL 網站憑證, 就會出現警告, 所以就出現要買 SSL 網站憑證的議題, 針對個人網站或是非正式的公司網站, 採用 Let's Encrypt 免費網站 SSL 憑證似乎是個不錯的選擇.

申請 Let's Encrypt 與設定 Web Server 程序

  • Ubunut 20.04
  • Alpine 3.16
  • CentOS 8
  • CentOS 7
載入中 ...

Ubunut 20.04

  • 下載 Let's Encrypt certbot 工具

    apt install certbot

  • 如果只要產生網站憑證檔案, 可以再 certbot 後面加上 certonly 的參數 Exp.
    certbot-auto --nginx certonly
  • 這樣執行後, 會在 /etc/letsencrypt/live 目錄內, 產生該網域的憑證相關檔案目錄

透過 ssllabs 來檢測是否正常

apache 相關設定

設定自動更新憑證

手動申請 *.ichiayi.com 的 SSL 網域憑證方式

  1. 執行以下的語法
    /root/lets-encrypt/certbot-auto -d ichiayi.com -d *.ichiayi.com --manual --preferred-challenges dns certonly --server https://acme-v02.api.letsencrypt.org/directory
  2. 當出現以下第二次的 Before continuing, verify the record is deployed. 必須去設定 ichiayi.com 的 DNS
    Please deploy a DNS TXT record under the name
    _acme-challenge.ichiayi.com with the following value:
    
    0sGmQQTfit9lW3okOa4jaYmefNQS4FsF6zZgEtyppKw
    
    Before continuing, verify the record is deployed.
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Press Enter to Continue
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Please deploy a DNS TXT record under the name
    _acme-challenge.ichiayi.com with the following value:
    
    fEsd-DYrN-AB_XVWb-Aa9Yx9OxCofBlFkxNmt7RWiCk
    
    Before continuing, verify the record is deployed.
  3. 將前面兩次出現的 hash 值正確設定到 ichiayi.com 的 DNS TXT Record 內, Exp.
    :
    ;
    $TTL 86400      ; 1 day -- ; 3 hours
    $ORIGIN ichiayi.com.
    @               IN SOA  ns1 server (
                                    2018123100 ; serial
                                    28800       ; refresh (2 hours)
                                    14400        ; retry (15 minutes)
                                    720000     ; expire (1 week)
                                    86400      ; minimum (12 hours)
                                    )
                            NS      ns4.everplast.net.
                            NS      ns7.ichiayi.com.
    ;
    ; lets-encrypt SSL Cert
    _acme-challenge IN TXT  "0sGmQQTfit9lW3okOa4jaYmefNQS4FsF6zZgEtyppKw"
    _acme-challenge IN TXT  "fEsd-DYrN-AB_XVWb-Aa9Yx9OxCofBlFkxNmt7RWiCk"
    ;
    :
  4. 重新啟動 DNS 讓這兩個 TXT 生效, 可透過 nslookup 指定詢問 server 8.8.8.8 來確認
    C:\Users\jonathan>nslookup
    預設伺服器:  UnKnown
    Address:  192.168.1.5
    > server 8.8.8.8
    預設伺服器:  google-public-dns-a.google.com
    Address:  8.8.8.8
    > _acme-challenge.ichiayi.com
    伺服器:  google-public-dns-a.google.com
    Address:  8.8.8.8
    名稱:    _acme-challenge.ichiayi.com
    > set type=txt
    > _acme-challenge.ichiayi.com
    伺服器:  google-public-dns-a.google.com
    Address:  8.8.8.8
    未經授權的回答:
    _acme-challenge.ichiayi.com  text =
            "fEsd-DYrN-AB_XVWb-Aa9Yx9OxCofBlFkxNmt7RWiCk"
    _acme-challenge.ichiayi.com  text =
            "0sGmQQTfit9lW3okOa4jaYmefNQS4FsF6zZgEtyppKw"
  5. 確認可以正確查詢到這兩個新增的 TXT Record 後才可執行下一步, 當出現以下訊息就表示正確產生
    :
    Waiting for verification...
    Cleaning up challenges
    
    IMPORTANT NOTES:
     - Congratulations! Your certificate and chain have been saved at:
       /etc/letsencrypt/live/ichiayi.com/fullchain.pem
       Your key file has been saved at:
       /etc/letsencrypt/live/ichiayi.com/privkey.pem
       Your cert will expire on 2019-03-31. To obtain a new or tweaked
       version of this certificate in the future, simply run certbot-auto
       again. To non-interactively renew *all* of your certificates, run
       "certbot-auto renew"
     - Your account credentials have been saved in your Certbot
       configuration directory at /etc/letsencrypt. You should make a
       secure backup of this folder now. This configuration directory will
       also contain certificates and private keys obtained by Certbot so
       making regular backups of this folder is ideal.
     - 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

相關重點檔案

透過 CloudFlare DNS 自動更新無 Web 對外網站 SSL 憑證

  • Ubuntu 20.04
  • CentOS 8
  • CentOS 7
  • CentOS 6
載入中 ...

Ubuntu 20.04

  • 安裝 DNS CloudFlare Plugin
    apt install python3-certbot-dns-cloudflare
  • 建立 /root/lets-encrypt/cloudflare.ini
    mkdir -p /root/lets-encrypt/

    Exp:

    # Cloudflare API credentials used by Certbot
    dns_cloudflare_email = [email protected]
    dns_cloudflare_api_key = 0123456789abcdef0123456789abcdef01234567
  • 設定保護權限
    chmod 600 /root/lets-encrypt/cloudflare.ini
  • 進行申請新憑證 Exp. example.com
    /usr/bin/certbot certonly \
      --dns-cloudflare \
      --dns-cloudflare-credentials /root/lets-encrypt/cloudflare.ini \
      --dns-cloudflare-propagation-seconds 10 \
      -d example.com
  • 進行定期更新憑證 Exp. example.com
    /usr/bin/certbot renew \
      --dns-cloudflare \
      --dns-cloudflare-credentials /root/lets-encrypt/cloudflare.ini \
      --dns-cloudflare-propagation-seconds 10
  • 設定每天自動檢查更新
    1. 建立 /root/lets-encrypt/renewcert.sh
      vi /root/lets-encrypt/renewcert.sh
      /usr/bin/certbot renew \
        --dns-cloudflare \
        --dns-cloudflare-credentials /root/lets-encrypt/cloudflare.ini \
        --dns-cloudflare-propagation-seconds 10
      chmod a+x /root/lets-encrypt/renewcert.sh
    2. 設定 /etc/crontab
      vi /etc/crontab
      :
      # let's encrypt
      35 2 * * * root /root/lets-encrypt/renewcert.sh > /tmp/renewcert.log
      systemctl restart crond

參考網址

1)
只產生一個憑證檔案, 但該憑證檔案內有包含多個網址