通过 letsencrypt 获取免费的 ssl 证书 概述 HTTP 与 HTTPS 原理 获取证书

概述

HTTP 是一个网络协议,大部分网站都是通过该协议来传输 WEB 页面的,内容包括图片、CSS 样式、JS 脚本等。但 HTTP 协议是明文的,传输的内容容易被偷窥和篡改。到了上世纪 90 年代,由网景公司发明了 SSL 协议,“Secure Sockets Layer” 的缩写,中文叫做“安全套接层”。到了 1999 年,SSL 因为应用广泛,已经成为互联网上的事实标准。IETF 就在那年把 SSL 标准化。标准化之后的名称改为 TLS(是“Transport Layer Security”的缩写),中文叫做“传输层安全协议”。

HTTP 与 HTTPS

很多相关的文章都把这两者并列称呼(SSL/TLS),因为这两者可以视作同一个东西的不同阶段。
HTTP 协议和“SSL/TLS”协议的组合,可以把 HTTPS 大致理解为——“HTTP over SSL”或“HTTP over TLS”。
超文本传输协议 HTTP 协议被用于在 Web 浏览器和网站服务器之间传递信息。HTTP 协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了 Web 浏览器和网站服务器之间的传输报文,就可以直接读懂其中的信息,因此 HTTP 协议不适合传输一些敏感信息,比如信用卡号、密码等。
为了解决 HTTP 协议的这一缺陷,需要使用另一种协议:安全套接字层超文本传输协议 HTTPS。为了数据传输的安全,HTTPSHTTP 的基础上加入了 SSL 协议, SSL 依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密。
HTTPS 协议需要到 ca 申请证书,一般免费证书很少,需要交费。证书获取途径:
通过 open ssl 生成自定义的签名证书,此证书并未获得全球信任机构(CA)的认可和记录;
向全球信任机构证书品牌厂商(letsencrypt)申请免费证书;
向全球信任机构证书品牌厂商(symantecgeotrusttrustasia)购买证书;
下面介绍如果通过 letsencrypt 获取免费的 ssl 证书。

原理

letsencrypt 提供了 2 种认证方式,HTTPDNSDNS 方式解决了内网不能被 HTTP 访问的问题。
详见: https://letsencrypt.org/how-it-works/

HTTP

大概的意思是,在服务器上创建一个路径 /.well-known/challenge/,让 letsencrypt 能下载其中的内容。以 www.example.com 为例,就是 letsencrypt 通过执行 http://www.example.com/.well-known/challenge/ 能下载生成在 challenge 文件下的随机字符串,以此来验证服务器。

DNS

如果是内网需要获取 SSL 证书,HTTP 方式就不行,因为 HTTP 访问不了内网的服务器,此时可以通过 DNS 方式申请,此时需要从 letsencrypt上获取一个随机字符,然后在DNS上添加一条 txt 的记录,以此验证你对域名的所有权。

获取证书

letsencrypt 为了方便用户获取证书,已开发了一个工具。
安装 certbot 工具:

$ git clone https://github.com/certbot/certbot.git
$ cd certbot
$ chmod a+x certbot-auto

certbot 获取证书有多种模式,可参考: https://certbot.eff.org/docs/using.html#third-party-plugins

HTTP 方式

创建及配置访问路径

webroot 模式为例。
创建 /.well-known/challenge/ 访问路径:

$ mkdir -p /var/www/www.example.com/

由于更新证书也需要用到这个路径,建议将该路径单独存放。
修改配置文件,添加如下配置:

location /.well-known/acme-challenge/{
    root    /var/www/www.example.com;
}

执行命令,生成证书:

$ ./certbot-auto certonly --webroot -w /var/www/www.example.com -d www.example.com -d example.com

如果有多个地址,可以多用几次 -w-d
如果不出意外的话,可以看到:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.sm.zhihuixupu.com
Using the webroot path /var/www/www.example.com for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/www.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/www.example.com/privkey.pem
   Your cert will expire on 2018-04-11\. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto 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

上面显示证书生成的路径及有效期。

  • cert.pemApache 服务器端证书
  • chain.pemApache 根证书和中继证书
  • fullchain.pemNginx 所需要 ssl_certificate 文件
  • privkey.pem:安全证书 KEY 文件

如果报如下 DNS 错误信息:

To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address.

很有可能是你域名解析DNS服务商的问题,域名一定要解析到当前 VPS 服务器。目前已知 dnspod(已验证)可用。网上说 cloudxns 也可以(笔者未验证过)。

配置 ssl

NGINX 为例:

ssl on;
ssl_certificate /etc/letsencrypt/live/www.exampe.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;

ssl_session_timeout 5m;
ssl_protocols  SSLv2 SSLv3 TLSv1;
ssl_ciphers  HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers oddn;

建议不要更改证书路径,因为更新的证书也存在这个位置。

更新证书

letsencrypt 生成的证书有效期只有 90 天,这是出于安全考虑。
在到期前,可以再次执行命令生成新的证书,如果觉得执行命令繁琐,可以将脚本写入 crontab,定时执行。
检查证书更新:

$ /data/certbot/certbot-auto --dry-run renew

更新证书脚本:

0 3 1 * * root /data/certbot/certbot-auto renew --quiet --renew-hook "/usr/local/nginx/sbin/nginx -s reload"

DNS 方式

获取 TXT 记录

./certbot-auto --manual --preferred-challenges dns certonly
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): www.example.com
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for www.example.com

-------------------------------------------------------------------------------
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
-------------------------------------------------------------------------------
(Y)es/(N)o: y

-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.www.example.com with the following value:

9Jmg5r1nZ6Y2z_mEhvWUj9YPQAbsay99YZKPgPnIXpI

Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------

添加 TXT 记录

主机记录:_acme-challenge
记录类型:TXT
记录值:9Jmg5r1nZ6Y2z_mEhvWUj9YPQAbsay99YZKPgPnIXpI
测试 TXT 记录是否生效。

$ dig -t txt _acme-challenge.example.com

生效后,回车。
看到如下内容:

Press Enter to Continue
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/www.exmaple.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/www.exmaple.com/privkey.pem
   Your cert will expire on 2018-04-11\. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto 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

证书生成成功!

配置 ssl

同上。

证书更新

同上。


参考地址:
http://www.techug.com/post/https-ssl-tls.html
https://baike.baidu.com/item/https/285356?fr=aladdin
letsencrypt 官网:https://letsencrypt.org/
https://www.jianshu.com/p/13bf14eff6dc
DNS 生成方式:http://blog.csdn.net/u012291393/article/details/78768547
acmehttps://github.com/Neilpang/acme.sh
https://github.com/xdtianyu/scripts/tree/master/le-dns

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章