[Linux]-如何使用certbot申请免费证书
引言
这篇文章主要讲解如何使用certbot申请免费证书,以及如何给证书续期。
文章目录
0×0.申请证书
可以用任何Linux发行版,首先安装certbot:
#Ubuntu sudo apt-get install certbot -y #Arch sudo pacman -S certbot --noconfirm #其他的Linux发行版请自行替换安装命令,软件名称都是certbot
申请证书:
#申请证书:修改下面的管理员邮箱demo@gmail.com、网站根目录/var/www/html、域名地址example.com,改为自己的信息填入 certbot certonly --non-interactive --agree-tos -m demo@gmail.com --webroot -w /var/www/html -d example.com #申请完成后,会有一串重要的信息,大致中文意思如下 重要笔记: - 恭喜!您的证书和链已保存在: /etc/letsencrypt/live/example.com/fullchain.pem 您的密钥文件已保存在: /etc/letsencrypt/live/example.com/privkey.pem 您的证书将于 2022-12-07 到期。获得新的或经过调整的 将来此证书的版本,只需运行 certbot 再次。要以非交互方式更新*所有*您的证书,请运行 'certbot renew' - 您的帐户凭据已保存在您的 Certbot 中 /etc/letsencrypt 的配置目录。你应该做一个 立即安全备份此文件夹。这个配置目录将 还包含 Certbot 获得的证书和私钥,所以 定期备份此文件夹是理想的。
0×1.更新证书
这个证书的有效期限应该是3个月,可以在到期前使用下面的命令续期,续期也是3个月,可以一直续:
手动更新:
#手动更新证书 需要重启nginx或Apache服务才能刷新到页面上 certbot -q renew --deploy-hook "systemctl restart nginx"
自动更新:
#自动更新证书,同样需要重启nginx或Apache,建议手动更新,或者在晚上自动更新证书,以免业务中断 编辑 /etc/cron.d/certbot 0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew --deploy-hook "systemctl restart nginx" #systemd #可以在 /lib/systemd/system中找到certbot.servcie,certbot.timer #同样我们需要修改certbot.service文件 #将文件内容中的/usr/bin/certbot -q renew修改为 #(注意自己是nginx还是Apache,或者其他的比如Openlitespeed,替换成对应的web服务名称即可) #/usr/bin/certbot -q renew --deploy-hook "systemctl restart nginx" #如下所示 [Unit] Description=Certbot Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html Documentation=https://letsencrypt.readthedocs.io/en/latest/ [Service] Type=oneshot ExecStart=/usr/bin/certbot -q renew --deploy-hook "systemctl restart nginx" PrivateTmp=true
0×2.查看证书有效期
#后面的目录指定证书链的位置,就能查看证书的到期期限 openssl x509 -noout -dates -in /etc/letsencrypt/live/example.com/fullchain.pem
安装证书,不同的环境安装方式不一样,可以在网上搜一下不同web服务端安装ssl证书的方法,不难。