温馨提示×

Nginx SSL证书如何续期

小樊
56
2025-09-05 14:56:43
栏目: 云计算

Nginx SSL证书续期主要通过Certbot工具实现(以Let’s Encrypt证书为例),步骤如下:

  1. 安装Certbot

    # Debian/Ubuntu  
    sudo apt update && sudo apt install certbot python3-certbot-nginx  
    # CentOS/RHEL  
    sudo yum install certbot python3-certbot-nginx  
    
  2. 获取证书(首次申请)

    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com  
    

    Certbot会自动配置Nginx并重启服务。

  3. 配置自动续期

    • Certbot默认创建cron任务(每天检查,到期前30天续期),可通过以下命令测试:
      sudo certbot renew --dry-run  
      
      若输出“Congratulations”则配置成功。
    • 若需自定义定时任务,可编辑/etc/crontab,添加类似以下内容(每月1日续期):
      0 0 1 * * certbot renew --quiet  
      
  4. 手动续期(可选)

    sudo certbot renew  
    

    续期后需手动重启Nginx:

    sudo nginx -t && sudo systemctl reload nginx  
    
  5. 验证证书

    • 命令行:
      openssl x509 -in /etc/letsencrypt/live/yourdomain.com/fullchain.pem -noout -dates  
      
    • 浏览器:访问https://yourdomain.com,检查锁标志。

说明:Certbot会自动更新Nginx配置中的证书路径,无需手动修改配置文件(除非自定义路径)。

0