最後更新日期:2017-03-16。
介紹
Let's Encrypt 是由許多大公司以及各大非營利團體為了推廣 HTTPS 而贊助的一家免費發佈 SSL certificate 的 Certiciate Authority。
這篇文章主要是介紹怎麼使用 Let's Encrypt 所提供的服務,會需要知道如何操作 command line interface。
限制
Let's Encrypt 目前已經正式開放,但有發行數量限制,一般人可能會遇到的:
- Names/Certificate:單一 certificate 限制 100 個 hostname。
- Certificates/Domain:每個 domain 每個禮拜最多 20 個 certificate,但 renew 不計算在 quota 內 (需要憑證內的 hostname 與之前完全一樣)。
- Certificates/FQDNset:相同 hostname 的憑證每個禮拜最多發出五個。。
最新的資訊以及完整的細節請參考「Rate Limits - Let's Encrypt - Free SSL/TLS Certificates」這邊的說明。
安裝過程
我們用到的程式需要 curl,所以先安裝 curl:
# echo "for Debian/Ubuntu" # apt-get install curl
# echo "for CentOS/RedHat" # yum install curl
下載最新 release 的 dehydrated 並且解開,目前是 0.4.0:
$ # refer: https://github.com/lukas2511/dehydrated/releases $ curl -LO https://github.com/lukas2511/dehydrated/archive/v0.4.0.tar.gz $ tar -zxv -f v0.4.0.tar.gz $ cd dehydrated-0.4.0/或是透過 Git 下載最新版本:
$ cd ~; git clone https://github.com/lukas2511/dehydrated.git $ cd dehydrated/除了用 Git 下載外,也可以直接只抓執行檔:
$ curl -LO https://raw.githubusercontent.com/lukas2511/dehydrated/master/dehydrated
把程式安裝到
/etc/dehydrated/ 下:# mkdir /etc/dehydrated/ # cp ~/dehydrated/dehydrated /etc/dehydrated/ # chmod a+x /etc/dehydrated/dehydrated
建立 SSL certificate 證驗證過程時所需要的目錄:
# mkdir -p /var/www/dehydrated/
# for Apache Alias /.well-known/acme-challenge/ /var/www/dehydrated/
# for nginx
location /.well-known/acme-challenge/ {
alias /var/www/dehydrated/;
}
第一次需要先同意 Let's Encrypt 的條款:
# /etc/dehydrated/dehydrated --register --accept-terms
第一次產生 SSL certificate,黃色的部份請代換成網域名稱:
# /etc/dehydrated/dehydrated -c -d letsencrypt.tw
成功的話會有類似的輸出:
# INFO: Using main config file /etc/dehydrated/config Processing letsencrypt.tw + Signing domains... + Generating private key... + Generating signing request... + Requesting challenge for letsencrypt.tw... + Responding to challenge for letsencrypt.tw... + Challenge is valid! + Requesting certificate... + Checking certificate... + Done! + Creating fullchain.pem... + Done!
成功後產生的檔案都在
/etc/dehydrated/certs/letsencrypt.tw/ 裡:drwx------ 2 root root 4096 Feb 24 02:25 . drwx------ 3 root root 4096 Feb 24 02:23 .. -rw------- 1 root root 1651 Feb 24 02:25 cert-1456280700.csr -rw------- 1 root root 2143 Feb 24 02:25 cert-1456280700.pem lrwxrwxrwx 1 root root 19 Feb 24 02:25 cert.csr -> cert-1456280700.csr lrwxrwxrwx 1 root root 19 Feb 24 02:25 cert.pem -> cert-1456280700.pem -rw------- 1 root root 1675 Feb 24 02:25 chain-1456280700.pem lrwxrwxrwx 1 root root 20 Feb 24 02:25 chain.pem -> chain-1456280700.pem -rw------- 1 root root 3818 Feb 24 02:25 fullchain-1456280700.pem lrwxrwxrwx 1 root root 24 Feb 24 02:25 fullchain.pem -> fullchain-1456280700.pem -rw------- 1 root root 3243 Feb 24 02:25 privkey-1456280700.pem lrwxrwxrwx 1 root root 22 Feb 24 02:25 privkey.pem -> privkey-1456280700.pem
接著就可以修改 Apache 或是 nginx 的 SSL 設定:
# for Apache SSLCertificateFile /etc/dehydrated/certs/letsencrypt.tw/cert.pem SSLCertificateChainFile /etc/dehydrated/certs/letsencrypt.tw/chain.pem SSLCertificateKeyFile /etc/dehydrated/certs/letsencrypt.tw/privkey.pem
# for nginx ssl_certificate /etc/dehydrated/certs/letsencrypt.tw/fullchain.pem; ssl_certificate_key /etc/dehydrated/certs/letsencrypt.tw/privkey.pem;
然後重新載入 Apache 或是 nginx 的設定檔 (或是直接重新啟動):
# echo "for Apache" # service apache2 reload
# echo "for nginx" # service nginx reload
接下來設定
/etc/cron.d/dehydrated-letsencrypt_tw (因為 /etc/cron.d/ 裡面的檔名不能有 . 這個符號,用 _ 取代),讓 cron 每天自動檢查並更新:# for Apache 0 0 * * * root sleep $(expr $(printf "\%d" "0x$(hostname | md5sum | cut -c 1-8)") \% 86400); ( /etc/dehydrated/dehydrated -c -d letsencrypt.tw; /usr/sbin/service apache2 reload ) > /tmp/dehydrated-letsencrypt.tw.log 2>&1
# for nginx 0 0 * * * root sleep $(expr $(printf "\%d" "0x$(hostname | md5sum | cut -c 1-8)") \% 86400); ( /etc/dehydrated/dehydrated -c -d letsencrypt.tw; /usr/sbin/service nginx reload ) > /tmp/dehydrated-letsencrypt.tw.log 2>&1
規劃
- 選用 dehydrated 而非官方的 certbot 是因為 dehydrated 的需求相當低,只需要有 curl 與 openssl 就可以執行,相較於官方版本需要 Python 會比較簡單。
- 放到
/etc/dehydrated/下的目的是避免之後各作業系統有提供 dehydrated 的套件而衝突到 (套件通常都會把可執行檔放到/usr/bin或是/usr/sbin下),另外一方面 dehydrated 會吃同一個目錄下的config,這對於設定上可以少一些功夫。 - 在 cron job 裡面每天執行是因為 dehydrated 會自己檢查憑證有效期限,如果還有一個月以上的時間有效就不會 renew,所以不需要擔心每天執行會造成 Let's Encrypt 的伺服器產生負擔。
- 在 cron job 中的
sleep $(expr $(printf "\%d" "0x$(hostname | md5sum | cut -c 1-8)") \% 86400)設計是利用機器名稱產生出十六進位 hash 值,抓一部分轉成十進位後除以一天的秒數,得到餘數後先停這個秒數再跑dehydrated,這樣可以避免同時間有太多機器到 Let's Encrypt 的伺服器,造成類似 DDoS 的攻擊。
參考資料
- Let's Encrypt - Free SSL/TLS Certificates (Let's Encrypt 官方網站)
- letsencrypt/acme client implemented as a shell-script (dehydrated 官方網站)
最需要注意的是JkWorkersFile與JkMount,JkWorkersFile是workers.properties的路徑,而JkMount是負責設定URL的對應。以我的例子而言,在Tomcat Server上我有LearningEnglish與LogoutArmyCD兩個應用程式,因此我針對LearningEnglish與LogoutArmyCD兩個名稱做filter,當然你也可以針對附檔名去處理。這兩個AP分別對應到le與lacd,這標記是記錄在workers.properties中。
JkWorkersFile conf/workers.properties JkLogFile /var/log/jk.log JkShmFile /var/log/jk-runtime-status JkLogLevel info JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories JkRequestLogFormat "%w %V %T" JkMount /LearningEnglish le JkMount /LearningEnglish/* le JkMount /LogoutArmyCD lacd JkMount /LogoutArmyCD/* lacd
ssl.conf的部分只需要加入Filter對應即可。當User透過https訪問到LearningEnglish或LogoutArmyCD時,會自動導向至le與lacd所設定的Tomcat Server上。
<VirtualHost *:443> ... JkMount /LearningEnglish le JkMount /LearningEnglish/* le JkMount /LogoutArmyCD lacd JkMount /LogoutArmyCD/* lacd </VirtualHost>
轉成 tomcat 用的 keystone
You do need to add the privkey.pem to the JKS, but I couldn't do so directly with keytool or Portecle and used a workaround instead.
With the help of openSSL you can create a PKCS12 keystore with both your certificate and the private key like so (no password for privkey.pem required):
With the help of openSSL you can create a PKCS12 keystore with both your certificate and the private key like so (no password for privkey.pem required):
openssl pkcs12 -export -in cert.pem -inkey privkey.pem -out cert_and_key.p12 -name tomcat -CAfile chain.pem -caname root
I than converted this PKCS12 to a JKS:
keytool -importkeystore -deststorepass <changeit> -destkeypass <changeit> -destkeystore MyDSKeyStore.jks -srckeystore cert_and_key.p12 -srcstoretype PKCS12 -srcstorepass <thePasswordUsedInTheCommandAbove> -alias tomcat
After that I added the chain.pem (although this might have been avoided with the use of
-chain in the openSSL command, I guess):keytool -import -trustcacerts -alias root -file chain.pem -keystore MyDSKeyStore.jks
The resulting JKS can be used in a Tomcat Connector configuration like so:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="/path/to/MyDSKeyStore.jks" keystorePass="<changeit>" keyAlias="tomcat" keyPass="<changeit>"/>
So this allows you to use your LE certificate with Tomcat =)
Hope this helps.
Hope this helps.