用Certbot 获取和自动更新SSL证书

前言

以前写过一篇「HTTPS时代,免费SSL获取与配置(Apache版)」, 使用的是sslforfree.org 服务,这个服务比较傻瓜,按照步骤一步一步就能够获得证书,可是由于证书最长的过时时间是3个月,每次都须要苦逼得从新去操做一遍。虽然 sslforfree.org 也会发邮件提醒,每3个月操做一下真的很麻烦(终于知道大姨妈的感受了)。html

自动化?

做为 Geeker ,能自动化确定要自动化,因而搜索了不少资料,看看怎么样能够实现 sslforfree 证书的自动申请,搜索结果终于找到了真相,原来 sslforfree.org 用的是 letsencrypt.org 的服务。里面详细写明了为何证书只有3个月的缘由------为了安全,就是要让大家(自)折(动)腾(化)!python

Certbot

官方推荐使用 CertBot的服务。打开网站,按照提示一步一步选择服务器操做系统,网页服务就会自动提示命令行,无脑复制粘贴便可。下面以CentOS 6.x + Apache为例。nginx

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
sudo ./certbot-auto --manual certonly

根据提示输入邮箱和域名,而后再在相应的目录下面建立文件,便可生成证书文件apache

修改 httpd.confsegmentfault

SSLCertificateKeyFile /etc/letsencrypt/live/www.yourdomain.com/privkey.pem
SSLCertificateFile /etc/letsencrypt/live/www.yourdomain.com/cert.pem
SSLCertificateChainFile /etc/letsencrypt/live/www.yourdomain.com/chain.pem

设置 crontab实现自动更新证书centos

0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && /path/to/certbot-auto renew

若是生成证书的是用 manual 模式,那么必定概要设置 --manual-auth-hook 设定验证脚本,不然没法自动更新api

#!/bin/bash
echo $CERTBOT_VALIDATION > /usr/share/nginx/html/mimvp_home/.well-known/acme-challenge/$CERTBOT_TOKEN
/path/to/certbot-auto renew   --manual-auth-hook  /etc/letsencrypt/renewal/mimji.com.sh

One more thing -- wildcard

./certbot-auto certonly --agree-tos --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory -d "*.<your host>"

参考资料

相关文章
相关标签/搜索