linux 安装 Let's Encrypt,并配置自动续约,centos 安装 Let's Encrypt ,ubnutu 安装 Let's Encrypt, Debian 安装 Let's Encrypt , Fedora 安装 Let's Encrypt
1、安装 Certbot 及依赖
# centos sudo yum install epel-release -y sudo yum update -y #其他的直接执行 sudo apt update # 对于 Fedora (使用 dnf) sudo dnf update
2、安装 Certbot 和 Certbot-Nginx
#对于 Debian/Ubuntu 系统,你可以使用以下命令安装 Certbot 和 Certbot-Nginx: sudo apt install certbot python3-certbot-nginx #对于 CentOS/RHEL 你可以使用以下命令 sudo yum install certbot certbot-nginx #对于 Fedora 你可以使用以下命令 sudo dnf install certbot certbot-nginx
3、生成指定域名的https证书 xxx.baidu.com请换成自己的域名,回车后需要先输入接收过期提醒的邮箱,后面全部Y回车,请注意域名一定要解析到此服务器
sudo certbot --nginx -d xxx.baidu.com -d yyy.baidu.com
4、查看是否生成
sudo ls /etc/letsencrypt/live/xxx.baidu.com/
5、检查nginx的配置是否自动生成了,如果未生成,请手动添加一个配置文件并加入以下配置
server { listen 443 ssl; server_name xxx.baiudu.com; client_max_body_size 50m; #请填写证书文件的相对路径或绝对路径 ssl_certificate /etc/letsencrypt/live/xxx.baiudu.com/fullchain.pem; # managed by Certbot #请填写私钥文件的相对路径或绝对路径 ssl_certificate_key /etc/letsencrypt/live/xxx.baiudu.com/privkey.pem; # managed by Certbot ssl_session_timeout 5m; #请按照以下协议配置 ssl_protocols TLSv1.2 TLSv1.3; #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:8080; } }
6、配置自动续约脚本
# 先测试续约 sudo certbot renew --dry-run # 编辑定时任务 sudo crontab -e # 添加以下定时任务:(每月1日续期) 0 0 1 * * /usr/bin/certbot renew --quiet --renew-hook "/bin/systemctl reload nginx"
#或者
0 0 1 * * /usr/bin/certbot renew --quiet --renew-hook "/usr/local/nginx/sbin/nginx -s reload"
#或者输出日志,方便排查
0 0 1 * * /usr/bin/certbot renew --quiet --renew-hook "/bin/systemctl reload nginx" >> /var/log/certbot-renew.log 2>&1
7、报错解决
#执行申请证书提示 #Could not find a usable 'nginx' binary. Ensure nginx exists, the binary is #executable, and your PATH is set correctly #需要将nginx放到环境变量中 ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx ln -s /usr/local/nginx/conf/ /etc/nginx
8、常用命令
#查看证书信息 certbot certificates #撤销证书 certbot revoke --cert-path /etc/letsencrypt/archive/xxx.baidu.com/cert1.pem #删除证书 certbot delete #测试续约
certbot renew --dry-run

浙公网安备 33010602011771号