• HOME
  • DOCS
  • WTF
  • TECH
  • LIFE
  • PAGES
    • ARCHIVE
    • TAGS
    • ABOUT
    • FRIENDS
    • RSS
  • TOOLS
    • GEO
    • RANDOM()
    • GOO.GL
    • CSS HEART
Aj's Blog

记录时间溜走的瞬间和折腾过的那些事

给博客加个HTTPS吧 (lnmp nginx+letsencrypt)
2016-01-10 @ DOCS httpsletsencryptlnmpNginxssl

本文主要参考DigitalOcean How To Secure Nginx with Let’s Encrypt on Ubuntu 14.04 并根据自己环境做了相应改动

一、Let’s Encrypt简介

https://letsencrypt.org/

Let’s Encrypt CA项目是由电子前哨基金会、Mozilla基金会、Akamai、密歇根大学、思科联合发起的一个项目。

它旨在为站长提供一个免费的、完全自动化的证书申请过程,从而让整个互联网都能享受到HTTPS加密。

Let’s Encrypt的证书申请过程非常简单、安全、快速、自动化并且免费。

Let’s Encrypt是一个中间CA,它的CA证书由IdenTrust签发。

IdenTrust是一个Root CA,受到所有主流浏览器的信任。

从2015年10月后,Let’s Encrypt的中间CA证书被chrome、Firefox、Microsoft Edge、Safari和Opera所信任。

二、安装letsencrypt客户端

https://github.com/letsencrypt/letsencrypt

安装个git或者直接在github上Download ZIP之后自己解压

注意事项

(1) letsencrypt的证书有效期90天,到期之后需要再次申请

(2) iptables开启443端口

(3) 如果使用了CloudFlare的CDN加速,申请期间记得暂时关闭

下载完成有先不着急运行,先apt-get update或者yum update一下

三、如何使用letsencrypt客户端

本例中环境如下:
letsencrypt客户端安装在 /root/letsencrypt;
lnmp主控脚本为/root/lnmp;
域名6zou.net;
网站目录为 /home/wwwroot/6zou.net/;
letsencrypt配置文件: /etc/letsencrypt.ini;

(1) 新建letsencrypt配置文件

    vi /etc/letsencrypt.ini
    rsa-key-size = 4096
    email = admin@6zou.net
    domains = 6zou.net, www.6zou.net
    webroot-path = /home/wwwroot/6zou.net
    :wq

(2) 自动注册、自动延期

    #!/bin/bash
    web_service='/root/lnmp'
    config_file="/etc/letsencrypt.ini"
    le_path='/root/letsencrypt'
    exp_limit=30;
    if [ ! -f $config_file ]; then
        echo "[ERROR] config file does not exist: $config_file"
        exit 1;
    fi
    domain=`grep "^\s*domains" $config_file | sed "s/^\s*domains\s*=\s*//" | sed 's/(\s*)\|,.*$//'`
    cert_file="/etc/letsencrypt/live/$domain/fullchain.pem"
    #==auto regist==
    if [ ! -f $cert_file ]; then
        echo "[ERROR] certificate file not found for domain $domain."
        $le_path/letsencrypt-auto certonly --standalone --agree-tos --config $config_file
        exit 1;
    fi
    #==auto rebew==
    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 webroot renewal script..."
            $le_path/letsencrypt-auto certonly -a webroot --agree-tos --renew-by-default --config $config_file
        echo "Reloading $web_service"
        $web_service reload
        echo "Renewal process finished for domain $domain"
        exit 0;
    fi

四、如何使用letsencrypt客户端
首先确认检查前面的执行结果: /etc/letsencrypt/live/6zou.net/fullchain.pem 是否存在

修改原nginx配置文件 /usr/local/nginx/conf/vhost/6zou.net.conf

server {
        listen 443 ssl;
        server_name 6zou.net www.6zou.net;
        ssl_certificate     /etc/letsencrypt/live/6zou.net/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/6zou.net/privkey.pem;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/6zou.net;
        include wordpress.conf;
        location ~ [^/]\.php(/|$){
            ...
        }
}

并新建配置文件 /usr/local/nginx/conf/vhost/6zou.net_http_301.conf

server {
        listen 80;
        server_name 6zou.net www.6zou.net;
        return 301 https://www.6zou.net$request_uri;
}

最后lnmp reload一下
全部完成,就是你现在看到的本站的效果了。
— 2016.02.01 UPDATE —
51LA 统计不支持HTTPS,换CNZZ即可。

下一篇:   nginx(lnmp)+uwsgi emperor模式多站配置
上一篇:   Hook入门之Detour库
暂无评论

Cancel reply