共计 1250 个字符,预计需要花费 4 分钟才能阅读完成。
概述
在 Linux 系统上自动签发免费的 HTTPS 证书,通常可以通过使用 Let’s Encrypt 证书颁发机构提供的自动化工具来实现。本文介绍使用
certbot
实现配置。
1. 安装 Certbot
首先,您需要安装 Certbot。由于 CentOS 7 默认的软件源可能不包含最新版本的 Certbot,因此推荐使用 Snap 或直接从源代码安装。
1.1 安装 Snapd
sudo yum install epel-release
sudo yum install snapd
sudo systemctl enable --now snapd.socket
1.2 通过 Snap 安装 Certbot
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
如果这里报错
error: too early for operation, device not yet seeded or device model not acknowledged
, 只需要执行下面的命令
sudo ln -s /var/lib/snapd/snap /snap
安装完成后再次执行命令通过 Snap 安装 Certbot 即可。
2. 配置 Certbot 自动签发证书
2.1 使用 Certbot 与 Nginx 集成来自动获取和配置 HTTPS 证书
sudo certbot --nginx -d test.com
- 多个域名可以添加多个
-d your_domain
, 之间使用空格分隔,如sudo certbot --nginx -d a.test.com -d b.test.com
- 该工具检测默认的
nginx
配置目录在/etc/nginx
,如果你的nginx
不在该目录下,可以添加--nginx-server-root /path/to/your/nginx/conf
指定,如nginx.conf
位于/usr/local/nginx/conf/nginx.conf
, 可以指定为--nginx-server-root /usr/local/nginx/conf
- 按照提示输入您的电子邮件地址,用于接收证书到期通知。
- 同意 Certbot 的服务条款。输入输入大写的
Y
2.2 查看已颁发的证书
sudo certbot certificates
3. 续签证书
3.1 检查 Certbot 的续签配置
sudo certbot renew --dry-run
3.2 手动续签
sudo certbot --force-renewal
3.3 使用计划任务自动续签
如每天的凌晨 2 点运行,自动续签证书
# 平滑重启(建议)
sudo tee /etc/cron.d/certbot
# 强制重启
sudo tee /etc/cron.d/certbot
4. 注意事项
因为 certbot
工具会自动修改你的域名对应的 nginx 配置,所以在签发完成后,请勿忘记检查 nginx 配置是否正确!
#推荐阅读
![在 CentOS 7 上配置 Certbot 以自动签发 Nginx 的 HTTPS 证书 在 CentOS 7 上配置 Certbot 以自动签发 Nginx 的 HTTPS 证书](https://yojack.cn/wp-content/uploads/2024/09/e59ca8centos-7e4b88ae9858de7bdaecertbote4bba5e887aae58aa8e7adbee58f91nginxe79a84httpse8af81e4b9a6_66e27bfee4ef2.png)
本文分享内容作者亲自在多台 centos7 机器上实践过,适 …
标签:https
nginx
文章来源: 在 CentOS 7 上配置 Certbot 以自动签发 Nginx 的 HTTPS 证书
正文完