【linux】使用certbot申请多个https通配符证书,立省2000,并自动更新

我的机器是Centos 7.9,由于我yum源的问题,所以我选择了使用pip安装,(需要你的机器有python环境),并且让他自动更新,示例域名为:*.example.com,请记得替换,本文分别介绍了域名在cloudflare和阿里云两种托管的情况,请按需要选择:

首先,确保你的系统已经安装了 pip。如果还没有安装,你可以使用以下命令来安装:

sudo yum install python-pip

然后,你可以使用 pip 来安装 Certbot 和 Certbot 的 Cloudflare 插件:

sudo pip install certbot
sudo pip install certbot-dns-cloudflare

接下来,你需要获取你的 Cloudflare API 密钥,并将其保存在一个文件中。你可以在 Cloudflare 的仪表板中找到你的 API 密钥。你需要创建一个文件(例如 cloudflare.ini),并在其中写入以下内容:

dns_cloudflare_email = your-email@example.com
dns_cloudflare_api_key = your-api-key

请将 your-email@example.com 替换为你的 Cloudflare 邮箱地址,将 your-api-key 替换为你的 Cloudflare API 密钥。

请确保这个文件的权限设置正确,只有 root 用户才能读取这个文件:

chmod 600 cloudflare.ini

现在,你可以使用 Certbot 来申请通配符证书:

单证书申请
sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials /path/to/cloudflare.ini -d "*.example.com"
多证书申请

sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials /path/to/cloudflare.ini -d “*.example1.com” -d ".example2.com “.example3.com”

请将 /path/to/cloudflare.ini 替换为你的 cloudflare.ini 文件的实际路径。域名也记得替换

(base) [root@hcss-ecs-a680 ssl]# certbot certonly --dns-cloudflare --dns-cloudflare-credentials /yourpath/cloudflare.ini -d "*.example.com"
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): your-email@example.com
# 在这一步需要你手动输入你的邮箱地址,这个电子邮件地址将被用于发送关于证书续期和安全通知的重要信息。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
# 用户协议,输入y同意
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
# 同上,输入y同意,接下去等着它完成就行啦!
Account registered.
Requesting a certificate for *.superxiang.com
Unsafe permissions on credentials configuration file: /sh/ssl/cloudflare.ini
Waiting 10 seconds for DNS changes to propagate

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/example.com/privkey.pem
# 上面两行就是证书地址,一定记得保存!!
This certificate expires on 2024-04-29.
These files will be updated when the certificate renews.

NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

在nginx配置证书

在 Nginx 中配置 SSL 证书,你需要在你的 Nginx 配置文件中指定证书文件和私钥文件的位置。这通常在一个 server 块中完成。以下是一个基本的示例:

server {
    listen 443 ssl;
    server_name your-domain.com;

    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;

    # 其他配置...
}

你需要将 your-domain.com 替换为你的实际域名。

在这个配置中:

  • listen 443 ssl; 告诉 Nginx 在 443 端口(HTTPS 的默认端口)上监听连接,并使用 SSL。
  • ssl_certificatessl_certificate_key 指令分别指定证书文件和私钥文件的位置。

你还需要一个 server 块来处理 HTTP(端口 80)的连接,并将它们重定向到 HTTPS。这可以通过添加一个像这样的 server 块来完成:

server {
    listen 80;
    server_name your-domain.com;
    return 301 https://$host$request_uri;
}

这个 server 块将所有到你的网站的 HTTP 连接重定向到 HTTPS。

完成这些更改后,你需要重启 Nginx 以使更改生效。这通常可以通过运行 sudo systemctl restart nginx 命令来完成(具体命令可能根据你的系统有所不同)。

在配置 SSL 之前,你应该确保你的 Nginx 配置文件没有语法错误。你可以通过运行 nginx -t 命令来检查这一点。如果这个命令报告说配置测试成功,那么你可以安全地重启 Nginx。

自动更新证书

一旦你安装了 Certbot 和它的阿里云或 Cloudflare插件,你可以使用它们来申请和更新 SSL/TLS 证书。
Certbot 添加到 crontab 中。你可以使用以下命令来编辑 crontab:

sudo crontab -e

然后,在文件中添加以下行:

0 12 * * * /usr/local/bin/certbot renew --quiet

这将会在每天中午时(0 分钟过 12 点)运行 Certbot,检查你的证书是否需要更新,并在需要的时候自动更新它们。

觉得有用就点个赞呗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值