EEF 电子前哨基金会、 Mozilla 基金会和美国密歇根大学成立了一个公益组织叫 ISRG ( Internet Security Research Group ),这个组织从 2015 年开始推出了 Let’s Encrypt 免费证书,后来又有思科、 Akamai 加入,甚至连 Linux 基金会也加入了合作,这些大牌组织的加入保证了这个项目的可信度和可持续性。
而ISRG 的发起者 EFF (电子前哨基金会)为 Let’s Encrypt 项目发布了一个官方的客户端 Certbot ,利用它可以完全自动化的获取、部署和更新安全证书。
Install
先是按照官方文档选择相应的系统和服务器,假如运行环境为CentOS 7,Web服务器是Nginx,安装代码如下
1 2 3
| $ yum -y install yum-utils $ yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
|
接着下载Certbot
1
| $ sudo yum install certbot-nginx
|
Get Started
安装写成后尝试申请和部署ssl证书
运行报错,信息如下
1 2 3 4 5 6 7 8 9 10
| //log 文件记录错误 Traceback (most recent call last): File "/root/.local/share/letsencrypt/lib/python2.6/site-packages/certbot/plugins/disco.py", line 130, in prepare self._initialized.prepare() File "/root/.local/share/letsencrypt/lib/python2.6/site-packages/certbot_nginx/configurator.py", line 155, in prepare raise errors.NoInstallationError NoInstallationError 2017-07-15 14:25:35,535:DEBUG:certbot.plugins.selection:No candidate plugin 2017-07-15 14:25:35,535:DEBUG:certbot.plugins.selection:Selected authenticator None and installer None
|
发现是默认nginx配置路径和自己搭建的nginx目录不匹配,按照网友推荐的方式我也创建了快捷链接到目录中来解决问题,代码如下
1 2 3
| ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx ln -s /usr/local/nginx/conf/ /etc/nginx
|
接着运行sudo certbot --nginx
,再次错误,具体报错信息如下
1 2 3
| UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 330: ordinal not in range(128) 2017-11-12 15:15:18,560:ERROR:certbot.log:An unexpected error occurred:
|
发现是字符编码解码的问题,一种解决方法为设置以utf-8编码运行代码,第二种就将nginx配置文件中的中文或其它非ascii编码替换成ascii编码(或者删除),我使用了第二种,将配置文件多余的中文去除,接着继续运行sudo certbot --nginx
,选择好自己的域名,选择yse即可
Automating renewal
测试能否更新证书
1
| sudo certbot renew --dry-run
|
如成功则添加定时任务crontab -e
,内容如下
1
| 0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew
|
后记
从http更新到https很简单(自动化),其它的做法也有去阿里云或七牛云配置https,根据不同需求选择自己的做法,以上在安装过程中发生的一个certbot中的nginx默认路径和配置文件编码问题供参考,具体代码配置跟着官方文档即可,希望你也能早日配置更安全的https网站,就酱。
(•ㅂ•)/♥ 共勉~
参考链接
Certbot-官方网站
Certbot-nginx-NoInstallationError-Github问题
Certbot-UnicodeDecodeError-Github问题
HTTPS 简介及使用官方工具 Certbot 配置