Let's Encrypt

趁今天放假给博客换上了 https 。

HTTPS 免费证书我选择了 Let's Encrypt ,不但免费,还靠谱、方便部署。

Let's Encrypt 推荐使用 Certbot 进行部署:

安装 certbot

$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install certbot 

nginx 修改

要部署的域名修改下配置方便 certbot 自动生成证书:

location / {  
    root /usr/share/nginx/html;
}

然后在要部署的域名的 nginx 的 root 目录里创建一个可写的 .well-known 目录:

mkdir -p /usr/share/nginx/html/.well-known  

certbot 配置

编写一个配置文件:

# vim /etc/letsencrypt/configs/example.com.conf
domains = example.com  
rsa-key-size = 2048  
email = [email protected]  
text = True  
authenticator = webroot  
webroot-path = /usr/share/nginx/html  

请根据情况修改自己的域名。

生成证书

certbot -c /etc/letsencrypt/configs/example.com.conf certonly  

没问题的话,certbot 会帮我们创建好所需要的证书:

# tree /etc/letsencrypt/live/example.com/
/etc/letsencrypt/live/example.com/
├── cert.pem -> ../../archive/example.com/cert1.pem
├── chain.pem -> ../../archive/example.com/chain1.pem
├── fullchain.pem -> ../../archive/example.com/fullchain1.pem
├── privkey.pem -> ../../archive/example.com/privkey1.pem
└── README

0 directories, 5 files  

还要手动生成一个 Diffie-Hellman 证书:

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048  

nginx 配置

server {  
    listen 80;
    return 301 https://$host$request_uri;
}

server {  
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;
    location / {
        root /usr/share/nginx/html;
    }
}

重启或者 reload nginx ,你就可以看到浏览器上自己的域名的旁边有个绿色的锁啦!

renew

Let's Encrypt 的证书有效期是 90 天,所以我们需要自动更新:

certbot renew  

每月弄个定时任务就 OK 了 (如果没到期会自动跳过的) 。

网站资源引用问题

需要将

href="http://cdn.bootcss.com/highlight.js/8.0/styles/github.min.css"  

这种类型的资源引用改为

href="//cdn.bootcss.com/highlight.js/8.0/styles/github.min.css"  

否则会被 block 。感谢小罗的提醒。