Certbot 是一个由 EFF(Electronic Frontier Foundation)开发的工具,旨在简化获取和更新 SSL/TLS 证书的过程。以下是 CentOS 7 上安装 Certbot 的步骤:

安装必要软件

  1. 安装 snapd

snapd 是一个用于管理软件包的工具,它支持跨 Linux 发行版的应用程序安装。

sudo yum install snapd
  1. 启用 snapd 服务

确保 snapd 服务处于活动状态:

sudo systemctl enable --now snapd.socket
  1. 安装Certbot

使用 snap 安装 Certbot:

sudo snap install --classic certbot
  1. 创建 Certbot 的符号链接

创建一个符号链接,以便在命令行中更方便地访问 Certbot:

sudo ln -s /snap/bin/certbot /usr/local/bin/certbot

生成域名证书

假如域名为 myhost.com, 网站指向目录为/www/blog

domainName=myhost.com
certbot certonly -d ${domainName}

已经使用 docker 运行了 nginx, 运行上面命令,提示如下,输入 2,后面还要求输入 Webroot 目录,我的是/www/blog

How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2: Saves the necessary validation files to a .well-known/acme-challenge/
directory within the nominated webroot path. A separate HTTP server must be
running and serving files from the webroot path. HTTP challenge only (wildcards
not supported). (webroot)

拷贝证书

若没报错,将证书拷贝到 nginx 配置的 ssl 目录

cp -L /etc/letsencrypt/live/${domainName}/fullchain.pem /www/data/nginx/ssl/${domainName}.fullchain.pem
cp -L /etc/letsencrypt/live/${domainName}/privkey.pem /www/data/nginx/ssl/${domainName}.privkey.pem

配置 Nginx 使用 HTTPS

打开对应 nginx 配置文件,添加如下配置

server {
    listen 443 ssl;
    server_name myhost.com;

    ssl_certificate /www/data/nginx/ssl/myhost.com.fullchain.pem;
    ssl_certificate_key /www/data/nginx/ssl/myhost.com.privkey.pem;

    location / {
        proxy_pass http://localhost:8080; # 根据实际情况调整
        # 其他配置...
    }
}

重新加载配置

在应用更改之前,检查 Nginx 配置文件的正确性:

sudo nginx -t
sudo nginx -s reload

验证 HTTPS 配置

配置完成后,打开浏览器并访问 https://myhost.com,验证 HTTPS 是否正常工作。确保浏览器地址栏中显示安全锁图标,表示连接已加密。

证书自动续期

Let’s Encrypt 的证书有效期为 90 天,为了避免证书过期,可以使用以下命令测试自动续期:

sudo certbot renew --dry-run

编写一个脚本去更新证书,复制新证书,重新加载,放置在/data/tools/update_cert.sh

#!/bin/bash

# 域名
domainName1="myhost.com"

# 源目录(Certbot 生成证书的位置)
sourceDir1="/etc/letsencrypt/live/${domainName1}"


# 目标目录(存放证书的地方)
destinationDir="/www/data/nginx/ssl"


# 检查 Certbot 是否成功更新了证书
if certbot renew --quiet --noninteractive --post-hook "echo 'Certbot renew executed'" ; then
    echo "证书更新成功,开始复制文件..."
    
    # 创建目标目录(如果不存在)
    mkdir -p "${destinationDir}"

    # 复制证书文件
    cp -L "${sourceDir1}/fullchain.pem" "${destinationDir}/${domainName1}.fullchain.pem"
    cp -L "${sourceDir1}/privkey.pem" "${destinationDir}/${domainName1}.privkey.pem"


    # 确保文件复制成功
    if [[ -f "${destinationDir}/${domainName1}.fullchain.pem" && -f "${destinationDir}/${domainName1}.privkey.pem" ]]; then
        echo "证书文件已成功复制到 ${destinationDir}"
    else
        echo "证书文件复制失败,请检查路径和权限。"
        exit 1
    fi

    # 重新加载 nginx 配置
    echo "重新加载 nginx 配置"
    if docker compose -f '/data/docks/docker-compose.yaml' exec nginx nginx -s reload; then
        echo "Nginx 已成功应用新的证书。"
    else
        echo "重新加载配置失败,请手动检查。"
        exit 1
    fi
else
    echo "证书更新失败,请检查 Certbot 日志以获取更多信息。"
    exit 1
fi

放到定时任务中,每天执行一次

sudo crontab -e

0 3 * * * bash /data/tools/update_cert.sh