certbot自动获取证书

使用certbot获取免费证书

前置条件

  • 域名申请后解析到服务器上

配置流程

  1. 安装certbot

sudo yum install -y epel-release sudo yum install -y certbot python2-certbot-nginx

  1. 给域名申请证书

    sudo certbot certonly –nginx -d gogs.010611.xyz

这会生成 SSL 证书,通常存放在 /etc/letsencrypt/live/gogs.010611.xyz/ 目录下。

  1. 配置 Nginx 启用 HTTPS

修改你的 Nginx 配置文件(通常在 /etc/nginx/conf.d/default.conf 或 /etc/nginx/nginx.conf)

server {
    listen 80;
    server_name gogs.010611.xyz;
    return 301 https://$host$request_uri;  # 可选,强制 HTTP 跳转到 HTTPS
}

server {
    listen 443 ssl;
    server_name gogs.010611.xyz;

    ssl_certificate /etc/letsencrypt/live/gogs.010611.xyz/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/gogs.010611.xyz/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    location / {
        proxy_pass http://43.153.22.66:3000;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
  1. 重新加载 Nginx 保存配置文件后,测试并重新加载 Nginx:

    nginx -t

nginx reload

  1. 配置自动续期(可选) Let’s Encrypt 证书有效期为 90 天,建议设置自动续期:
    • 测试自动续期

      sudo certbot renew –dry-run # 测试自动续期

    • 添加定时任务:

      echo “0 3 * root certbot renew –quiet && systemctl reload nginx” | sudo tee -a /etc/crontab

这个任务会每天凌晨 3 点自动检查并续期证书。

这样,你就可以通过域名访问您的项目了!🚀

常用问题

centos stream9 安装certbot

CentOS Stream 9 的配置步骤略有不同,主要是 Certbot 的安装方式不同。请按照以下步骤操作:

  1. 安装 Certbot 在 CentOS Stream 9,你需要使用 snapd 来安装 Certbot:

sudo dnf install -y epel-release

sudo dnf install -y snapd

sudo systemctl enable –now snapd.socket

然后安装 Certbot:

sudo snap install core

sudo snap refresh core

sudo snap install –classic certbot

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

报错缺少nginx-mod-http_ssl_module模块

如果你的 Nginx 没有安装 nginx-mod-http_ssl_module,可能会提示错误,你可以手动安装:

sudo dnf install -y nginx-mod-http_ssl_module

snap安装core失败

[root@VM-4-13-centos conf.d]# snap install core
error: too early for operation, device not yet seeded or device model not acknowledged

原因:CentOS Stream 9 默认没有完全配置 snapd,导致 snap install 失败。你可以尝试以下方法来解决:

  • 方案一:手动初始化 snapd

    sudo snap install core

如果仍然报错,尝试执行:

sudo snap refresh sudo systemctl restart snapd

然后再次尝试 snap install core。

  • 方法 2:使用 DNF 直接安装 Certbot

如果 snapd 仍然无法正常使用,你可以通过 dnf 直接安装 Certbot:

sudo dnf install -y epel-release sudo dnf install -y certbot python3-certbot-nginx

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇