Certbot实现HTTPS证书自动化管理

引言

在当今互联网环境中,网站的安全性越来越受到重视。HTTPS 作为一种能够提供加密通信和验证网站真实性的协议,已经成为网站安全的基本标准。然而,许多平台提供的 HTTPS 证书往往有有效期限制,比如腾讯云等平台目前提供的证书只有 3 个月的有效期。这不仅增加了证书管理的复杂性,还可能因为证书过期而导致网站访问中断。

为了解决这个问题,我们可以使用Certbot这个强大的工具来自动申请和管理 HTTPS 证书。Certbot是一个开源软件,它可以通过自动化的方式从Let’s Encrypt等证书颁发机构(CA)获取和安装 HTTPS 证书。更重要的是,这些证书是免费的,并且可以通过自动续订来保持长期有效。Certbot支持多种服务器和平台,包括ApacheNginx等。

为什么选择 Certbot?

  1. 自动化:Certbot 可以自动申请、安装和更新证书,无需手动干预。
  2. 免费:Let’s Encrypt 提供的证书完全免费。
  3. 安全:使用 HTTPS 加密网站流量,保护用户数据安全。
  4. 兼容性:广泛支持各种操作系统和 Web 服务器。
  5. 社区支持:拥有活跃的社区和丰富的文档资源。

如何使用 Certbot 申请 HTTPS 证书?

首先,你需要在服务器上安装 Certbot。具体安装方法取决于你使用的操作系统和 Web 服务器软件。你可以访问 Certbot 的官方网站 https://certbot.eff.org/ 获取详细的安装指南。

步骤 1:安装 snap

官方推荐使用 snap 客户端安装 cerbot 客户端,对于CentOS系统,可以使用以下命令:



yum -y install epel-release           #安装依赖
yum -y install snapd     #安装 snap
systemctl enable --now snapd.socket  #启动 snap 通信套接字
ln -s /var/lib/snapd/snap /snap   #创建如下软连接,为了启用 classic snap 的支持

如果少一步,安装 cerbot 会报错。如:没有创建软连接,在安装 cerbob 报以下错误;error: cannot install “certbot”: classic confinement requires snaps under /snap or symlink from /snap to /var/lib/snapd/snap

步骤 2:安装 Certbot


# 如果之前装过 certbot 的话要先卸载
sudo yum remove certbot

# 安装 Certbo
snap install --classic certbot

# 配置 Certbot 命令行,执行如下命令以确保 Certbot 命令行可用
ln -s /snap/bin/certbot /usr/bin/certbot

注意:若出现 error: cannot communicate with server: Post http://localhost/v2/snaps/snap-store: dial un,尝试重启 snap


systemctl restart snapd.service

步骤 3:选择插件

Certbot 支持多种 Web 服务器插件。例如,如果您使用的是 Nginx,可以使用 nginx 插件(二选一):

  • 运行此命令获取证书,并让 Certbot 自动编辑 Nginx 配置以提供服务,只需一步即可打开 HTTPS 访问:
    
    certbot --nginx
    

    注:Certbot 默认 nginx 配置文件在 /etc/nginx/nginx.conf 或 /usr/local/etc/nginx/nginx.conf,若你的 nginx 配置文件不在此处(以/usr/local/nginx/conf/nginx.conf 为例),需在命令后加上 –nginx-server-root /usr/local/nginx/conf.
    若出现 The nginx plugin is not working; there may be problems with your existing configuration.,说明 Nginx 未安装,如已安装使用下面命令加入环境变量。由于没有将 nginx 放到环境变量中,设置 nginx 软连接

    
    ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
    ln -s /usr/local/nginx/conf/ /etc/nginx
    
  • 仅获得证书。如果你希望手动配置 nginx,输入如下命令(推荐):
    
    certbot certonly --nginx --nginx-ctl /application/nginx-1.24/sbin/nginx --nginx-server-root /application/nginx-1.24/conf/
    
    Saving debug log to /var/log/letsencrypt/letsencrypt.log
    Enter email address (used for urgent renewal and security notices)
     (Enter 'c' to cancel): [这里输入你的邮箱]
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Please read the Terms of Service at
    https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
    agree in order to register with the ACME server. Do you agree?
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    (Y)es/(N)o: Y
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Would you be willing, once your first certificate is successfully issued, to
    share your email address with the Electronic Frontier Foundation, a founding
    partner of the Let's Encrypt project and the non-profit organization that
    develops Certbot? We'd like to send you email about our work encrypting the web,
    EFF news, campaigns, and ways to support digital freedom.
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    (Y)es/(N)o: Y [选 Y 继续]
    Account registered.
    
    Which names would you like to activate HTTPS for?
    We recommend selecting either all domains, or all domains in a VirtualHost/server block.
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    1: chat.boysec.cn
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Select the appropriate numbers separated by commas and/or spaces, or leave input
    blank to select all options shown (Enter 'c' to cancel):  [这里不需要输入,回车选所有]
    Requesting a certificate for chat.boysec.cn
    
    Successfully received certificate.
    Certificate is saved at: 
    # [这里告诉我们生成的文件路径和有效期]
    /etc/letsencrypt/live/chat.boysec.cn/fullchain.pem
    Key is saved at:         /etc/letsencrypt/live/chat.boysec.cn/privkey.pem
    This certificate expires on 2024-09-25.
    These files will be updated when the certificate renews.
    Certbot has set up a scheduled task to automatically renew this certificate in the background.
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    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
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    

步骤 4:修改 Nginx 配置


server {
    listen       80;
    server_name  chat.boysec.com;
    return 302 https://chat.boysec.com;
}
server {
    #监听 443 端口
    listen 443 ssl;
    #对应的域名,空格分隔域名就可以了
    server_name   chat.boysec.com;
    #证书相关配置
    ssl_ciphers           HIGH:!aHULL:!MD5:!DES:!DES3;
    ssl_certificate       /etc/letsencrypt/live/chat.boysec.com/fullchain.pem;
    ssl_certificate_key   /etc/letsencrypt/live/chat.boysec.com/privkey.pem;
    ssl_session_cache     shared:SSL:10m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ......
}

查看网站签发证书:

Certbot 实现 HTTPS 证书自动化管理
配置自动续签

虽然是免费的,但 certbot 只有 90 天的有效期,但它有自动续期的命令,无需再次运行 certbot 重新签证书。通过运行以下命令,可以测试证书的续订是否正常。


certbot renew --dry-run

如果没有报错,则自动续期命令是正常运行的。

最后所以我们设置个定时任务,让这个续期命令到期自动执行。用certbot renew –force-renewal强制续签,否则没到期,无法续签的。

#进入编辑定时任务


crontab -e 
#添加以下,每隔三个月的,凌晨 1 点 0 分,强制续签
0 1 24 3,6,9,12 * /usr/bin/certbot renew --dry-run --force-renewal && /application/nginx-1.24/sbin/nginx -s reload

附加: 安装泛域名证书


certbot certonly --manual --preferred-challenges dns --nginx-ctl /application/nginx-1.24/sbin/nginx --nginx-server-root /application/nginx-1.24/conf/ -d *.boysec.com -d boysec.com

添加一条类型为 text 的域名解析记录
主机记录为:wangxiansen.boysec.com
值为 certbot 提示生成的:arCjK1Xoi_xxxxxxxxxxxxx

验证 DNS 解析是否成功


nslookup -type=txt wangxiansen.boysec.com 8.8.8.8     

© 版权声明

☆ END ☆
喜欢就点个赞吧
点赞0 分享
图片正在生成中,请稍后...