环境 Centos7 + jdk1.8 + tomcat9 +nginx
首先需要安装Git 如果有可以忽略这一步
yum install git
通过git获取letsencrypt
git clone https://github.com/certbot/certbot.git
进入letsencrypt目录
cd certbot-master
这里可以通过下面的命令来查看letsencrypt的帮助
./certbot-auto --help
letsencrypt为我们提供了五种生成证书的方式
--apache Use the Apache plugin for authentication & installation //使用Apache插件进行身份验证和安装
--standalone Run a standalone webserver for authentication //运行一个独立的webserver进行身份验证
--nginx Use the Nginx plugin for authentication & installation //使用Nginx插件进行身份验证和安装
--webroot Place files in a server's webroot folder for authentication //将文件放在服务器的webroot文件夹中进行身份验证。
--manual Obtain certificates interactively, or using shell script hooks //交互式地获取证书,或者使用shell脚本钩子
使用方式
certbot-auto [SUBCOMMAND] [options] [-d DOMAIN] [-d DOMAIN]
获取证书
./certbot-auto certonly --standalone --email 你的邮箱 -d 你的域名
多域名配置可以在后面追加 -d 域名 -d 域名
成功之后可以在/etc/letsencrypt/live/[你的域名]/下面的四个文件
cert.pem chain.pem fullchain.pem privkey.pem
在nginx下部署证书
server {
listen 443;
#server_name _;根据实际需求自行配置
ssl on;
#root 根据实际需求自行配置
ssl_certificate /etc/letsencrypt/live/[域名文件夹]/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/[域名文件夹]/privkey.pem;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location /{
}
}
重启nginx
Tomcat9下部署证书
安装相关依赖 如果有请忽略
//安装openssl
# wget http://www.openssl.org/source/openssl-1.1.0e.tar.gz
# tar -zxvf openssl-1.1.0e.tar.gz
# cd openssl-1.1.0e
# ./config --prefix=/usr/local/openssl
# make && make install
//安装apr
# cd
# wget https://archive.apache.org/dist/apr/apr-1.5.2.tar.gz
# tar -zxvf apr-1.5.2.tar.gz
# cd apr-1.5.2
# ./configure --prefix=/usr/local/apr
# make && make install
//安装apr-util
# cd
# wget https://archive.apache.org/dist/apr/apr-util-1.5.4.tar.gz
# tar -zxvf apr-util-1.5.4.tar.gz
# cd apr-util-1.5.4
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install
//安装tomcat-native,在Tomcat的bin目录下自带native安装包
//进入tomcat的bin目录
# cd /tomcat/bin/
# tar -zxvf tomcat-native[你的版本].tar.gz
# cd tomcat-native[你的版本]/native
# ./configure --with-apr=/usr/local/apr --with-java-home=[你的jdk路径]
--with-ssl=/usr/local/openssl
# make && make install
//配置环境变量
# vim /etc/profile
//在末尾添加
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib
//使环境生效
# source /etc/profile
修改tomcat配置
//server.xml
//找到相关配置,去掉注释后修改
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="300" 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
//在</welcome-file-list>后添加下面代码
<security-constraint>
<web-resource-collection >
<web-resource-name >SSL</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
启动tomcat完成配置
关于letsencrypt有效期的问题
letsencrypt有效期为三个月,三个月后可以选择免费续期
./certbot-auto renew