LINUX服务器最简洁的HTTPS免费证书配置方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuchangjie0112/article/details/85001429
注意:该方法已在多台服务器配置了免费的https证书,无论是更新还是第一次配置都运行成功;由于是免费版,每个证书都只有三个月的有效期,也无法保证安全和稳定性,所以只建议做测试用,客户的项目需要时,请让购买正式版证书
一、配置https免费证书,确保服务器已开启443端口

    ①在root目录下,执行下面命令:只有第一次安装时需要执行,后面追加域名时不再需要
        wget https://dl.eff.org/certbot-auto
    
    ②停止nginx,必须停止,否则安装证书时会报80端口被占用
        service nginx stop
    
    ③追加多个域名
        // [email protected] 换成自己的常用邮箱,据说到期前会给发提醒邮件
        // www.xieyouhui.com    换成自己需要配置https的域名,多个域名使用 -d 追加
        
        ./certbot-auto certonly --standalone --email [email protected] --agree-tos -d www.xieyouhui.com -d app.xieyouhui.com -d admin.xieyouhui.com
        
        //  也能用于后期域名的追加
        ./certbot-auto certonly --standalone --email [email protected] --agree-tos -d www.weimi888.com -d weimi888.com



        如果出现./certbot-auto: Permission denied 错误,需要certbot-auto文件拥有可执行权限


    ④查看生成的证书 
        ls /etc/letsencrypt/live/
        
    ⑤在nginx配置证书
        //证书文件需要开启读写权限
        ssl_certificate /etc/letsencrypt/live/cdw.me/fullchain.pem;#证书位置
        ssl_certificate_key /etc/letsencrypt/live/cdw.me/privkey.pem;# 私钥位置
        
    ⑥启动nginx
        service nginx start
    
    手动更新https证书
    2、手动更新的方法,如果遇到更新报错,可以重新运行上面第③条,重新申请证书
        service nginx stop                停止nginx        
        ./certbot-auto renew -v            执行更新方法,certbot-auto该文件需要获得执行权限
        service nginx start                启动nginx

    3、自动更新的方法 certbot-auto脚本带有自动更新证书功能,运行如下代码即可:
        // 该方法执行后,由于证书到期时间过长,还未证实是否有用
        ./certbot-auto renew --quiet --no-self-upgrade
    
    
    定时任务:不确定下面这俩定时任务是否都能使用,请自己测试
    # 每月1号5时执行执行一次更新,并重启nginx服务器
    00 05 01 * * /root/certbot renew --quiet && /bin/systemctl restart nginx
    #更新SSL证书
    30 2 3 * * /root/letsencrypt/./letsencrypt-auto renew > /var/log/le-renew.log && nginx -s reload
    
二、配置域名,通用配置文件下载地址,请按照提示更换成自己的信息
        https://liuniu.oss-cn-zhangjiakou.aliyuncs.com/xyh/peizhiwenjian.zip



三、查询证书到期时间封装方法
    
    /**
     * 获取SSL证书到期时间
     * param $domain 需要查询的域名,例如www.baidu.com
     * return array
     */
    public function getValidity($domain){
        $context = stream_context_create(array("ssl" => array("capture_peer_cert_chain" => true)));
        $socket = stream_socket_client("ssl://$domain:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
        $context = stream_context_get_params($socket);
        foreach ($context["options"]["ssl"]["peer_certificate_chain"] as $value) {
            //使用openssl扩展解析证书,这里使用x509证书验证函数
            $cerInfo = openssl_x509_parse($value);
            if(strpos($cerInfo['name'],$domain)) {

                // 提前三天预警
                $early_warning_time = time() - 3 * 24 * 3600;
                if ($cerInfo['validTo_time_t'] <= $early_warning_time) {
                    $end_type = 1;
                } else {
                    $end_type = 0;
                }

                $result = array(
                    'start_time' => date("Y-m-d H:i",$cerInfo['validFrom_time_t']), // 开始时间
                    'end_time' => date("Y-m-d H:i",$cerInfo['validTo_time_t']), // 到期时间
                    'end_type' => $end_type
                );

                return $result;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/liuchangjie0112/article/details/85001429