当我们的网站想要使用https加密时,必须提供SSL证书。SSL证书有收费的,也有免费的,但是免费的证书有效期只有90天,到期就需要续费。我们可以通过certbot申请证书,并通过插件来实现自动续费,从而方便使用。
申请证书
- 首先安装certbot 对于Ubuntu/Debian系统
|
|
对于CentOS系统
|
|
-
获取api凭证 根据域名的托管平台,获取对应的api凭证。以下只提供dnspod(即腾讯云)和cloudflare的证书申请和续费方式,其他平台请自行搜索。 cloudflare:先点击右上角的头像并点击我的个人资料,然后点击API令牌,再点击创建令牌,根据提示创建即可。 dnspod:先点击右上角的头像并点击我的账号,然后在账号中心下点击API密钥,然后点击DNSPod Token(不是腾讯云API密钥),最后点击创建密钥并根据提示创建Token即可。注意,创建Token后一定要保存。
-
创建配置文件,并填写api凭证 创建
ini格式的配置文件,文件路径随意,下面以/etc/ssl/credentials.ini为例- dnspod
- 创建并编辑配置文件
1vim /etc/ssl/credentials.ini- 在配置文件下填写以下配置
1 2 3 4;API_TOKEN:填写TokenID和Token,中间以英文的逗号分隔 dns_dnspod_api_key = <API_TOKEN> ;YOUR_EMAIL:填写dnspod的注册邮箱 dns_dnspod_email = <YOUR_EMAIL> - cloudflare
- 创建并编辑配置文件
1vim /etc/ssl/credentials.ini- 在配置文件下填写以下配置
1 2;API_TOKEN:填写API令牌 dns_cloudflare_api_token = <API_TOKEN>
- dnspod
-
安装插件。需要先安装python3和pip3
- 对于dnspod,安装
certbot-dns-dnspod
1pip3 install certbot-dns-dnspod- 对于cloudflare,安装
certbot-dns-cloudflare
1pip3 install certbot-dns-cloudflare - 对于dnspod,安装
注意:如果使用debian系统,pip不能在系统环境下安装python包,可以添加--break-system-packages参数强制安装
- 申请证书。将
example.com换成自己的域名,将/etc/ssl/credentials.ini换成配置文件的路径- 对于dnspod
1sudo certbot certonly --dns-dnspod --dns-dnspod-credentials /etc/ssl/credentials.ini -d example.com -d "*.example.com"- 对于cloudflare
如果在1sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials /etc/ssl/credentials.ini -d example.com -d "*.example.com"/etc/letsencrypt/live/目录下,新建了一个文件夹example.com,则表示证书申请成功
自动续费
- certbot默认会创建一个定时器来自动续签证书,可以检查它是否已经配置
|
|
- 如果没有看到
certbot.timer,需要手动启用
|
|
- 手动测试续签
|
|
这将运行一个模拟续签过程,不会实际续签证书,但可以验证配置是否正确。
使用证书
对于大多数支持https的服务器,都需要提供两个文件,分别是证书文件和私钥文件。通过certbot申请的证书,证书文件路径为:/etc/letsencrypt/live/example.com/fullchain.pem,私钥文件路径为:/etc/letsencrypt/live/example.com/privkey.pem
- 在nginx上使用ssl证书
|
|