CentOS 主機需安裝 Certbot

yum -y install certbot

安裝完成後執行 (下面是一行指令, 請勿拆開) (your@email.address 及 *.yourdmain 請自行替換

certbot certonly --manual --preferred-challenges=dns --email your@email.address --server https://acme-v02.api.letsencrypt.org/directory --agree-tos -d *.yourdomain

執行後會出現一段訊息如下:

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Please deploy a DNS TXT record under the name
    _acme-challenge.yourdomain with the following value:

    koIg48ThCRwFZ7yhYCNQyEqdW5WkoqzzF7DzAnEv40C

    Before continuing, verify the record is deployed.
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

其中 koIg48ThCRwFZ7yhYCNQyEqdW5WkoqzzF7DzAnEv40C

隨機產生, 將此段資料放到 DNS Server 的 _acme-challenge.yourdomain 文字 (TXT) 紀錄裡. 主要用來驗證憑證用.

用 certbot certificates 指令查詢簽發的憑證。可以看到憑證位置如下:

Certificate Path: /etc/letsencrypt/live/yourdomain/fullchain.pem
Private Key Path: /etc/letsencrypt/live/yourdomain/privkey.pem

一共會產出四個憑證相關檔案,將他們填入 Apache vhost 設定中。 vmhost 範例如下:

    <VirtualHost *:443>
    DocumentRoot "/var/www/html"
    ServerName yourdomain

    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1

    # 20200731 使用 certbot 自動產生
    SSLCertificateFile /etc/letsencrypt/live/yourdomain/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/yourdomain/chain.pem
    SSLCACertificateFile /etc/letsencrypt/live/yourdomain/fullchain.pem

    SSLHonorCipherOrder on
    SSLCipherSuite ALL:+HIGH:!ADH:!EXP:!SSLv2:!SSLv3:!MEDIUM:!LOW:!NULL:!aNULL

    <Directory "/var/www/html">
        SetOutputFilter DEFLATE
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        DirectoryIndex index.html index.php
    </Directory>
    </VirtualHost>

存檔案後,重新啟動 Apache。 (其餘domain放置於 Apache 下的網站一樣畫葫蘆)

設定自動 renew SSL 憑證
Let’s Encrypt 憑證目前的有效期只有 3 個月,在憑證到期前 1 個月可以 renew以下設定自動 renew 憑證,以下是更新憑證的指令:

certbot renew --quiet --agree-tos --post-hook "systemctl reload httpd"

以上指令會自動更新所有 SSL 憑證,如果成功更新,會執行 “systemctl reload httpd” 這條指令,讓 apache 重新載入新憑證。
為了日後方便管理,建立一個 renew SSL 的 Shell Script,加入以下內容:

    #!/bin/sh
    /usr/bin/certbot renew --quiet --agree-tos --post-hook "systemctl reload httpd"

然後將然後把上面 Shell Script 加入可執行權限及放到 crontab中讓它定期執行執行,這樣 certbot 便會自動檢查及更新憑證。

將 pem 憑證檔轉換成 pfx 憑證給 Windows IIS 使用,在 /etc/letsencrypt/live/yourdomain/ 目錄下,執行下面指令:
openssl pkcs12 -export -in cert.pem -inkey privkey.pem -out foriis.pfx -certfile chain.pem

將產生出來的 foriis.pfx 檔案複製到 Windows Server 中並將其導入 IIS 中即可。

最後修改日期: 2020 年 9 月 22 日