Let's Encrypt是一个于2015年三季度推出的数字证书认证机构,旨在以自动化流程消除手动创建和安装证书的复杂流程,并推广使万维网服务器的加密连接无所不在,为安全网站提供免费的SSL/TLS证书。
Let's Encrypt由互联网安全研究小组(缩写ISRG)提供服务。主要赞助商包括电子前哨基金会、Mozilla基金会、Akamai以及思科。2015年4月9日,ISRG与Linux基金会宣布合作。
用以实现新的数字证书认证机构的协议被称为自动证书管理环境(ACME)。
2017年6月,Let's Encrypt宣布将于2018年1月启用 ACME v2 API。
2017年7月,Let's Encrypt宣布将于2018年1月支持 通配符证书。
Certbot 是 Let’s Encrypt 官方推荐的证书生成客户端工具。Certbot可以自动发行和安装证书,也可以使用手动模式自己安装。不需要停机。
PS:Certbot 还提供 staging environment.
ACME v2
https://acme-staging-v02.api.letsencrypt.org/directory
详情请查看:https://letsencrypt.org/docs/staging-environment/
如果Certbot不能满足你的需求,你可以选择其他方式。https://letsencrypt.org/docs/client-options/
安装软硬件环境:
Docker Nginx on Ubuntu 16.04 (xenial)
首先你得有一台外网正常访问的服务器,并且将域名(将要签发证书的域名,如 domain.com) 解析到这台服务器上。
登陆SSH并安装
certbot
$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install certbot
$ certbot --version
certbot 0.26.1
- nginx插件模式
安装 python-cert-nginx
$ sudo apt-get update
$ sudo apt-get install python-certbot-nginx
$ certbot --nginx
- 手动模式 Manual (推荐使用)
可以选择的模式有http
、dns
和tls-sni
方式生成。建议选择dns模式,方便快捷,失误少。
只需要在域名解析上添加一条txt dns记录即可验证。
例如:_acme-challenge.example.com 300 IN TXT "-dByV6BUJ_eRNwvQAfczKdcvWmDqMfLvGWaNiXNcomU"
dns模式下生成证书:
$ certbot certonly -d *.example.com --manual --server https://acme-v02.api.letsencrypt.org/directory --preferred-challenges dns
certbot certonly 手动模式下仅生成证书
-d *.example.com
通配符模式
--server https://acme-v02.api.letsencrypt.org/directory 证书服务地址
--preferred-challenges dns
生成模式,这是dns
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for 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?
确认、按提示添加dns解析。
Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:
-dByV6BUJ_eRNwvQAfczKdcvWmDqMfLvGWaNiXNcomU
Before continuing, verify the record is deployed.
如果你是用阿里的域名,到域名控制面板里添加完解析后,确认继续生成。
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2019-02-20. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. 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
上面显示的路径 /etc/letsencrypt/live/example.com/
下就是你生成的证书所在地方,以及证书到期时间等。证书有效期是90天,到期续期即可。
查看生成证书的情况:
$ sudo certbot certificates
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
Certificate Name: example.com
Domains: *.example.com
Expiry Date: 2019-02-17 06:19:48+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- 配置nginx
打开nginx配置nginx.conf,开启https
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_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
listen 443 SSL 访问端口号为443
ssl_certificate 证书文件
ssl_certificate_key 私钥文件
- nginx配置http重定向到https
对于用户不知道网站可以进行https访问的情况下,让服务器自动把http的请求重定向到https。实现全站https。
在http的server里增加rewrite ^(.*) permanent;
server{
listen 80;
server_name *example.com;
access_log /var/log/nginx/access.log main;
location / {
rewrite (.*) https://$host$1 permanent;
}
}
https地址中,如果加载了http资源,浏览器将认为这是不安全的资源,将会默认阻止。
- http请求直接跳转至https请求
- 不指定具体协议,使用资源协议自适配。
例如:<script src='//example.com/jquery.js'></script>
- 把页面的http请求都改为https
来源:公众号《互联网逻辑》 网站:https://www.nodejstack.com