Ghost Blog启用HTTPS使用LetsEncrypt SSL证书

终于把全站都启用了https,花费了好一番功夫!虽然有很多高手已经分享过方法,但是由于发布时间较老,很多地方的改动让人摸不到头脑。整个过程下来,依然花了我不少时间,所以在此分享我折腾的整个过程。整个过程可以分为两部分,一个是LetsEncrypt的证书生成,一个是Ghost blog 在Nginx上的配置:(本人使用的VPS为Debian,所以一小部分命令可能有些不同。)

另外,Startssl的配置难度大于Let’s Encrypt,环节十分多且麻烦,所以建议使用Let’s Encrypt的证书。

一、LetsEncrypt篇

首先确认自己的VPS是否安装了Python2.7以上版本,Git。如果没有自行安装。

git clone https://github.com/letsencrypt/letsencrypt`  
cd letsencrypt  
sudo service nginx stop  
./letsencrypt-auto --agree-dev-preview --server  https://acme-v01.api.letsencrypt.org/directory auth
sudo service nginx start

在第四步时,按照提示输入Email和域名即可。至于为什么需要停止Nginx,是因为接下来的环节需要占用80等端口。之后证书会生成到/etc/letsencrypt/live/example.com/privkey.pem;下,其中的example.com改为自己的域名。

二、Ghost篇

修改nginx配置,sudo vim /etc/nginx/sites-available/ghost.conf,全部清空并粘贴进去下列配置。注意需要更改所有的“example.com”为自己的域名,还需要正确的配置自己的密钥位置。

server {  
    listen 80;
    server_name  example.com;
    return       301 https://example.com$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_ciphers         HIGH:!aNULL:!MD5;

    access_log   /var/log/nginx/ghost.log;
    error_log    /var/log/nginx/ghost_error.log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_pass http://127.0.0.1:2368;
        proxy_redirect off;
    }
}

接着到Ghost博客目录,把其中的config.js中的url也改成https,也就是只加个s,另外在下面填一行forceAdminSSL: true;,别忘了前面的缩进。官方教程中是这样修改的,但是我添加这一行总是出现503错误。

三、自动更新证书

由于Let’s Encrypt 只有60天的期限,所以需要续期,如果嫌手动续期麻烦,可以使用脚本加上crontab定时完成。renewal有3~7天的频率限制,所以一个月定时启动一次即可。注意:reneweal也需要关掉占用80端口的程序!

./letsencrypt-auto certonly --renew-by-default --email [email protected] -d example.com -d www.example.com

如果是使用的Apache2作为WEB Server,可以使用下列命令

sh /home/yearliny/letsencrypt/letsencrypt-auto certonly --apache --renew-by-default -d example.com -d www.example.com

如果需要自动更新,先使用“crontab -e”,选择编辑器后,在最底部加入下列内容保存退出即可。

0 0 1 * * sh /letsencrypt/letsencrypt-auto certonly --apache --renew-by-default -d example.com -d www.example.com

另外,由于是由Git控制的,所以更新程序只需要在目录下使用git pull命令即可。

现在已经过了一段时间,原来的更新方法失效,如果更新失败,请先更新系统和letsencrype,然后使用./letsencrype-auto renew 命令即可。

四、番外篇:设置HSTS

HSTS的作用就是在浏览器就直接把网站链接转为https,以防止中间301跳转请求被中间人拦截。具体设置方法也很简单,直接在Nginx配置文件中添加一行在server块中即可。

add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";  

配置完成后,可以下列命令测试是否成功。

curl -I https://yearliny.com -k

当结果中出现下列内容就是成功了。关于其他Web server如何设置Hsts,可以查看网站启用HSTS严格HTTPS

Strict-Transport-Security

Reference:

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注