使用Certbot工具从Let’s Encrypt获取免费SSL证书

一、简介

Let’s Encrypt 官方文档:https://letsencrypt.org/zh-cn/

为了在您的网站上启用 HTTPS,您需要从证书颁发机构(CA)获取证书(一种文件)。Let’s Encrypt 是一个证书颁发机构(CA)。

限制:

  • 每个注册域名的证书数量(每周 50 张)
  • 一张证书中最多包含 100 个域名
  • 证书有效期为 90 天,到期手动续费

Certbot 官方文档:https://certbot.eff.org/docs/

Certbot是一种免费的开放源代码软件工具,可用于在手动管理的网站上自动使用Let’s Encrypt证书来启用HTTPS。Certbot也是Let’s Encrypt官方推荐使用的一个客户端。

二、Certbot使用

2.1 安装snapd

Certbot 官方强制让你使用 snap 安装,否则用不了。会提示以下内容:

Your system is not supported by certbot-auto anymore.
Certbot cannot be installed.

yum 安装 snapd

yum install -y epel-release
yum install -y snapd
systemctl start snapd.service && systemctl enable snapd.service

需要创建一个软连接 /snap/var/lib/snapd/snap,安装软件需要在 /snap中。

ln -s /var/lib/snapd/snap /snap

做到这里,Snapd已经安装完成了,查看:

[root@iZ8vb9g3s6akav8m1bmbuoZ snap]# snap list
Name     Version   Rev    Tracking       Publisher     Notes
core20   20201210  904    latest/stable  canonical✓    base
snapd    2.48.2    10707  latest/stable  canonical✓    snapd

2.2 安装Certbot

cd /snap
snap install --classic certbot

查看Certbot版本:

[root@iZ8vb9g3s6akav8m1bmbuoZ snap]# snap list 
Name     Version   Rev    Tracking       Publisher     Notes
certbot  1.11.0    889    latest/stable  certbot-eff✓  classic
core20   20201210  904    latest/stable  canonical✓    base
snapd    2.48.2    10707  latest/stable  canonical✓    snapd

2.3 证书获取

先将域名解析到Certbot服务器上。

#--webroot 参数:指定使用临时目录的方式
## -w 参数:指定后面-d 域名所在的根目录, 如果一次申请多个域的, 可以附加更多 -w...-d... 这段.
/snap/bin/certbot certonly --webroot --email xxxxx@qq.com -w /usr/share/nginx/html -d blog.liuli.host
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Requesting a certificate for blog.liuli.host
Performing the following challenges:
http-01 challenge for blog.liuli.host
Using the webroot path /usr/share/nginx/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges
Subscribe to the EFF mailing list (email: xxxxxxxxx@qq.com).
We were unable to subscribe you the EFF mailing list because your e-mail address appears to be invalid. You can try again later by visiting https://act.eff.org.

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/blog.liuli.host/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/blog.liuli.host/privkey.pem
   Your certificate will expire on 2021-04-07. To obtain a new or
   tweaked version of this certificate in the future, simply run
   certbot again. To non-interactively renew *all* of your
   certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

完成上面的操作即可获得 SSL 证书, 保存在 /etc/letsencrypt/live/根域名/ 目录下会有四个文件

  • cert.pem : Apache服务器端证书
  • chain.pem :Apache根证书和中继证书
  • fullchain.pem : Nginx所需要ssl_certificate文件
  • privkey.pem : 安全证书KEY文件

如有报错,如:

Couldn't download https://raw.githubusercontent.com/certbot/certbot/v1.6.0/letsencrypt-auto-source/letsencrypt-auto. <urlopen error [Errno 111] Connection refused>

该台阿里云ECS上解析不到该域名,ping 一下获取该域名IP,添加本地域名解析,或升级到最新版的客户端。
1.5.0版本:https://download.csdn.net/download/qq_39680564/12645820
1.6.0版本:https://download.csdn.net/download/qq_39680564/12645817

vim /etc/hosts
51.101.108.133 raw.githubusercontent.com

配置nginx服务器,

#设置非安全连接永久跳转到安全连接
server{
    listen 80;
    server_name www.liuli.host;
    #告诉浏览器有效期内只准用 https 访问
    add_header Strict-Transport-Security max-age=15768000;
    #永久重定向到 https 站点
    return 301 https://$server_name$request_uri;
}
server {
   #启用 https, 使用 http/2 协议, nginx 1.9.11 启用 http/2 会有bug, 已在 1.9.12 版本中修复.
   listen 443 ssl http2;
   server_name www.liuli.host;
   #首页
   index  index.php index.html index.htm;
   #网站根目录
   root   /usr/share/nginx/html;
   #告诉浏览器当前页面禁止被frame
   add_header X-Frame-Options DENY;
   #告诉浏览器不要猜测mime类型
   add_header X-Content-Type-Options nosniff;

   #证书路径
   ssl_certificate /etc/letsencrypt/live/www.liuli.host/fullchain.pem;
   #私钥路径
   ssl_certificate_key /etc/letsencrypt/live/www.liuli.host/privkey.pem;
   #安全链接可选的加密协议
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   #可选的加密算法,顺序很重要,越靠前的优先级越高.
   ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
   #在 SSLv3 或 TLSv1 握手过程一般使用客户端的首选算法,如果启用下面的配置,则会使用服务器端的首选算法.
   ssl_prefer_server_ciphers on;
   #储存SSL会话的缓存类型和大小
   ssl_session_cache shared:SSL:10m;
   #缓存有效期
   ssl_session_timeout 60m;

    location / {
        try_files $uri $uri/ /index.php?$args;  #修改内容
    }
}

2.4 续期证书

证书的有效期是3个月,你可以在证书过期前的30天内,进行续期,也可以进行脚本自动续期。

#!/usr/bin/env bash
# Auth: liuli
# Version: v1.0, 2021/1/7
# Sys: CentOS 7.9
# Features: 更新域名证书有效期
# Prepare:系统内已安装certbot客户端

if ! /snap/bin/certbot renew > /var/log/letsencrypt/renew.log 2>&1 ; then
    echo Automated renewal failed:
    cat /var/log/letsencrypt/renew.log
    exit 1
fi

# 需要重启nginx证书才能生效
systemctl restart nginx

添加定时任务,每月28号23点执行脚本

0 23 28 * * /opt/shell/ssl_renew.sh

三、卸载

删除 certbot 服务

snap remove certbot

关闭 snap 服务

systemctl stop snapd.service && systemctl disable snapd.service
systemctl stop snapd.socke t&& systemctl disable snapd.socket

卸载 snapd 服务

yum remove -y snapd
rm -rf /etc/letsencrypt/
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
let's encrypt是一个非盈利性质的机构,致力于提供免费SSL证书服务,以促进整个互联网的安全性。在官网上,他们提供了一键式免费申请SSL证书的脚本,使得用户可以快速而简便地获取自己网站的SSL证书。 这个一键式免费申请SSL证书脚本的使用非常简单。首先,用户需要在自己的网站服务器上安装并配置好Certbot工具Certbot是一个由let's encrypt官方开发的自动化工具,用于申请和更新SSL证书。 在安装和配置好Certbot之后,用户只需要在命令行中输入一条简单的指令,就可以申请自己网站的SSL证书了。具体指令如下: $ sudo certbot certonly --standalone -d <your_domain> 其中,“<your_domain>”替换为用户自己的域名。这条指令的作用是告诉Certbot以standalone模式运行,并申请一个新的证书,该证书将与用户输入的域名关联。 Certbot会自动与let's encrypt的服务器进行通信,验证用户所拥有的域名和服务器的控制权。一旦验证通过,Certbot就会生成一个有效期为90天的SSL证书,并将其保存在用户指定的位置。 用户可以根据自己的需要选择将证书文件保存到哪个目录,以及将其用于哪个Web服务器(例如Apache或Nginx)。 值得一提的是,这个一键式免费申请SSL证书脚本还支持自动续订证书的功能。Certbot会在证书即将过期之前自动执行更新过程,以确保用户的网站能够持续使用最新的SSL证书。 总之,let's encrypt官网提供的一键式免费申请SSL证书脚本极大地简化了证书申请和管理的过程,让网站拥有更安全可靠的加密通信。用户只需几个简单的步骤,就能轻松获得免费SSL证书,提升网站的安全性和可信度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值