簡述幾個安裝步驟

首先安裝 Certbot Ports 套件,這也是 Let's Encrypt 建議的工具
# cd /usr/ports/security/py-certbot/
# make install clean

這過程會有一點久,因為它要連同一起裝 python

安裝好之後就可以來申請憑證了,首先先確認您的網站根目錄在哪兒,以我的來說是在 /usr/local/www/apache24/data 底下,這路徑等等會用到

申請憑證指令如下 (請自行更換紅字網址名稱)

# certbot certonly --webroot --webroot-path /usr/local/www/apache24/data/ -d www.bojack.com

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): bojack@bojack.com <--- 請輸入您的 Email

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A <--- 同意使用條款

-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: Y <--- 同意分享 Email 給 Electronic Frontier Foundation 加入他們 mailinglist (會另外收到一封 Email)
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.bojack.com
Using the webroot path /usr/local/www/apache24/data for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /usr/local/etc/letsencrypt/live/www.bojack.com/fullchain.pem.
   Your cert will expire on 2017-10-31. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /usr/local/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

好了,這樣就完成憑證申請的作業,上面標藍色的字就是我們存放憑證的位置,同時也告知您憑證有效期限為三個月

接下來要將憑證搬到 Apache 目錄底下

#cp -LRp /usr/local/etc/letsencrypt/live/ /usr/local/etc/apache24/certs

搬完之後就來設定我們 httpd-ssl.conf

vi /usr/local/etc/apache24/httpd.conf

要記得載入 LoadModule ssl_module libexec/apache24/mod_ssl.so

( 我在啟用 HTTPS 曾發生錯誤,後來發現也要載入 LoadModule socache_shmcb_module libexec/apache24/mod_socache_shmcb.so ,這部份就看您啟用時會不會有錯 )

Include etc/apache24/extra/httpd-ssl.conf 前面的 # 記得拿掉,這樣 httpd-ssl.conf 就搞定了

接下來設定 httpd-ssl.conf

vi /usr/local/etc/apache24/extra/httpd-ssl.conf

我將 <VirtualHost _default_:443> 這一行(包含)以下的東西全刪光,替換成以下設定,請記得替換掉紅字的部份喔

<VirtualHost *:443>
ServerName www.bojack.com:443
ServerAdmin bojack@bojack.com
SSLEngine on
SSLCertificateFile "/usr/local/etc/apache24/certs/www.bojack.com/fullchain.pem"
SSLCertificateKeyFile "/usr/local/etc/apache24/certs/www.bojack.com/privkey.pem"
</VirtualHost>

設定好之後就可以啟動 Apache 了,此時連到 https://your_web_site/ 應該就可以看到已經成功啟用加密囉 !

工作還沒完,稍早有提到憑證有效期是三個月,因此我們必須讓它自動去更新憑證,可以寫一個 shell script 定期每週去檢查,請記得替換掉紅字的部份喔

#/bin/sh
/usr/local/bin/certbot renew
if ! diff "/usr/local/etc/letsencrypt/live/www.bojack.com/fullchain.pem" \
"/usr/local/etc/apache24/certs/www.bojack.com/fullchain.pem"  >/dev/null 2>&1; then
        cp -LRp /usr/local/etc/letsencrypt/live/ /usr/local/etc/apache24/certs/
        service apache24 restart
fi

把這個 shell 存起來,舉例我放在 /root/work/check_cert.sh,最後寫進 crontab 就可以讓它每週自行檢查囉

@weekly /root/work/check_certs.sh

參考資料

How to configure Let's Encrypt on FreeBSD with Apache in a jail - https://danm.io/2016/07/08/how-to-configure-letsencrypt-on-freebsd-with-jails.html

創作者介紹
創作者 bojack 的頭像
bojack

有為青年生活札記

bojack 發表在 痞客邦 留言(0) 人氣()