通过letsencrypt实现永久免费的SSL证书续订
letsencrypt是一个CA机构,提供免费的SSL证书,通过certbot向letsenctypt申请证书,结合corn定时任务可以实现自动更新证书,并且永久免费
通过snap安装Certbot
如何没有snap,需先安装snap
#安装有snapd安装包的资源仓库
yum install epel-release
#安装snap
yum install snapd
#启动snapd
systemctl start snapd
#创建软链接
ln -s /var/lib/snapd/snap /snap
使用snap安装certbot
#安装certbot
snap install certbot
#创建软连接
ln -s /snap/bin/certbot /usr/bin/certbot
使用cerbot向letsenctypt申请SSL证书
- 使用DNS验证的方式
--manual:表示手动输入 DNS 记录
--preferred-challenges dns:指定使用 DNS 验证方式
-d api.moon.com:需要申请证书的域名
certbot certonly --manual --preferred-challenges dns -d www.xxx.cn
- 使用webroot验证,需要使用到80端口
CA会通过80端口验证webroot下的文件
需要nginx配置反向代理
#certbot自动续签使用的验证路径映射 location ^~ /.well-known/acme-challenge/ { root /root/webroot; }
certbot certonly --webroot -w /root/webroot -d tool.anyi.space
证书的有效期为90天
使用corn定时任务,每两个月就向letsenctypt申请一个新的证书
0 0 15 */2 * certbot renew && nginx -s reload
#!/bin/bash
#仅申请证书,域名是git.anyi.space,验证文件生成路径为(需要CA验证服务器的这个验证文件)/root/webroot
#certbot certonly --webroot -w /root/webroot -d git.anyi.space
#查看当前certbot申请的证书状态
#certbot certificates
#测试续订证书命令能否正常运行
#certbot renew --dry-run
#续订证书,并更新到文件中
echo '续订证书'
certbot renew
echo '拷贝证书'
cat /etc/letsencrypt/live/git.anyi.space/fullchain.pem > /usr/local/nginx/cert/git.anyi.space.pem
echo '拷贝私钥'
cat /etc/letsencrypt/live/git.anyi.space/privkey.pem > /usr/local/nginx/cert/git.anyi.space.key
echo '重新加载nginx'
nginx -s reload