Quantcast
Channel: LinE's Blog
Viewing all articles
Browse latest Browse all 25

使用Let’s Encrypt 来启用HTTPS

$
0
0

随着HTTPS的普及及适应信息安全的必要需求,我给我的博客也启用了HTTPS,在这里记录一下启用的过程

证书简介

此处介绍摘录自https://zh.wikipedia.org/wiki/Let%27s_Encrypt
Let’s Encrypt 是一个于2015年三季度推出的数字证书认证机构,将通过旨在消除当前手动创建和安装证书的复杂过程的自动化流程,为安全网站提供免费的SSL/TLS证书。
Let’s Encrypt 是由互联网安全研究小组(ISRG,一个公益组织)提供的服务。主要赞助商包括电子前哨基金会,Mozilla基金会,Akamai以及思科。2015年4月9日,ISRG与Linux基金会宣布合作。
用以实现这一新的数字证书认证机构的协议被称为自动证书管理环境(ACME)。GitHub上有这一规范的草案且提案的一个版本已作为一个Internet草案发布。
Let’s Encrypt 宣称这一过程将十分简单、自动化并且免费。
官方网站:https://letsencrypt.org/
项目主页:https://github.com/letsencrypt/letsencrypt

准备阶段

我自己的 VPS 使用的是 Debian Linux 系统,具体可以在

博客架构


看到详细的部署过程

首先是需要安装git,然后从github上拉取相应的生成脚本,执行构建脚本

apt-get install git
git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
./letsencrypt

执行过程中。会自动安装相关依赖,然后会询问为哪一个站点启用HTTPS,我这里是为了我博客,所以选择blog这个域名


然后提示让你输入一个邮箱,来接收关于紧急续期和安全通知的消息,这里写自己的邮箱就行

接下来需要同意相关的服务条款,选择Y就好

然后提示是否愿意与电子前线基金会共享您的电子邮件地址,电子前线基金会是Let’s Encrypt项目及相关非营利项目的创始合作伙伴,组织开发了Certbot,我们想向您发送有关EFF和的电子邮件
我们的工作是加密网络,保护其用户和捍卫数字权利。这里根据需要来选择 Yes 或是 No,不会影响申请流程

然后程序就会自动的去生成相应的证书,并修改apache配置,来启用支持ssl的配置,完成之后询问是保留 HTTP 和 HTTPS 的双重访问还是将 HTTP 的全部重定向到 HTTPS,我这里选择2,全部重定向

最后就是完成提示了。

完成之后可以去https://www.ssllabs.com/ssltest/analyze.html验证自己的网站是否启用了HTTPS
同时在Chrome上可以看到绿色的小锁,表示一切已经OK

自动续期

因为 Let’s Encrypt 的证书只有90有效期,所以需要进行续期,手动续期肯定很不方便,所以这里我寻找了一个脚本来完成自动续期的操作

#!/bin/bash
#================================================================
# Let's Encrypt renewal script for Apache on Ubuntu/Debian
# @author Erika Heidi
# Usage: ./le-renew.sh [base-domain-name]
#================================================================
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

我将这个脚本保存到了/etc/letsencrypt/letsencrypt_renew.sh
如果一切是按照默认步骤来做的,则只需要每隔一段时间跑一下这个脚本即可

/etc/letsencrypt/letsencrypt_renew.sh blog.l1n3.net


把这个脚本丢到crontab里,设置每月1日早上6点整跑一下来自动续期
00      6       1       *       *       root    /etc/letsencrypt/letsencrypt_renew.sh blog.l1n3.net


Viewing all articles
Browse latest Browse all 25

Trending Articles