Apache环境服务器配置Let's Encrypt免费SSL证书及自动续期方法

现在愈来愈多的网站开始使用SSL证书,实现HTTPS网址形式,若是咱们是英文网站更须要用到这样格式的HTTPS网址,由于根据谷歌搜索结果提示到若是用到SSL证书的在同等条件下排名结果是有靠前可能的。咱们能够购买SSL证书,也可使用免费证书,Let's Encrypt目前已经公测阶段,这个免费证书是获得众多主流商家推荐和支持的,力求普及SSL证书在网站中的应用。git

因而咱们须要在本身网站架设SSL证书能够如何作呢?在这篇文章中,我将整理利用APACHE环境安装SSL证书的方法。github

第1、安装GITapache

    apt-get update
    apt-get -y install gitbash

第2、获取证书dom

git clone https://github.com/letsencrypt/letsencryptide

第3、进入目录生成证书网站

cd letsencrypt插件

./letsencrypt-auto --apache -d hostusvps.comvps

若是只签一个域名,按照上面的命令就能够了
他会自动安装插件,而后你须要输入邮箱来用于证书的找回。同时还会要求你选择是否同时开启Http和https和是否开启强制https。ip

这里须要注意的一个问题,由于Let's Encrypt证书是免费90天的,咱们须要提早免费续约,能够继续用90天,这里有一个脚本能够自动续约。

脚本:http://www.hostusvps.com/script/le-renew.sh

咱们能够看到脚本的内容:

#!/bin/bash
domain=$1
le_path='/opt/letsencrypt'
le_conf='/etc/letsencrypt'
exp_limit=30;

get_domain_list(){
        certdomain=$1
        config_file="$le_conf/renewal/$certdomain.conf"
        
        if [ ! -f $config_file ] ; then
                echo "[ERROR] The config file for the certificate $certdomain was not found."
                exit 1;
        fi

        domains=$(grep --only-matching --perl-regex "(?<=domains \= ).*" "${config_file}")
        last_char=$(echo "${domains}" | awk '{print substr($0,length,1)}')

        if [ "${last_char}" = "," ]; then
                domains=$(echo "${domains}" |awk '{print substr($0, 1, length-1)}')
        fi

        echo $domains;
}

if [ -z "$domain" ] ; then
        echo "[ERROR] you must provide the domain name for the certificate renewal."
        exit 1;
fi

cert_file="/etc/letsencrypt/live/$domain/fullchain.pem"

if [ ! -f $cert_file ]; then
        echo "[ERROR] certificate file not found for domain $domain."
        exit 1;
fi

exp=$(date -d "`openssl x509 -in $cert_file -text -noout|grep "Not After"|cut -c 25-`" +%s)
datenow=$(date -d "now" +%s)
days_exp=$(echo \( $exp - $datenow \) / 86400 |bc)

echo "Checking expiration date for $domain..."

if [ "$days_exp" -gt "$exp_limit" ] ; then
        echo "The certificate is up to date, no need for renewal ($days_exp days left)."
        exit 0;
else
        echo "The certificate for $domain is about to expire soon. Starting renewal request..."
        domain_list=$( get_domain_list $domain )
        "$le_path"/letsencrypt-auto certonly --apache --renew-by-default --domains "${domain_list}"
        echo "Restarting Apache..."
        /usr/sbin/service apache2 reload
        echo "Renewal process finished for domain $domain"
        exit 0;
fi

咱们能够将上面的脚本放到定时文件中,好比2个月自动更新一次就能够继续续约。或者咱们手工./le-renew.sh [须要续约SSL的域名],执行同样能够续约90天。

相关文章
相关标签/搜索