centOS https证书(Let's Encrypt)免费安装和自动续期

基于 Certbot + Let's Encrypt 的详细实施步骤,适用于已部署 Nginx 的 Linux 服务器:

1、安装 Certbot 及依赖

# 启用 EPEL 仓库(Let's Encrypt 依赖)
sudo yum install epel-release -y

# 更新系统并安装 Certbot + Nginx 插件
sudo yum update -y
sudo yum install certbot python3-certbot-nginx -y

2、生成 SSL 证书并配置 Nginx

# 执行 Certbot 命令(自动修改 Nginx 配置)
sudo certbot --nginx

交互提示:

  • 输入邮箱(接收证书到期提醒)

  • 同意服务条款(输入 A)

  • 选择要启用 HTTPS 的域名(按数字键)

  • 选择 2: Redirect(强制 HTTP 跳转到 HTTPS)

这一步可能产生的问题:

1、申请https证书时遇到错误:

The nginx plugin is not working; there may be problems with your existing configuration.
The error was: NoInstallationError("Could not find a usable 'nginx' binary. Ensure nginx exists, the binary is executable, and your PATH is set correctly.")

解决方案:

由于没有将nginx放到环境变量中,设置nginx软连接

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/ /etc/nginx

2、请求证书时发生错误:

Certbot failed to authenticate some domains (authenticator: nginx). The Certificate Authority reported these problems:
Domain: poemfar.com
Type: unauthorized
Detail: 123.56.146.184: Invalid response from http://poemfar.com/.well-known/acme-challenge/qlS84E_CPkmPbQUO_14HL-_YxeomDvni4N_JjH1uKR0: "\n<!--\n\tStory by HTML5 UP\n\thtml5up.net | @ajlkn\n\tFree for personal and commercial use under the CCA 3.0 license (h"

Hint: The Certificate Authority failed to verify the temporary nginx configuration changes made by Certbot. Ensure the listed domains point to this nginx server and that it is accessible from the internet.

解决方案:
犯蠢了,这个域名已经改到另一个服务器上了。而我却还在旧服务器上执行证书申请!

3. 验证https是否已经可以访问

正常情况下,直接就可以访问了。否则检查下nginx配置是否改写正确,是否重启失败之类的。

4. 配置自动续期

# 测试续期流程(模拟操作)
sudo certbot renew --dry-run

# 添加定时任务(每月1日续期)
sudo crontab -e
# 添加以下行:
0 0 1 * * /usr/bin/certbot renew --quiet --renew-hook "systemctl reload nginx"
posted @ 2025-05-13 09:06  流失的痕迹  阅读(272)  评论(0)    收藏  举报