问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

免费永久HTTPS(ssl)证书——Let's Encrypt来了

创作时间:
作者:
@小白创作中心

免费永久HTTPS(ssl)证书——Let's Encrypt来了

引用
1
来源
1.
https://www.cnblogs.com/chuanghongmeng/p/18466820

Let's Encrypt是一个免费的SSL证书服务,可以为网站提供永久的HTTPS支持。本文将详细介绍如何使用Let's Encrypt和Certbot工具来配置和管理HTTPS证书,包括证书的生成、Nginx配置以及自动更新等步骤。

什么是Let's Encrypt

Let's Encrypt是一个非盈利的,免费的CA,可以提供免费HTTPS认证服务。 提供了一套完整的工具,基于这套工具,我们可以免费来搭建HTTPS网站。 详情可参见它的官网:
https://letsencrypt.org/

为什么使用Let’s Encrypt

国内有许多机构可以提供免费的SSL证书,但是一般只有一年的免费服务。Let’s Encrypt可以基于cron可以实现定时更新证书,从而实现永久免费的目的。

如何使用Let's Encrypt配置HTTPS证书

查阅Let’s Encrypt官网可以发现我们应该安装Certbot工具来配置HTTPS证书。
介绍certbot安装方式的网站如下:
certbot.eff.org/instruction…
它的文档还是非常人性化的,选择好系统后,会自动展示对应系统的安装文档,这里我选的系统是:Nginx + CentOS 7

安装

#安装snapd软件
sudo yum install snapd
sudo systemctl start snapd
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap
#使用snapd安装certbot
snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot

方式一:使用certbot生成证书(默认文件的方式验证,需要80端口能够访问)

sudo certbot certonly --nginx

输入邮箱:

(Enter 'c' to cancel): abc123@foxmail.com

注册服务:

You must agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y

输入生成的证书域名:

Please enter the domain name(s) you would like on your certificate (comma and/or space separated) 
(Enter 'c' to cancel): www.xxx.cn

生成成功后命令行会显示证书文件在:

/etc/letsencrypt/live/www.xxx.cn/fullchain.pem;
/etc/letsencrypt/live/www.xxx.cn/privkey.pem;

方式二:使用dns的方式验证(用于80端口不可用的情况,需要在域名解析处添加txt解析验证)

sudo certbot certonly --manual --preferred-challenges dns -d www.xxx.cn

参数说明:

--manual:表示手动输入 DNS 记录
--preferred-challenges dns:指定使用 DNS 验证方式
-d api.moon.com:需要申请证书的域名

执行命令:

[root@hecs-90770 ~]# certbot certonly --manual --preferred-challenges dns -d www.xxx.cn
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for www.xxx.cn
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/www.xxx.cn/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/www.xxx.cn/privkey.pem
This certificate expires on 2025-01-22.
These files will be updated when the certificate renews.

卸载

#yum安装,使用下面命令进行移除
sudo yum remove certbot  

配置nginx

server {
    listen       443 ssl;
    server_name  www.xxx.cn;
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers X-Requested-With;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
    ssl_certificate      /etc/letsencrypt/live/www.xxx.cn/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/www.xxx.cn/privkey.pem;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    
    root         /usr/share/nginx/html;
}

更新证书

#测试续期
sudo certbot renew --dry-run
#为了尽量确保证书不失效,我们配置一下定时任务即可更新证书并重启nginx。
0 0 * 1 * sudo certbot renew && nginx -s reload

使用certbot的renew命令续签之前申请的通配符域名时遇到了一个报错,本文记录该问题的解决方法。

使用的命令如下,由于certbot判定我的这个证书还没有接近到期,所以使用--force-renew参数来强制续签证书。

certbot renew --force-renew

报错:

The error was: PluginError('An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.',). Skipping.

可以看到,问题的原因是:

An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.

也就是需要使用--manual-auth-hook参数来指定一个脚本来续签证书。

问题原因

出现这个问题的原因是,第一次申请证书的时候使用的是DNS验证方式,而续签时需要使用脚本来更新DNS记录,如果你想使用脚本来更新的话,可以从github中搜寻相关自动DNS验证脚本,本文将使用另一种方式解决。

问题解决

可以使用standalone命令来解决续签问题,使用该方法的前提是,本机的certbot目录中已存在之前申请过的证书相关资料

certbot certonly --standalone  

添加新的证书

sudo certbot certonly --nginx --domains xxx.xxxxx.com

查询证书有效期:

openssl x509 -in /etc/letsencrypt/live/www.xx.cn/fullchain.pem -noout -dates

取消证书

可以使用以下命令取消刚刚生成的密匙,也就是以上的反操作:

$ certbot revoke --cert-path /etc/letsencrypt/live/xyl.anthb.cn/cert.pem
$ certbot delete --cert-name xyl.anthb.cn  
© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号