使用Let's Encrypt生成免费SSL证书操作记录

最近要做微信小程序,要求接口必须备案且是https,个人小站就直接准备使用免费的SSL证书,网上搜了一圈,发现Let's Encrypt是浏览器支持比较好的。

流程:

1. 首先去服务器上安装了Let's Encrypt的生成工具,我系统是云服务器CENTOS7,在github clone了源码,无需安装。

2. 进入到clone下来的代码文件夹,看看cerbot-auto这个文件有没有执行权限,如果没有,chmod +x ./cerbot-auto

3.可以开始正式生成了,如果是第一次申请,中间应该会有email的注册流程,一路yes即可,另外需要用root执行,一般有3种生成方式,

 (1)--standalone 这种需要使用80端口,需要停止网站服务,不太方便

  例:

./certbot-auto certonly --standalone --email [email protected] -d example.com -d www.example.com -d other.example.net

 (2)--webroot 这种比较方便只会往网站根目录生成随机认证文件,然后会验证,这种方式比较常用,不过有多少子域名就得写多少个,不支持通配符

  例

./certbot-auto certonly --webroot -w /var/www/example --email [email protected] -d example.com -d www.example.com -d other.example.net

   (3) 支持通配符的方法。

./certbot-auto certonly --email [email protected]  -d *.example.com -d example.com --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory 

 这种方法是通过DNS验证,一定要看清楚再下一步,其中有一步是给了你一个随机字符串,需要添加到域名解析的TXT记录里,这一步必须加好了且测试已经生效才能回车,测试方法,新开一个shell执行一下:dig -t txt _acme-challenge.example.com @8.8.8.8,如果在返回信息里包含了正确的随机字符串,则说明已经生效,这时回车即可

4. 证书生成好会放在/etc/letsencrypt/live里,以域名区分不同的证书,像nginx这些直接配置一下就可使用

ssl_certificate /etc/letsencrypt/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/chain.pem;

5. docker中使用的话,建议容器启动时分配一个volume,然后在宿主机上手动复制到这个volume,容器中就可以直接使用,这里注意不能使用软链,因为容器启动后软链的地址会变成容器自己的/etc/letsencrypt,就会找不到文件

6. 其他地方使用,像有些服务商的虚拟机,不支持pem后缀的,就必须转换成.crt和.key

openssl x509  -in fullchain.pem -out fullchain.crt
openssl rsa -in privkey.pem -out privkey.key

把转换后的文件传到对应位置即可

7.续期,一般直接使用./cerbot-auto renew即可,可写个脚本,设置成每60天执行一次

#!/usr/bin bash
cd /data/certbot
./certbot-auto renew
/usr/bin/cp -f /etc/letsencrypt/live/example.com/* /data/nginxconf/
# 每隔60天执行一次
0 4 */60 * * sh /root/certbot-renew.sh >> /var/log/certbot-renew.log

8.如果重复执行生成命令会生成example.com-001等带后缀的域名文件夹,新的证书就放在这里面,这样会对脚本使用产生问题,就需要删除历史不用的证书

rm -rf /etc/letsencrypt/archive/example.com/

rm -rf /etc/letsencrypt/live/example.com/

rm -rf /etc/letsencrypt/renewal/example.com.conf

rm -rf /etc/letsencrypt/archive/example.com-001/

rm -rf /etc/letsencrypt/live/example.com-001/

rm -rf /etc/letsencrypt/renewal/example.com-001.conf

必须删除这3个路径下的文件,不然继续执行还会生成-002文件夹,删除完成执行,重新执行生成命令,就会看到生成新的证书到正确的目录了,,后面就最好直接用renew来续期

猜你喜欢

转载自www.cnblogs.com/oliverCJ/p/10612364.html