Certbot, 安装并申请 Let‘s Encrypt 证书

原文地址:Certbot, 安装并申请 Let's Encrypt 证书(永久地址,保存网址不迷路 🙃)

问题描述

该笔记将记录:在 Linux 中,如何安装 Certbot 工具,并使用它申请证书,以及相关问题的处理。

解决方案

注意事项

该部分内容属于简述,详细内容请参考 certbot instructions 官方页面,依据提示操作即可。以下是操作大致流程:
1)选择站点服务器软件,以及操作系统发行版
2)查看是否满足条件
3)依据提示安装软件包
4)执行命令生成证书
5)访问站点以检查证书是否生效
6)添加定时任务以实现证书自动续期

on CentOS 7.4 with PIP3.6

经验:这种东西就应该在虚拟环境里搞,但凡与应用有关且与系统管理无关的 Python 应用,都应该在虚拟环境里搞。要是在系统环境里搞,直接 yum install,pip2.7 install,等着吧,早晚有难受的时候。

我们并没有在虚拟环境里,因为系统没有使用 Python 3.6 环境,因此我们可以直接使用:

pip3.6 install certbot certbot-nginx certbot-dns-aliyun

Certbot 1.0.0 on CentOS 7.4

################################################################################
# 安装 Certbot 工具
################################################################################

# 安装相关软件包
yum install -y epel-release
yum -y install yum-utils
yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional

# 安装并获取证书
yum install certbot python2-certbot-nginx

################################################################################
# 申请证书并配置
################################################################################

# 申请 Nginx 证书,并自动修改 Nginx 配置
certbot --nginx

# 仅仅生成证书,不修改配置
# certbot certonly --nginx

# 配置证书自动续期
echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew" \
    | tee -a /etc/crontab > /dev/null

################################################################################
# 验证配置正确
################################################################################

# 访问站点确认 HTTPS 是否生效

Certbot on Debian 8 (jessie)

################################################################################
## 第一步、安装 Certbot 工具
################################################################################

apt-get remove certbot
wget https://dl.eff.org/certbot-auto
mv certbot-auto /usr/local/bin/certbot-auto
chown root /usr/local/bin/certbot-auto
chmod 0755 /usr/local/bin/certbot-auto

################################################################################
## 第二步、申请证书并配置
################################################################################

# 申请证书
/usr/local/bin/certbot-auto --nginx
# /usr/local/bin/certbot-auto certonly --nginx # 或者仅仅生成证书,不修改配置

# 配置证书自动续期
echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto renew" \
    | tee -a /etc/crontab > /dev/null

################################################################################
## 第三步、验证配置正确
################################################################################
# 访问站点,以确认 HTTPS 是否生效

on Debian GNU/Linux 10 (buster)

# 环境设置
apt update
apt install python3 python3-venv libaugeas0

# 安装 certbot 命令
python3 -m venv /srv/certbot/
/srv/certbot/bin/pip install --upgrade pip
/srv/certbot/bin/pip install certbot
ln -s /srv/certbot/bin/certbot /usr/bin/certbot

# 证书申请操作
...

# 定时任务,以完成证书自动续期
echo "0 0,12 * * * root /srv/certbot/bin/python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q --post-hook 'systemctl reload nginx'" \
    | sudo tee -a /etc/crontab > /dev/null

附加说明

自动重启服务

在自动证书续期后,可能需要重启服务以加载证书:

certbot -q renew --renew-hook "/etc/init.d/nginx reload"

关于定时任务

定时任务用于完成证书续期。其中 sleep() 以秒为单位,random() 在 0-1 之间,因此每天 12:00、00:00 执行,在执行时,最多休眠 1 小时。

常见问题汇总

卡在 installing python packages 步骤

no response "Installing Python packages" #2516

问题描述:在Debian中,执行/usr/local/bin/certbot-auto --nginx命令后,它会调用pip命令安装相关Python包,这是时候会卡住。

问题原因:在执行pip命令时,它访问官方仓库,由于网络原因导致无法正常快速下载。

解决办法:修改$HOME/.pip/pip.conf文件,配置如下内容(使用阿里云镜像):

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

相关文章

「Certbot」- Rate Limits
「Certbot」- 基本概念
「Certbot」- ocsp.int-x3.letsencrypt.org Read timed out
「Certbot」- SERVFAIL looking up CAA for
「Certbot」- 仅申请证书、在内网中申请证书、DNS01

参考文献

certbot instructions
Let’s Encrypt: Reload Nginx after Renewing Certificates
Let's Encrypt and Certbot
Python time sleep() Method - Tutorialspoint
Python Random random() Method

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值