CentOS 源码环境下 Certbot 安装 + HTTPS 配置完整指南
☑️ 背景信息
系统:CentOS 7
Python:源码安装 Python 3.11,路径
/usr/bin/python3
Nginx:源码安装,配置目录
/usr/local/nginx/conf/nginx.conf
Web 路径:
/usr/local/nginx/html/bctc/wordpress
域名:
bctc-squad.cn
☑️ 安装 certbot 并解决路径问题
① 确保 Python3 pip3 正常
python3 -m ensurepip --upgrade python3 -m pip install --upgrade pip
② pip 安装 certbot
pip3 install certbot pip3 install certbot-nginx
③ 找不到 certbot 输入点
执行 certbot
时报错:
-bash: certbot: command not found
解决办法:搜索 certbot 实际命令位置:
find /usr/python3.11 -type f -name certbot
如果找到:
/usr/python3.11/bin/certbot
创建全局软链接:
ln -s /usr/python3.11/bin/certbot /usr/bin/certbot
此时再执行:
certbot --version
☑️ 签发 SSL 证书
/usr/bin/certbot certonly \ --webroot \ -w /usr/local/nginx/html/bctc/wordpress \ -d bctc-squad.cn
成功后证书保存于:
/etc/letsencrypt/live/bctc-squad.cn/fullchain.pem
/etc/letsencrypt/live/bctc-squad.cn/privkey.pem
☑️ 配置 Nginx 支持 HTTPS
server { listen 80; # 注意需要放开80端口 server_name *.bctc-squad.cn bctc-squad.cn; rewrite ^/(.*)$ https://bctc-squad.cn/$1; } server { listen 443 ssl; server_name bctc-squad.cn; ssl_certificate /etc/letsencrypt/live/bctc-squad.cn/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/bctc-squad.cn/privkey.pem; # 其他配置省略 }
重载 Nginx :
nginx -s reload
☑️ 设置证书自动续签 crontab
crontab -e
推荐版:每两个月一次(使用绝对路径)
0 3 1 */2 * /usr/bin/certbot renew --quiet --post-hook "/usr/local/nginx/sbin/nginx -s reload"
☑️ 结论
这次配置经历了:
certbot 安装位置异常
缺少 main 时无法
python3 -m certbot
运行手动创建软链接解决 certbot not found
成功签发证书并配置 nginx 支持 https
配置自动续签
这套思路适用于所有使用清空系统或静态 Nginx 环境、自行编译 Python 环境的情况。
发表评论