certbot 免费申请 https 证书

certbot 免费申请 https 证书

前置条件

个人使用 dns 是腾讯云,以下流程均以腾讯云为主,默认使用 nginx 代理,以下不介绍 nginx安装教程。

certbot 安装教程

  1. 安装 pip

    [!important]

    apt 找不到腾讯云的 dnspod 插件

    root@VM-4-7-ubuntu:/usr/local/nginx# apt install certbot-dns-dnspod
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    E: Unable to locate package certbot-dns-dnspod
    

    snap 是独立沙箱应用,安装的 Certbot 和 插件会存在看不到的情况,不知道🤷‍♀️怎么解决

    apt-get update
    apt install python3-venv python3-pip -y
    
  2. 安装 certbot

    # 创建 pip 虚拟环境
    python3 -m venv /opt/certbot
    # 激活 venv
    source /opt/certbot/bin/activate
    pip install --upgrade pip
    # 安装 certbot,dnspod插件
    pip install certbot certbot-dns-dnspod
    pip install "certbot-dns-dnspod[all]"
    # 退出虚拟环境
    deactivate
    # 设置软链
    ln -s /opt/certbot/bin/certbot /usr/bin/certbot
    
    # 查看 certbot 插件
    certbot plugins
    

    [!tip]

    阿里云(certbot-dns-aliyun)、腾讯云(certbot-dns-dnspod),cf(certbot-dns-cloudflare

  3. 设置 dns 配置文件

    1. 阿里云略

    2. 腾讯云

      mkdir /root/.secrets/certbot
      # 写入配置
      cat > /root/.secrets/certbot/dnspod.ini <<EOF
      dns_dnspod_email = "xxx@163.com"
      dns_dnspod_api_token = "DNSPod id,DNSPod Token"
      EOF
      # 设置权限 确保 只有 root 可读写,Certbot 才能使用。
      chmod 600 /root/.secrets/certbot/dnspod.ini
      
    3. cf 略

  4. 申请证书(泛域名)

    # --dns-dnspod-credentials 后面需要指定 dns 配置文件用于认证
    certbot certonly \
      --dns-dnspod \
      --dns-dnspod-credentials /root/.secrets/certbot/dnspod.ini \
      -d xxx.com \
      -d "*.xxx.com"
    

    成功信息:

    image-20251128155911695

自动续期

使用 Unix 系统 crontab 定时任务进行续期,下面是我的任务脚本,可自定义

# 每月 1 日,早上 1 点准时检测证书状态,-hook 是执行完 job 之后要执行的命令
0 0 1 1 * ? certbot renew --renew-hook "/usr/sbin/nginx -s reload"

nginx 配置

worker_processes  2;

events {
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout  65;
    types_hash_max_size 2048;

    # ---------------------------
    # 通用 SSL 配置
    # ---------------------------
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_tickets off;

    ssl_certificate fullchain.pem 路径;
    ssl_certificate_key privkey.pem 路径;

    # ---------------------------
    # 默认服务器 (catch-all)
    # ---------------------------
    server {
        listen 80 default_server;
        listen 443 ssl default_server;
        server_name _;

        return 403;
    }

    # ---------------------------
    # localhost 本地站点
    # ---------------------------
    server {
        listen 80;
        listen 443 ssl;
        server_name localhost;
    }

    # ---------------------------
    # 主域名 xxx.com
    # ---------------------------
    server {
        listen 443 ssl;
        server_name xxx.com;
    }

    # ---------------------------
    # HTTP 跳转 HTTPS
    # ---------------------------
    server {
        listen 80;
        server_name xxx.com;
        return 301 https://$host$request_uri;
    }
}


posted @ 2025-11-28 16:04  plum_wink  阅读(0)  评论(0)    收藏  举报