安装Certbot客户端
dnf install certbot python3-certbot-nginx
特别说明:关于 python3-certbot-nginx ,并不是每个版本的 Linux 系统的软件仓库里对应的名称都叫:python3-certbot-nginx,低版本的 Linux 系统,也可能叫:python2-certbot-nginx,如何安装正确的版本?可以借用如下命令搜索软件仓库里与之匹配的包名称:
dnf search *certbot-nginx
Certbot客户端生成证书
在搭建好 web server 的前提下,certbot 会借助服务器上现有的 web server 来进行验证,在其目录下创建隐藏文件, Let’s Encrypt服务端会通过域名来访问这些隐藏文件,以确认你的确拥有对应域名的控制权。以下,使用 webroot 模式来让Certbot进行验证并生成证书。
certbot --nginx -d example.com -d www.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 2017-10-23. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - 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文件夹下,生成以域名命名的文件夹,SSL证书就在里面。包含cert.pem、chain.pem、fullchain.pem、privkey.pem四个文件。默认,证书会被自动加载到 nginx 的网站配置文件当中。看起来像是这样的:
server { server_name www.example.com; root /usr/share/nginx/html/example; listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot ... } server { if ($host = www.example.com) { return 301 https://$host$request_uri; } # managed by Certbot server_name www.example.com; listen 80; return 404; # managed by Certbot }
设置自动续订
crontab -e
此时,文本编辑器将打开默认的crontab,它是一个空文本文件。粘贴在以下行中,然后保存并关闭它:
15 3 * * * /usr/bin/certbot renew --quiet
该15 3 * * *行的部分意思是“每天凌晨3:15运行以下命令”。
Certbot的renew命令将检查系统上安装的所有证书,并更新任何设置在30天内过期的证书。–quiet 告诉 Certbot 不要输出信息或等待用户输入。
cron现在每天运行这个命令。所有已安装的证书在过期前30天或更短时间内将自动更新和重新加载。
至此,网站依靠Certbot客户端完成了自动更新Let’s encrypt证书过程。