Use lets-encrypt tool to add ssl for website

Use lets-encrypt tool to add ssl for website Modified: 2023-07-04 20:24:23 Created: 2022-01-04 15:11:09 Tags: #linux #ssl

certbot-3来获取免费 ssl 证书。

各大浏览器停止对 http 的支持,所有的网站都需要一个 ssl 证书。收费的证书当然有保障,但是也还是有免费的午餐,如let'sencrypt

获取证书这个操作需要一个客户端(或者说脚本来实现)。常见的客户端有:

  • acme.sh
  • certbot

除了 let'sencrypt 之外,acme.sh支持多个公司的证书。(2021 年 8 月起,默认使用 ZeroSSL 的证书。)

这里还是使用 letsencrypt,使用官方推荐的certbot,好处是可以自动配置 nginx 参数。

1. 安装配置

直接使用dnf包安装工具。

sudo dnf install certbot python3-certbot-nginx -y

2. 第一次生成证书:

  1. host 机器安装nginx
    sudo dnf install nginx -y
    
  2. 确保 host 开放80443端口
    sudo firewall-cmd --zone=public --add-port 443/tcp --permanent
    sudo firewall-cmd --zone=public --add-port 80/tcp --permanent
    sudo firewall-cmd --reload
    
  3. certbot运行
    sudo certbot-3 --domain your_domain --nginx
    
    需要输入邮箱,是否同意等,按照常识输入即可
  4. 修改 nginx 的配置,将 443 端口的配置单独拿出来

通常还会使用 cloudflare 的免费套餐来做 cdn 加速,有可能遇到 526 的问题,是说证书不合法。需要设定 cloudflare 证书的类型:

cloudflare证书设定

不要设定为 Full(strict),设定为 Full 即可。

3. 更新证书

通常免费证书只有 90 天有效期,需要在到期之前手动更新。更新证书之前需要先关闭 cloudflare。然后在网站 host 上运行:

sudo certbot-3 renew