Skip to content

TLS 证书自动申请

::: tip

  • Certbot automatically connects to Let’s Encrypt to obtain the certificate

  • Certbot will only download the certificate and private key.

  • The files are saved in:

    /example.com/fullchain.pem   (certificate)
    /etc/letsencrypt/live/example.com/privkey.pem     (private key)
    

:::

certbot

sudo apt update
sudo apt install python3 python3-dev python3-venv libaugeas-dev gcc
  • set up a virtual environment
sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip
  • Install Certbot
sudo /opt/certbot/bin/pip install certbot certbot-nginx
  • Prepare the Certbot command
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot
  • run 自动配置nginx
    sudo certbot --nginx -d example.com
    
certbot certonly --standalone 

只申请证书

不占用80端口

sudo certbot certonly --webroot -w /var/www/html -d example.com

配置 Nginx

80端口下的代理需要添加 在 Nginx 配置文件中添加以下内容:

location /.well-known/acme-challenge/ {
    root /var/www/certbot;
}

创建并设置目录权限

  1. 创建证书验证目录:
sudo mkdir -p /var/www/certbot
  1. 设置权限:
sudo chown -R www-data:www-data /var/www/certbot

申请通配符证书

Warnning

必须使用 DNS-01 验证,HTTP 验证不支持通配符

sudo certbot certonly \
  --manual \
  --preferred-challenges dns \
  -d "*.catpd.cn" \
  -d "catpd.cn"

域名解析中添加 _acme-challenge value 为 日志中输出的

校验

dig TXT _acme-challenge.catpd.cn
☁️ 部署建议
如果你打算长期运行项目(博客 / API / 自动化脚本),建议直接用云服务器,会比本地稳定很多。
👉 查看云服务器(新用户优惠)