Lishengxie
  • Posts
  • About
  • Contact
  1. Home
  2. Posts
  3. Let's Encrypt 免费申请 SSL 证书,并实现自动续期

Let's Encrypt 免费申请 SSL 证书,并实现自动续期

Sep 14, 2025 linux Lishengxie

背景

对于公网可以访问的网站,使用 HTTPS 协议是保证安全访问的基础要求,而 SSL 证书又是 HTTPS 协议不可或缺的一环。SSL 证书需要 CA 机构颁发,当前使用了阿里云的个人测试证书(免费证书),申请后上传到服务器并更新 nginx 配置。

当前存在的问题是,阿里云的免费 SSL 证书每三个月过期一次,需要手动重新申请并部署到服务器上,流程比较复杂。在网上看到可以使用 Let’s Encrypt 免费申请 SSL 证书,并实现自动续期,于是做了尝试,这里记录下相关流程。

环境准备

  • 一台可以访问公网的服务器,我使用的是 Linux/Ubuntu。
  • root 权限或者 sudo 权限。
  • 存在 DNS 解析的域名。

Let’s Encrypt 证书颁发

Let’s Encrypt 是一个免费的、自动化的、开放的证书颁发机构(CA),提供免费 SSL 证书。它的目标是让所有网站都能轻松实现 HTTPS 加密,保障用户数据的安全。通过 Let’s Encrypt 可以为网站的主域名、子域名,甚至泛域名配置安全证书,而且过程完全自动化,不需要手动申请和管理证书,官方推荐使用 Certbot。

Certbot 安装

Linux 平台上可以使用 snap 或 pip 安装 Certbot,这里使用 Python + pip 的方式,参考安装流程。

1、首先安装 Python 环境。

sudo apt update
sudo apt install python3 python3-dev python3-venv libaugeas-dev gcc

2、移除旧版本的 Certbot 工具。

sudo apt-get remove certbot

3、设置 Python 虚拟环境并安装 Certbot

# 设置 Python 虚拟环境
sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip
# 安装 Certbot
sudo /opt/certbot/bin/pip install certbot certbot-nginx
# 创建符号链接方便直接访问
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot

获取证书

1、使用 Certbot 获取证书,默认 nginx 配置文件的路径为 /etc/nginx/nginx.conf,可以使用 –nginx-server-root 指定不同的配置文件位置。

sudo certbot --nginx --nginx-server-root /www/server/nginx/conf/

2、配置自动更新。由于 Let’s Encrypt 的证书有效期也只有 90 天,这里可以配置一个系统定时任务,由 Certbot 每天检查证书是否过期并自动更新。

# 在/etc/crontab定时任务配置文件添加定时任务
# 如下表示每天的0和12点随机延迟0-3600秒检查并执行证书更新服务
echo "0 0,12 * * * root /opt/certbot/bin/python -c 'import random; import time; time.sleep(random.random() * 3600)' && sudo certbot renew --nginx --nginx-server-root /www/server/nginx/conf -q" | sudo tee -a /etc/crontab > /dev/null

验证证书安装成功

quicklist

定期更新Certbot

sudo /opt/certbot/bin/pip install --upgrade certbot certbot-nginx

参考资料

  • 使用 Let’s Encrypt 免费申请泛域名 SSL 证书,并实现自动续期
  • https://certbot.eff.org/instructions?ws=nginx&os=pip

Table of Contents

  • 背景
  • 环境准备
  • Let’s Encrypt 证书颁发
    • Certbot 安装
    • 获取证书
    • 验证证书安装成功
    • 定期更新Certbot
  • 参考资料

Recent Posts

  • 分布式ID生成方案全解析:从数据库到雪花算法 Jun 25, 2026
  • Let's Encrypt 免费申请 SSL 证书,并实现自动续期 Sep 14, 2025
  • Redis ziplist、quicklist 和 listpack Mar 3, 2025
  • Nginx禁止使用IP直接访问服务器上相应端口 May 11, 2024
  • LeetCode刷题 - KMP算法 Feb 4, 2024

Categories

  • Linux7
  • 算法学习7
  • 论文笔记6
  • C++4
  • 未分类3
  • Go学习2
  • Redis1
  • SystemC1
  • Verilog1

Tags

← Redis ziplist、quicklist 和 listpack 分布式ID生成方案全解析:从数据库到雪花算法 →

Related Posts

  • Nginx禁止使用IP直接访问服务器上相应端口 May 11, 2024
  • 云服务器软件安装 Apr 30, 2023
  • 常用资源汇总 Apr 30, 2023
  • Linux常用命令记录 Apr 30, 2023
皖ICP备2023003716号-1 | 公安备案皖公网安备34012202341113 | 违法和不良信息举报邮箱:1141751053@qq.com
Powered by Hugo & Explore Theme.