最近公司網站要用https,從自己摸索到找到國內的免費證書到選購正式的收費證書,最后老板說:太貴!不要。一臉懵逼的聽老板提到Let’s Encrypt證書,沒辦法,用唄。之前是有一些了解,國外發布的一款純免費證書,只是覺得天下沒有免費的午餐,免費和收費的差距肯定還是有的。於是,硬着頭皮開始鼓搗Let’s Encrypt證書。
前言
Let's Encrypt 作為新的證書頒發機構,免費,自動,開放,這三點對於個人用戶來說尤為重要。免費自不用說,自動更是免去了很多過程和麻煩。官網目前推薦的獲取和安裝方式是 certbot ,只需要簡單運行一些命令並作一些配置即可。
前提
1.需要有域名,它會生成指定域名的證書。(填IP會報錯不支持的)
1.需要在域名指向的服務器上能訪問https。(不然會報找不到443端口的錯誤)
2.需要linux環境。
獲取Let's Encrypt
##獲取
wget https://dl.eff.org/certbot-auto
##設置為可執行
chmod a+x certbot-auto
執行自動部署
./certbot-auto
部署過程中會下載一大堆的依賴包,不需要緊張。如果需要確認,(輸入"y")確認就好了
這里注意下,在執行過程中,會卡在'Installing Python packages',等待一段時間下載Phthon lib
如果等待很長時間都不動, 等不及了就修改pip下載源。 vi ~/.pip/pip.conf #如果文件不存在則創建 mkdir ~/.pip vi pip.conf 在里面修改或寫入下面內容 [global] index-url = http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com 重新執行自動部署 ./certbot-auto 重新部署依然會在'Installing Python packages'卡一會兒,但不會很久,五分鍾以內吧。
依賴包下載完后會進入交互界面,依次是: 填寫郵箱、域名(多域名用空格或逗號隔開)、條款確認、驗證域名所有權(這一步可以選擇第一種方式,手動選擇tomcat的root目錄)
執行成功后會顯示
Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/xxx.com/fullchain.pem. Your cert will
expire on 2016-10-05. 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"
此時的生成的證書文件(.pem)都已經在如下路徑了
/etc/letsencrypt/
#主要用到的文件(.pem)在
/etc/letsencrypt/live/xxxxx(域名)/
Let's Encrypt申請的證書會有三個月的有效期,可以到期前手動續約,也可以自己寫定時腳本任務自動續約。嫌手動麻煩,就寫了個簡單的續期腳本。
1、腳本
#/bin/sh #續期 說明:只用renew的話,會先檢查證書是否需要更新,大概是距離到期還有三天或者十幾天之內才會執行更新,否則會提示不需要更新。(昨天更新了證書,今天直接用renew,提示不允許更新) #這里方便測試,增加參數--force-renew,能夠強制立即更新(但好像也會有檢查,時間會變短,比如我剛才更新過了,馬上再次執行會報錯並提示不需要更新)。 ./certbot-auto renew --force-renew #生成p12 cd /mnt/web/letsTemp && openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out fullchain_and_key.p12 -name tomcat -passin passyourPKCS12pass -passout pass:yourPKCS12pass #移動新生成的證書文件 cp /etc/letsencrypt/live/yourDomain/fullchain.pem /mnt/web/letsTemp cp /etc/letsencrypt/live/yourDomain/privkey.pem /mnt/web/letsTemp #生成jks文件 #備份並刪除原jks文件 mv /mnt/web/letsTemp/MyDSKeyStore.jks /mnt/web/letsTemp/MyDSKeyStore`date '+%Y-%m-%d'`.jks cd /mnt/web/letsTemp && keytool -importkeystore -deststorepass yourKeyPass -destkeypass yourKeyPass -destkeystore MyDSKeyStore.jks -srckeystore fullchain_and_key.p12 -srcstoretype PKCS12 -srcstorepass yourPKCS12pass -alias tomcat #重啟服務器 /mnt/web/tomcat/tomcat8/bin/restartup.sh
2、定時任務
腳本有了,還需要在linux中添加一個定期執行腳本的任務,這里用linux自帶的cron來處理這部分。
crontab -e
在打開的編輯器中添加如下內容(每個月1號凌晨3點更新)
0 0 3 * * sh /mnt/web/lets/ssl_auto_auth.sh >/dev/null 2>&1 &
最后
感謝下面兩個博文作者提供的幫助。
http://ju.outofmemory.cn/entry/269602
https://melo.myds.me/wordpress/lets-encrypt-for-tomcat-7-on-ds/
https://github.com/letsencrypt/boulder/issues/1517
[from:http://www.linuxdiyf.com/linux/24147.html]
