服务器

发布于 更新于

AI总结: 本文介绍了如何安装和配置acme.sh以生成和管理SSL证书。首先,通过curl命令安装acme.sh,并手动下载相关文件。执行acme.sh后,会在主目录创建.acme.sh目录。接着,配置nginx以添加验证,创建相应的目录并更新nginx配置。然后,通过acme.sh生成证书,指定使用Let’s Encrypt服务器和证书有效期。证书和密钥的路径会在执行后显示。最后,更新nginx配置以使用生成的证书,并添加crontab任务以定期检查和更新证书。改进建议包括编写脚本以自动判断证书更新情况并自动重载nginx。

安装acme.sh

curl https://get.acme.sh | sh -s email=my@example.com  

手动下载

curl https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh -o acme.sh  
curl https://github.com/acmesh-official/acme.sh/archive/master.tar.gz -o master.tar.gz  

执行acme.sh后 会在主目录创建.acme.sh目录

nginx添加验证

mkdir -p /var/www/letsencrypt  
location ~ ^/.well-known/(acme-challenge|pki-validation)/ {  
    add_header Content-Type text/plain;  
    root /var/www/letsencrypt;  
}  
nginx -t  
nginx -s reload  

生成证书

sh acme.sh --issue --server letsencrypt -d 47.xx.xx.xx -w /var/www/letsencrypt --certificate-profile shortlived --days 3  

--issue:申请证书
--server letsencrypt:使用 Let’s Encrypt 服务器
-d 47.xx.xx.xx:目标IP地址
--certificate-profile shortlived:申请一个短期证书
--days 3:证书的有效期3天 不确定有效, 申请的3天, 生成的7天

证书路径:

Your cert is in: /root/.acme.sh/47.xx.xx.xx_ecc/47.xx.xx.xx.cer  
Your cert key is in: /root/.acme.sh/47.xx.xx.xx_ecc/47.xx.xx.xx.key  
The intermediate CA cert is in: /root/.acme.sh/47.xx.xx.xx_ecc/ca.cer  
And the full-chain cert is in: /root/.acme.sh/47.xx.xx.xx_ecc/fullchain.cer  

nginx配置

#填写证书文件绝对路径  
ssl_certificate /root/.acme.sh/47.xx.xx.xx_ecc/47.xx.xx.xx.cer;  
#填写证书私钥文件绝对路径  
ssl_certificate_key /root/.acme.sh/47.xx.xx.xx_ecc/47.xx.xx.xx.key;  

添加crontab定时任务

crontab -e  
0 0 * * * /root/.acme.sh/acme.sh --cron --home "/root/.acme.sh" > /dev/null  
0 1 * * * nginx -s reload > /dev/null  

可以写个脚本判断证书是否有更新, 有更新再reload nginx