永久免费 SSL 证书

注:内容源于网络,用于学习交流,如有侵权,请联系本人删除

HTTP 报文以明文形式传输,如果你的网站只支持 http 协议,那么就有可能遭受到安全攻击。你可以使用 Google 浏览器打开一个 HTTP 协议网站,会发现 Chrome 在网址的左边将这个网站标记为不安全。

HTTPS 为 HTTP 报文提供了一个加密传输的通道,这样攻击者就无法窃听或者篡改传输的内容。要启用 HTTPS,必须向一个可信任机构申请一个 HTTPS 证书。专业的证书申请需要收费,不过对于个人博客网站来说,有很多免费的证书申请机构。比如 Let’s Encrypt,它提供了免费的证书申请服务,申请过程十分简单,只需要运行几条命令即可,而且证书到期后支持自动续期,可谓一劳永逸。接下来我们就是用 Let’s Encrypt 提供的工具来申请免费的 HTTPS 证书。

首先安装 Let’s Encrypt 提供的证书申请工具。登录 https://certbot.eff.org/ 选择我们博客网站使用的服务器软件和操作系统。教程中以 Nginx 和 CentOS 7 为例:

首先安装必要工具:

1
2
$ sudo yum -y install yum-utils
$ sudo sudo yum install -y certbot python2-certbot-nginx

certbot python2-certbot-nginx 是 Let’s Encrypt 提供的 HTTPS 证书申请的工具,python2-certbot-nginx 是专门针对 Nginx 的插件,使得 Nginx 运行的服务申请证书更加简单方便。

然后运行证书申请命令:

1
$ sudo certbot --nginx

注意:

经测试,运行上述命令后有可能报 ImportError: No module named ‘requests.packages.urllib3’ 的错误,这是由于 requests 和 urlib3 版本过低所致(可以参考这个 issue[2] 的讨论),解决办法是重装它们,运行下面的命令:

1
2
$ pip uninstall requests
$ pip uninstall urllib3$ yum remove python-urllib3$ yum remove python-requests

然后重新安装 certbot,由于它依赖上面两个包,所以重装时会一并装上:

1
$ sudo yum install -y certbot python2-certbot-nginx

重新执行证书申请命令:sudo certbot –nginx

会有一系列交互式的提示,首先会让你输入邮箱,用于订阅。然后输入 a 同意他们的政策。

接着 certbot 会自动扫描出来域名,根据提示输入想开启 HTTPS 的域名标号:

Which names would you like to activate HTTPS for


1: django-blog-tutorial-v2-demo.zmrenwu.com


Select the appropriate numbers separated by commas and/or spaces, or leave input

blank to select all options shown (Enter ‘c’ to cancel): 1

然后 certbot 会做一个域名校验,证明你对这个域名有控制权限。验证通过后,Let’s Encrypt 就会把证书颁发给你。

最后会提示你是否把 HTTP 重定向到 HTTPS,当然选择是,这样 certbot 会自动帮我们修改 Nginx 的配置,将 HTTP 重定向到 HTTPS,如果用户使用 HTTP 协议访问我们的博客网站,就会重定向到 HTTPS 协议访问,确保安全性。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.


1: No redirect - Make no further changes to the webserver configuration.

2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for

new sites, or if you’re confident your site works on HTTPS. You can undo this

change by editing your web server’s configuration.


Select the appropriate number [1-2] then [enter] (press ‘c’ to cancel): 2

Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/django-blog-tutorial-v2.conf

certbot 申请的证书只有 3 个月有效期,不过没有关系,certbot 可以无限续期,我们增加一条 crontab 定时任务用来执行 certbot 自动续期任务,这样一次申请,终生使用。

打开 /etc/crontab,增加定时任务:

1
echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew" | sudo tee -a /etc/crontab > /dev/null

这里配置每天 12 点执行自动续期命令。

由于全站开启了 HTTPS,因此需要把网站中非 HTTPS 的内容(比如通过 HTTP 协议请求的外部资源)改为 HTTPS,我们的博客中目前有一处引入外部图标库的样式文件是以 HTTP 协议引入的,需要改为 HTTPS:

1
2
base.html
<link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">

以上,简单几步,就开启了全站 HTTPS。

参考资料

**[1]**HelloGitHub-Team 仓库: https://github.com/HelloGitHub-Team/HelloDjango-blog-tutorial

**[2]**issue: https://github.com/certbot/certbot/issues/5104

注:文章源于网络,用于自己学习使用,如有侵权,请联系作者删除。

更新于

请我喝[茶]~( ̄▽ ̄)~*

Ming Liu 微信支付

微信支付

Ming Liu 支付宝

支付宝

Ming Liu 贝宝

贝宝