let’s encrypt是目前提供免费的ssl证书。
经过一番的实践,终于把证书正确安装上,这里记录下过程和遇到的问题,方便需要的朋友。
环境
我的环境是阿里云ubuntu-14.04
下载工具
下载 certbot
工具
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
生成ssl证书
service nginx stop
./certbot-auto certonly --standalone --email '[email protected]' -d 'example.com'
修改 example.com
为你的域名
在Installing Python packages 那里等了太长时间,中断之后再安装就出问题了
使用 certbot-auto 安装let s encrypt 证书报错
You should consider upgrading via the ‘pip install --upgrade pip’ command.
Certbot has problem setting up the virtual environment.
We were not be able to guess the right solution from your pip output.
Consult https://certbot.eff.org/docs/install.html#problems-with-python-virtual-environment
for possible solutions.
You may also find some support resources at https://certbot.eff.org/support/ .
想直接在安装 cert 但换了各种源找不到cert安装
然后发现下面的解决方法
rm /root/.pip/pip.conf
解决之后重新运行./certbot-auto certonly --standalone --email '[email protected]' -d 'example.com'
这里可能需要等待几分钟,出现类似的信息,则生成成功了。
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/example.com/fullchain.pem. Your cert
will expire on 2019-09-18. 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
配置nginx
在nginx配置文件的server中增加下面代码:
listen 443 ssl;
ssl on;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
记得修改 example.com
为你的域名
重启nginx
service nginx start
如果出现启动失败,请执行如下命令检查测配置文件
nginx -t
打开网站:https://example.com
如果看到浏览器的绿色标志,恭喜你设置成功!
自动更新证书
证书有效期90天,一般到剩下30天或者十几天的时候会给你申请证书时候的邮箱发提醒邮件,这时候可以新建一个 crontab 任务定期执行更新操作。
./certbot-auto renew --pre-hook "service nginx stop" --post-hook "service nginx start" --force-renewal
–pre-hook 这个参数表示执行更新操作之前要做的事情,因为我有 --standalone 模式的证书,所以需要 停止 nginx 服务,解除端口占用。
–post-hook 这个参数表示执行更新操作完成后要做的事情,这里就恢复 nginx 服务的启用
–force-renewal 这个参数表示强制执行更新证书,证书如果没到更新期限内,执行renew是不会更新的
这个命令是更新所有部署的证书,如果想单独更新某个证书,就用-d 参数,例如: -d 'example.com'
详细参数命令可以在这里文档查询