📜  Letencrypt 通配符证书 certbot - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:02:40.608000             🧑  作者: Mango

Letencrypt 通配符证书 certbot - Shell-Bash

介绍

Letencrypt 是一种开放且自动化的证书认证机构,致力于让互联网更加安全。使用 Letencrypt 证书可以使你的网站的访问更加安全,并可以避免被浏览器标记为不安全。Certbot 是 Letencrypt 的一个官方命令行工具,可以帮助你在服务器上自动化申请、部署和更新证书。本篇介绍如何使用 certbot 在服务器上申请 Letencrypt 通配符证书。

准备

在使用 certbot 之前,必须确保以下条件已经满足:

  • 服务器已经正确配置了 DNS 解析
  • 服务器已经安装了 Python 2.x 或 3.x
  • 服务器已经正确配置了 Web 服务器,例如 Nginx 或 Apache
安装

certbot 可以通过 pip 安装:

sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot python-certbot-nginx
使用

在使用 certbot 申请 Letencrypt 通配符证书之前,必须创建一个 DNS TXT 记录,用于验证域名的所有权。具体创建方法请咨询你的 DNS 服务提供商。

创建 DNS TXT 记录后,运行以下命令申请证书(以 Nginx 服务器为例):

sudo certbot certonly \
  --server https://acme-v02.api.letsencrypt.org/directory \
  --email youremail@example.com \
  --agree-tos \
  --dns-cloudflare \
  --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \
  -d *.example.com \
  --preferred-challenges dns-01

其中:

  • --server:Letencrypt 服务器地址
  • --email:邮箱地址,用于证书更新通知
  • --agree-tos:自动同意 Letencrypt 的服务条款
  • --dns-cloudflare:使用 Cloudflare DNS API 进行验证
  • --dns-cloudflare-credentials:Cloudflare 的 API 信息,保存在指定的文件中
  • -d:证书中包含的域名,这里使用通配符 *.example.com
  • --preferred-challenges dns-01:使用 DNS 记录的方式验证域名

执行上述命令后,certbot 会自动向 Letencrypt 服务器发送证书申请,然后会让你输入 Cloudflare 的 API 信息。完成后,证书文件将存储在 /etc/letsencrypt/live/example.com/ 目录中。

自动更新

Letencrypt 证书的有效期为 90 天,因此需要定期更新。Certbot 可以自动化证书更新,可以在 crontab 中设置定时任务。例如,在每天凌晨 4 点自动更新证书:

0 4 * * * /usr/bin/certbot renew \
  --quiet \
  --no-self-upgrade
结论

Certbot 是一种方便易用的 Letencrypt 官方命令行工具,可以帮助你轻松申请和更新证书。使用 certbot,你可以在任何需要安全性支持的网站上使用 Letencrypt 证书。