使用certbot申请HTTPS证书

2025-03-23 21:24:00
丁国栋
原创 76
摘要:本文记录如何使用certbot申请https证书。

certbot为了方便申请HTTPS证书,针对不同的Web服务器、操作系统以及部署方案提供不同的操作指南,有经验和能力的可以直接参照上手。

根据笔者实践和经验来看,安装certbot最好是使用pip安装,因为包管理安装虽然是方便更新和维护,但pip可以提供certbot的额外插件,比如针对Apache2的certbot-apache插件包,针对Nginx的certbot-nginx插件包,这些是包管理器可能不具备的。或许是因为某些原因,certbot在Linux操作系统选项下并没有提供软件源那样的功能,也没有提供某种特定的封包比如deb或者rpm,而是选择了snap和pip(python包管理器)。

提示:如果我们使用 Cloudflare ,那其实没必要申请 HTTPS 证书,因为只要域名的NS服务器 设置在CF,那么 CF 可以提供代理 + HTTPS 证书功能。但如果域名没有托管在CF,那么certbot或者acme.sh这样的工具还是值得一用。

以下以Debian/Ubuntu为例安装 certbot 和certbot-apache,如果使用Nginx,则安装certbot-nginx。这两个包可以实现添加https类型的虚拟主机和修改http类型的虚拟主机使其跳转到https访问。


sudo apt-get remove certbot
sudo apt update
sudo apt install python3 python3-venv libaugeas0
sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip
sudo /opt/certbot/bin/pip install certbot certbot-apache
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot

查看命令/子命令帮助

certbot -h
certbot -h register
注册账户(可以根据需要选择 --no-eff-email 选项或者 -eff-email)
sudo certbot register --email admin@thedf.cc --agree-tos --no-eff-email
申请证书

注意:这个方法仅限于已经在apache里正确设置了http类型的虚拟主机。如果证书成功申请,则certbot会自动修改Apache2的配置文件将http访问重定向到https。

sudo certbot run --cert-name thedf.cc -d thedf.cc -d www.thedf.cc --apache

注意:如果使用nginx则将--apache参数替换为--nginx参数即可。 如果证书成功申请,则certbot会自动修改Nginx的配置文件将http访问重定向到https。

设置计划任务续订证书

echo '0 12 * * * sudo /usr/bin/certbot renew -q' | sudo tee -a /var/spool/cron/crontabs/$USER
执行更新和设置计划任务更新certbot本身
sudo /opt/certbot/bin/pip install --upgrade certbot certbot-apache
echo '0 1 15 * * * sudo /opt/certbot/bin/pip install --upgrade certbot certbot-apache' | sudo tee -a /var/spool/cron/crontabs/$USER

--

发表评论
博客分类