经过 Certbot 安装 Let's Encrypt 证书,来实现全站的 HTTPS 访问

相关知识

HTTP/HTTPS 是什么?

简单来讲,HTTP 是一个传输网页内容的协议,好比咱们浏览一个网页,网页上的文字、图片、 CSS 、 JS 等文件都是经过 HTTP 协议传输到咱们的浏览器,而后被咱们看到。由于 HTTP 是明文传输的,经过 HTTP 协议传输的内容很容易被偷看和篡改,为了安全(你确定不想被人偷看或者篡改网页内容吧,好比网站银行密码什么的。)就为 HTTP 协议再加上了一层 SSL/TLS 安全协议,因此就有了 HTTPS 。

SSL/TLS 是什么?

SSL 是指安全套接字层(Secure Sockets Layer),心里纯洁的同窗也能够理解为「带安全套的 HTTP」,由于带了安全套,因此固然会比较安全。TLS 是 传输层安全协议(Transport Layer Security),SSL 和 TLS 是同一个东西的不一样阶段,理解为同一个东西也行,都是安全协议就对了。

为何要部署 HTTPS?

说到底,就是 HTTPS 更安全。甚至为了安全,一个专业可靠的网站, HTTPS 是必须的。 Firefox 和 Chrome 都计划将没有配置 SSL 加密的 HTTP 网站标记为不安全,目前它们也正在联合其余相关的基金会与公司推进整个互联网 HTTPS 化,如今你们访问的一些主要的网站。如 Google 多年前就已经所有启用 HTTPS ,国内的淘宝、搜狗、知乎、百度等等也全面 HTTPS 了。甚至 Google 和百度的搜索结果也正在给予 HTTPS 的网站更高的排名和优先收录权。

怎么部署 HTTPS 呢?

你只须要有一张被信任的 CA ( Certificate Authority )也就是证书受权中心颁发的 SSL 安全证书,而且将它部署到你的网站服务器上。一旦部署成功后,当用户访问你的网站时,浏览器会在显示的网址前加一把小绿锁,代表这个网站是安全的,固然同时你也会看到网址前的前缀变成了 HTTPS ,再也不是 HTTP 了。

怎么得到 SSL 安全证书呢?

理论上,咱们本身也能够签发 SSL 安全证书,可是咱们本身签发的安全证书不会被主流的浏览器信任,因此咱们须要被信任的证书受权中心( CA )签发的安全证书。而通常的 SSL 安全证书签发服务都比较贵,好比 Godaddy 、 GlobalSign 等机构签发的证书通常都须要20美金一年甚至更贵,不过为了加快推广 HTTPS 的普及, EEF 电子前哨基金会、 Mozilla 基金会和美国密歇根大学成立了一个公益组织叫 ISRG ( Internet Security Research Group ),这个组织从 2015 年开始推出了 Let’s Encrypt 免费证书。这个免费证书不只免费,并且还至关好用,因此咱们就能够利用 Let’s Encrypt 提供的免费证书部署 HTTPS 了。

Let’s Encrypt 简介

前面已经介绍过, Let’s Encrypt 是 一个叫 ISRG ( Internet Security Research Group ,互联网安全研究小组)的组织推出的免费安全证书计划。参与这个计划的组织和公司能够说是互联网顶顶重要的先驱,除了前文提到的三个牛气哄哄的发起单位外,后来又有思科(全球网络设备制造商执牛耳者)、 Akamai 加入,甚至连 Linux 基金会也加入了合做,这些大牌组织的加入保证了这个项目的可信度和可持续性。

Certbot 简介

ISRG 的发起者 EFF (电子前哨基金会)为 Let’s Encrypt 项目发布了一个官方的客户端 Certbot ,利用它能够彻底自动化的获取、部署和更新安全证书。虽然第三方工具也可使用,可是官方工具更权威,风险也更小,并且遇到问题也更容易解决,毕竟有官方的支持。

实际操做

Certbot 使用方法

Certbot 的官网是https://certbot.eff.org/, 咱们打开这个连接,选择本身使用的 web server 和操做系统。选择完以后,官网就会显示出对应的安装操做步骤。
以目前我所使用的服务器为例,web server 使用Nginx(1.12),操做系统是CentOS(7.3)。由于 Certbot 打包在EPEL中,因此在安装 Certbot 以前要先安装EPELhtml

yum -y install epel-release

而后按着官网给出的步骤提示命令安装 Certbotpython

yum install python2-certbot-nginx

安装完毕后,继续输入官网提示命令linux

certbot --nginx

运行此命令会自动获取证书,而且 Certbot 会自动编辑Nginx配置文件配置HTTPS服务。nginx

这边在运行配置命令时遇到一个坑
ImportError: No module named 'requests.packages.urllib3'

能够看出是缺乏一个模块包,看配置命令在命令台的输出,Certbot 是用 python 来写 Nginx 配置。既然是用的 python,能够经过 pip list 命令查看 python 的依赖包列表。可是查看是已经存在的,这个问题很快经过 Certbot 在 github 上的一个 Issues 找到了答案(Issues连接)。目前看是包的版本依赖有问题,执行如下命令进行依赖包的重装git

pip uninstall requests
pip uninstall urllib3
yum remove python-urllib3
yum remove python-requests
yum install python-urllib3
yum install python-requests
yum install certbot

再次执行 certbot --nginx 命令成功。命令执行后首先会提示输入一个邮箱地址,主要做用是订阅一些通知,输入后回车。github

[root@izuf6fco0zwyipdq9485s2z ~]# certbot --nginx
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx

▽
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): demo@mail.com // 演示邮箱
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

回车后 Certbot 就会自动请求下载 Let's Encrypt 证书,并设置 Nginx 配置文件。期间会有一些赞成相关协议和选择哪一个网站配置的交互,这个看输出文字就能明白意思。web

...
▽
server {
(A)gree/(C)ancel: a

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
Starting new HTTPS connection (1): supporters.eff.org

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: kisstime.top
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for kisstime.top
Cleaning up challenges
...
可是这边到 Certbot 设置 Nginx 配置文件的时候遇到一个坑,报以下一个错误
An unexpected error occurred:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 2: ordinal not in range(128)
Please see the logfiles in /var/log/letsencrypt for more details.

熟悉 python 的同窗应该对这个错误比较了解。不过博主 python 仍是用的比较少,花了点时间了解了一下。shell

在解决错误以前,首先要了解unicode和utf-8的区别。unicode指的是万国码,是一种“字码表”。而utf-8是这种字码表储存的编码方法。unicode不必定要由utf-8这种方式编成bytecode储存,也可使用utf-16,utf-7等其余方式。目前大多都以utf-8的方式来变成bytecode。其次,Python中字符串类型分为byte string 和 unicode string两种。若是在python文件中指定编码方式为utf-8(#coding=utf-8),那么全部带中文的字符串都会被认为是utf-8编码的byte string(例如:mystr="你好"),可是在函数中所产生的字符串则被认为是unicode string。问题就出在这边,unicode string 和 byte string 是不能够混合使用的,一旦混合使用了,就会产生这样的错误。例如:
self.response.out.write("你好"+self.request.get("argu"))
其中,"你好"被认为是byte string,而self.request.get("argu")的返回值被认为是unicode string。因为预设的解码器是ascii,因此就不能识别中文byte string。而后就报错了。

那理解了这个错误缘由后,我这边首先想到的就是网站的 Nginx 配置文件中是否含有中文。打开一看,确实存在中文注释。将注释去掉,从新执行命令成功不在报错api

接着命令交互会提示是否将全部HTTP重定向到HTTPS,我这边选择所有重定向也就是「2」。浏览器

Cleaning up challenges
Deploying Certificate to VirtualHost:  //这边会显示你的网站配置文件目录

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 //这边会显示你的网站配置文件目录

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://kisstime.top

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=kisstime.top
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   .../fullchain.pem
   Your key file has been saved at:
   .../privkey.pem
   Your cert will expire on 2019-02-14. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

OK,当完成上面最后一步后,输入如上信息后就代表 HTTPS 配置成功了!咱们能够经过提示中的 SSL Server Test 网址来测试网站是否可以 HTTPS 来进行访问

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=kisstime.top

事实证实,高兴的太早了。。。经过测试网站和直接访问域名发现网站都是访问不了。思考了一下,确认下服务器的443端口是否开启,我这边用的是阿里云的服务器,登陆控制台查看安全组规则,发现确实是443端口没有开启,配置开启后,从新访问成功。

其余

Nginx的设置说明

基本上 Certbot 会在对应的 Nginx 配置文件加上下面的参数:

server {
    # ... other configs
    
    # SSL 设置
    listen 443 ssl;

    # set crt and key
    ssl_certificate .../fullchain.pem;
    ssl_certificate_key .../privkey.pem;
    
    # include 基本的 ssl 设置
    include .../options-ssl-nginx.conf;
    
    # Certbot 也会生成一把 Diffie-Hellman 密钥
    ssl_dhparam .../ssl-dhparams.pem;

    # ... other configs
}

自动更新证书

Let’s Encrypt 免费SSL证书用起来很是方便,但每次申请只有三个月有效期,在每次到期以前都须要从新申请,Certbot 已经提供了一键续订的命令

certbot renew

咱们能够经过添加此条命令的cron做业(或systemd计时器)来安排证书的自动续订

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin

0 0,12 * * * certbot renew --quiet

Certbot 的 log 路径

Certbot 的 log 预设路径在 /var/log/letsencrypt,有须要能够去确认查看。

相关第三方

certbot-auto是对脚本certbot的封装,能够设置系统环境或自动升级。

参考资料

牛顿曾经说过:若是说我看得比别人更远些,那是由于我站在巨人的肩膀上。(If I have seen further, it is by standing on the shoulders of giants.)感谢。

博客原文连接

原文连接

相关文章
相关标签/搜索