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

什么是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
#(1)输入邮箱 (Enter 'c' to cancel): 
abc123@foxmail.com
#(2) 注册服务  You must agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
#(3) 输入有生成的证书域名,多个用空格分开 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

 

posted @   大司徒  阅读(5304)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek “源神”启动!「GitHub 热点速览」
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示