如何使用Let's Encrypt和Nginx为您的网站提供免费的SSL/TLS证书

步骤1:安装Certbot和Nginx

yum install -y epel-release
yum install -y certbot
yum install certbot python3-certbot-nginx

步骤2:配置Nginx以使用域名 

# 编辑Nginx配置文件
sudo nano /etc/nginx/conf.d/your_domain

# 添加以下内容
server {
    listen 80;
    server_name your_domain.com;
    location / {
        root /var/www/your_domain;
        index index.html;
    }
}

步骤3:使用Certbot获取和安装证书

  • 方法1 不指定域名自动安装证书
certbot --nginx

列出域名,选择序号添加证书并更新nginx配置

查看nginx配置信息,发现已经配置好https 443监听已经证书路径,自动加入的内容如下:

server {
    server_name admin.abc.com;
    rewrite ^(.*)$ https://${server_name}$1 permanent;
    client_max_body_size 10M;
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/admin.abc.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/admin.abc.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
  • 方法2 指定域名手动安装证书
certbot certonly --nginx  -d www.abc.com -m ejiyuan@163.com --agree-tos

运行结果如下:

需要手动配置nginx添加https 443 监听

步骤3:手动更新证书

Let's Encrypt SSL 证书的有效期仅为 90 天,因此定期更新非常重要。您可以使用以下命令续订全部 SSL 证书:

certbot renew

此命令将检查证书是否到期续订,并在必要时续订证书。

步骤4:使用定时任务自动动更新证书

使用命令:

sudo crontab -e

插入代码:

0 3 * * * /usr/bin/certbot renew --quiet && systemctl reload nginx
  • 0 3 * * *: 每天凌晨 3 点执行任务。
  • /usr/bin/certbot renew --quiet: 执行证书续期操作,--quiet 参数会隐藏输出。
  • && systemctl reload nginx: 在证书续期成功后重新加载 Nginx 配置。

确保 crond 服务正在运行

sudo systemctl enable crond
sudo systemctl start crond

检查 crontab 是否设置正确:

sudo crontab -l

其他:

certbot delete # 移除证书
certbot certificates # 查看证书信息

 

posted @   ejiyuan  阅读(93)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示