[轉]部署Let’s Encrypt免費SSL證書&&自動續期

最近公司網站要用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申請的證書會有三個月的有效期,能夠到期前手動續約,也能夠本身寫定時腳本任務自動續約。嫌手動麻煩,就寫了個簡單的續期腳本。
 
一、腳本
#/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

 

 
 
二、定時任務
腳本有了,還須要在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]
相關文章
相關標籤/搜索