转载

linux使用Let's Encrypt记录

温馨提示:
本文最后更新于 2024年12月11日,已超过 142 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

一 安装客户端

yum install certbot

二 生成证书

需要提前解析好example.com www.example.com域名指向本机IP,因为会使用443端口去校验,所有需要先停止nginx或其他可能占用443端口的服务,证书生成后再去启动即可。

certbot certonly --standalone -d example.com -d www.example.com

三 配置证书自动续期

使用cron命令进行自动续期操作

证书是3个月到期,一般2个月的时候进行一次更新即可,以下是cron脚本。脚本名称为auto_renew

15 2 * */2 * certbot renew --pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx"

--pre-hook 这个参数表示执行更新操作之前要做的事情,先停止 nginx 服务,解除端口占用。
--post-hook 这个参数表示执行更新操作完成后要做的事情,恢复 nginx 服务。

使用crontab 启动这个定时任务

crontab auto_renew

四 配置相关

生成好的证书文件在/etc/letsencrypt/live/目录下,配置好域名然后指向即可。

以下是配置项模板

proxy_headers_hash_bucket_size 1024;
types_hash_bucket_size 1024;

server {
        server_name www.example.com;
        listen 443;
        ssl on;
        ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;

        location / {
           proxy_pass http://127.0.0.1:8888;
           proxy_http_version 1.1;
           proxy_set_header X_FORWARDED_PROTO https;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $host;
        }
    }
正文到此结束