手动申请SSL证书并自动续签

Administrator
Administrator
发布于 2025-05-13 / 23 阅读
0
0

手动申请SSL证书并自动续签

在 CentOS 7 上手动申请 SSL 证书(以 Let's Encrypt 为例)并设置自动续签的步骤如下:


​一、手动申请 SSL 证书​

1. ​准备工作​

  • 确保域名已解析​:将域名(如 example.com)的 A 记录指向你的 CentOS 7 服务器 IP。

  • 安装 Web 服务​(如 Nginx/Apache):

    # 安装 Nginx
    sudo yum install epel-release -y
    sudo yum install nginx -y
    sudo systemctl start nginx
    sudo systemctl enable nginx
    
    # 或安装 Apache
    sudo yum install httpd -y
    sudo systemctl start httpd
    sudo systemctl enable httpd
  • 开放 80/443 端口​:

    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    sudo firewall-cmd --reload

2. ​安装 Certbot​

# 安装 Certbot 和 Nginx/Apache 插件
sudo yum install certbot python2-certbot-nginx -y  # Nginx 插件
# 或 Apache 插件
# sudo yum install certbot python2-certbot-apache -y

3. ​申请证书​

  • 自动配置(推荐)​​:

    # Nginx 自动配置
    sudo certbot --nginx -d example.com -d www.example.com
    # 或 Apache 自动配置
    # sudo certbot --apache -d example.com -d www.example.com

    按照提示输入邮箱并同意条款,Certbot 会自动修改配置文件并获取证书。

  • 手动获取证书(不修改配置)​​:

    sudo certbot certonly --standalone -d example.com -d www.example.com

    证书会保存在 /etc/letsencrypt/live/example.com/ 目录下。

4. ​手动配置 Web 服务器(如果未自动配置)​​

  • Nginx 配置示例​:
    编辑 /etc/nginx/conf.d/example.com.conf

    server {
        listen 443 ssl;
        server_name example.com www.example.com;
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        # 其他配置...
    }

    重启 Nginx:

    sudo systemctl restart nginx
  • Apache 配置示例​:
    编辑 /etc/httpd/conf.d/example.com.conf

    <VirtualHost *:443>
        ServerName example.com
        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
        # 其他配置...
    </VirtualHost>

    重启 Apache:

    sudo systemctl restart httpd

​二、设置自动续签​

Let's Encrypt 证书有效期为 90 天,通过 Certbot 可自动续签。

1. ​测试续签命令​

sudo certbot renew --dry-run

如果输出 Congratulations, all renewals succeeded 表示配置正常。

2. ​添加定时任务​

使用 cron 设置自动续签(每周检查一次):

sudo crontab -e

添加以下行(每天凌晨 3 点检查续签并重启 Web 服务):

0 3 * * * /usr/bin/certbot renew --quiet --post-hook "systemctl reload nginx"
# 或 Apache
# 0 3 * * * /usr/bin/certbot renew --quiet --post-hook "systemctl restart httpd"
  • --quiet:静默模式,不输出日志。

  • --post-hook:续签成功后重启 Web 服务加载新证书。


​三、其他注意事项​

  1. 通配符证书​(如 *.example.com):

    • 需使用 DNS 验证:

      sudo certbot certonly --manual --preferred-challenges=dns -d *.example.com
    • 需手动在域名解析中添加 TXT 记录,自动化需配合 DNS API(如 Cloudflare、阿里云 DNS)。

  2. 证书路径​:

    • 始终引用 /etc/letsencrypt/live/example.com/ 下的符号链接(如 fullchain.pem),而非 /archive/ 目录。

  3. 强制续签​(测试用):

    sudo certbot renew --force-renewal
  4. 查看证书信息​:

    sudo certbot certificates

​四、验证自动续签​

  • 查看下次续签时间:

    sudo certbot renew --dry-run
  • 手动检查证书有效期:

    sudo openssl x509 -noout -dates -in /etc/letsencrypt/live/example.com/cert.pem

通过以上步骤,你的 CentOS 7 服务器即可完成 SSL 证书的手动申请和全自动续签。


评论