linux-ubuntu 14.04
tomcat 9
jdk8
(免费证书三个月有效期,可用脚本自动续期)
//git获取letsencrypt
# git clone https://github.com/letsencrypt/letsencrypt
# cd letsencrypt
//运行letsencrypt-auto,运行前最好关闭tomcat服务,以免443端口占用
# ./letsencrypt-auto certonly
运行到 Installing Python packages… 处也许会出现几分钟停顿,停顿时间过长最好更换源,同时 update 和 upgrade。
之后按照提示填写邮箱,域名,网站根目录(邮箱建议国外邮箱)。
看到下面提示,就表示运行成功了:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/ubock.com/fullchain.pem. Your cert will
expire on 2017-05-27. 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"
- 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
运行部署成功后,可以在 /etc/letsencrypt/live/ 下看到域名文件夹,里面是证书。
证书:
cert.pem - 服务器端证书
chain.pem - 根证书和中继证书
fullchain.pem - 需要的 ssl_certificate 文件
privkey.pem - 安全证书 KEY 文件
切换到 tomcat 的 conf 目录下,修改 server.xml
//server.xml
//找到相关配置,去掉注释后修改
<Connector port="443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" URIEncoding="UTF-8">
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="/etc/letsencrypt/live/<你的域名文件夹>/privkey.pem"
certificateFile="/etc/letsencrypt/live/<你的域名文件夹>/cert.pem"
certificateChainFile="/etc/letsencrypt/live/<你的域名文件夹>/chain.pem"
type="RSA" />
SSLHostConfig>
Connector>
//修改 web.xml
//在最后添加下面代码
<security-constraint>
<web-resource-collection >
<web-resource-name >SSLweb-resource-name>
<url-pattern>/*url-pattern>
web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIALtransport-guarantee>
user-data-constraint>
security-constraint>
到这可以重启 tomcat 服务,查看有没有上 https。
如果没有成功,则是还需要安装 ARP环境,tomcat-native,openssl (默认系统自带版本可能过低)
(不太清楚为什么要装这个,这三个彼此依赖。查到的资料nginx都是直接配完xml就生效的,而我tomcat没有起作用。
翻了一些博文,发现有提到tomcat要安装APR环境;tomcat-native 库使tomcat运行时通过APR更多的调用本地API,达到提升性能的目的。native库依赖openssl,jdk;openssl系统自带版本过低可能要升级)
切换到 tomcat 下 bin 目录,解压安装 tomcat-native.tar.gz
(默认bin目录下带有tomcat-native,不需要另下)
# cd ./tomcat-native
# ./configure --with-apr=/usr/local/apr/bin/apr-1-config --with-java-home=/usr/java/jdk1.8 --with-ssl=yes
# make
# make install
同理,安装缺少的 apr 环境和升级 openssl,不另作叙述。
PS:如果没起作用,查看 tomcat/logs 目录下 catalina.out 里的日志
反复关闭启动 tomcat 服务,查看日志中的报错,
根据错误信息谷歌百度StackOverflow
当证书成功获取后,先用下面命令测试是否可以续期,此命令只是测试用,不会更新证书
# certbot renew --dry-run
//嫌开头check部分多余可以加 --no-bootstrap
出现下面的表示成功
-------------------------------------------------------------------------------
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates below have not been saved.)
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/xxxxxxx.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
-------------------------------------------------------------------------------
然后编辑 ReStartReNew.sh 脚本
#!/bin/sh
sh /home/ubuntu/tomcat-9/bin/shutdown.sh
sh /home/ubuntu/letsencrypt/certbot-auto renew --quiet
sh /home/ubuntu/tomcat-9/bin/startup.sh
设置定时
//以root用户运行
# crontab -e
//1-4选择编辑方式
//添加内容
0 3 1 * * /home/ubuntu/letsencrypt/ReStartReNew.sh > /dev/null 2>&1
//定时每月1号3点运行脚本, > /dev/null 2>&1 表示错误和标准输出到空设备(不显示信息)
crontab命令详解