网站制作学习网经验与学习→正文:免费ssl证书自动续费
字体:

免费ssl证书自动续费

经验与学习 2024/5/29 9:25:07  点击:不统计

原载于:原文章%77w%77%2Ef%6F%72%61%73%70%2E%63n
 这里学习一下免费ssl证书以及自动续费,原因是:很多免费的证书都变成3个月了,不能自动续期,包括阿里云,腾讯云等。

 
 
1. 安装certbot 
Ubuntu: apt install -y certbot
Centos: yum install -y certbot
Alpine linux: apk add certbot
 
2. 申请证书
(1)首先停止现在的 80 和443 端口nginx 服务
(2)解析域名到当前服务器上,比如 ssl.forasp.cn 到1.2.3.4
在服务器上运行 curl -s ipv4.ip.sb 获取当前服务器对外ip 为1.2.3.4
(3)将域名@符号的dns名称指向1.2.3.4
(4)域名ssl命令
certbot certonly --standalone -d ssl.forasp.cn --email you@email.com --agree-tos --no-eff--email --force-renewal
运行后会自动生成密钥,以及本地地址
(5)一个星期一个域名最多申请5个
 
3. 申请续签shell 代码如下
 
# 定义证书存储目录
certs_directory="/etc/letsencrypt/live/"
 
days_before_expiry=5  # 设置在证书到期前几天触发续签
 
# 遍历所有证书文件
for cert_dir in $certs_directory*; do
    # 获取域名
    domain=$(basename "$cert_dir")
 
    # 忽略 README 目录
    if [ "$domain" = "README" ]; then
        continue
    fi
 
    # 输出正在检查的证书信息
    echo "检查证书过期日期: ${domain}"
 
    # 获取fullchain.pem文件路径
    cert_file="${cert_dir}/fullchain.pem"
 
    # 获取证书过期日期
    expiration_date=$(openssl x509 -enddate -noout -in "${cert_file}" | cut -d "=" -f 2-)
 
    # 输出证书过期日期
    echo "过期日期: ${expiration_date}"
 
    # 将日期转换为时间戳
    expiration_timestamp=$(date -d "${expiration_date}" +%s)
    current_timestamp=$(date +%s)
 
    # 计算距离过期还有几天
    days_until_expiry=$(( ($expiration_timestamp - $current_timestamp) / 86400 ))
 
    # 检查是否需要续签(在满足续签条件的情况下)
    if [ $days_until_expiry -le $days_before_expiry ]; then
        echo "证书将在${days_before_expiry}天内过期,正在进行自动续签。"
 
        # 停止 Nginx
        docker stop nginx
 
        iptables -P INPUT ACCEPT
        iptables -P FORWARD ACCEPT
        iptables -P OUTPUT ACCEPT
        iptables -F
    
        ip6tables -P INPUT ACCEPT
        ip6tables -P FORWARD ACCEPT
        ip6tables -P OUTPUT ACCEPT
        ip6tables -F
 
        # 续签证书
        certbot certonly --standalone -d $domain --email your@email.com --agree-tos --no-eff-email --force-renewal
 
        # 启动 Nginx
        docker start nginx
 
        echo "证书已成功续签。"
    else
        # 若未满足续签条件,则输出证书仍然有效
        echo "证书仍然有效,距离过期还有 ${days_until_expiry} 天。"
    fi
 
    # 输出分隔线
    echo "--------------------------"
done
 
 
将上面代码保存为shell auto_cert_renewal.sh
 
 
chmod +x auto_cert_renewal.sh
 
./auto_cert_renewal.sh
 
 
本文总结来源:https://www.bilibili.com/video/BV1NM4m1y79Z/?spm_id_from=333.1007.tianma.10-3-37.click&vd_source=79250fedfa79be51962ec2a184d937c3
 
 

·上一篇:ipad 设置ssh远程 >>    ·下一篇:无法将磁盘“system.vhd”附加到 WSL2系统找不到指定的文件 >>
推荐文章
最新文章