acme.sh
Cygwin
cygwin crontab
tomcat 8.5 ssl doc
tomcat 8.5 config-http
cygpath
acme.sh
Windows平臺下自動生成替換Let's Encrypt證書,比Linux麻煩一些,Linux平臺下有很多免費的工具,Windows下要么收費,要么使用麻煩。
這里選擇使用acme.sh,在Windows平臺,可以通過安裝Cygwin來使用acme.sh。
Cygwin中選擇安裝 curl, nc, cron, cygrunsrv

安裝完成后,以管理員身份運行cygwin,執(zhí)行:
$ cron-config
Do you want to remove or reinstall it (yes/no) yes
Do you want to install the cron daemon as a service? (yes/no) yes
Enter the value of CYGWIN for the daemon: [ ] ntsec
Do you want the cron daemon to run as yourself? (yes/no) no
Do you want to start the cron daemon as a service now? (yes/no) yes
安裝acme.sh
curl https://get.acme.sh | sh
生成證書
./acme.sh --issue -d <domain> --standalone
證書生成后,將證書轉(zhuǎn)成PKCS12格式,方便配置Tomcat
acme.sh --toPkcs -d <domain> --password <password>
配置Tomcat
這里使用Tomcat 8.5.9版本,將生成的pfx文件COPY到Tomcat下的conf目錄,修改server.xml配置文件:
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="UTF-8" redirectPort="443" />
<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol" SSLEnabled="true" secure="true" URIEncoding="UTF-8" useBodyEncodingForURI="true">
<SSLHostConfig certificateVerification="none">
<Certificate certificateKeystoreFile="conf/<domain>.pfx" certificateKeystoreType="PKCS12" certificateKeystorePassword="<password>" certificateKeyAlias="<keyAlias>"/>
</SSLHostConfig>
</Connector>
<Connector port="8009" protocol="AJP/1.3" redirectPort="443" URIEncoding="UTF-8" />
配置中的證書名稱、密碼和別名,替換成你的真實值
查看證書別名:
keytool -list -v -keystore <path><keystore-name>.pfx -storepass <password>

在Cygwin中 通過 cygpath 將windows路徑轉(zhuǎn)成Linux路徑,方便在Cygwin中使用:
$ cygpath D:\ssl -a
/cygdrive/d/ssl
# 復(fù)制證書到D:\ss目錄
$ cp /home/Administrator/.acme.sh/<domain>/<domain>.pfx /cygdrive/d/ssl
自動續(xù)簽證書的問題
Let's Encrypt 證書只有三個月有效期,在Linux上自動替換證書,然后重啟Tomcat比較方便,可以參考 這里。
Windows上怎么搞,用cygwin執(zhí)行定時腳本,問題是如何在cygwin中重啟Tomcat服務(wù)?參考cygwin郵件列表,通過以下方式可以在cygwin中啟動Windows服務(wù):
# 啟動
cygrunsrv -S <serviceName>
# 停止
cygrunsrv -E <serviceName>
# Remove 刪除
cygrunsrv -R <serviceName>
# 查看幫助
cygrunsrv -h
既然cygwin中可以啟動windows服務(wù),那應(yīng)該可以做到自動更新證書,利用hook機制,在renew后重啟Tomcat,如下(注意該腳本未測試,僅供參考):
# 生成證書時,添加renew-hook
acme.sh --issue -d <domain> --standalone --renew-hook "sh renew-tomcat.sh"
renew-tomcat.sh
#/bin/sh
# 將證書轉(zhuǎn)成PKCS12格式
acme.sh --toPkcs -d <domain> --password <password>
# 備份證書
mv /cygdrive/d/ssl/<domain>.pfx /cygdrive/d/ssl/<domain>`date '+%Y-%m-%d'`.pfx
# 復(fù)制到指定目錄
cp /home/Administrator/.acme.sh/<domain>/<domain>.pfx /cygdrive/d/ssl
# 重啟tomcat服務(wù)
cygrunsrv -E Tomcat8
cygrunsrv -S Tomcat8
使用letsencrypt-win-simple可以參考: