飘易有一台服务器安装了LNMP 1.5的运行环境,然后创建虚拟主机VHOST的时候,使用了Let'sEncrypt创建了免费的SSL证书,这个证书是完全免费的,但是有3个月的限制,意味着3个月后就需要续期,幸运的是LNMP已经自动帮我们添加了一个计划任务,用于Let'sEncrypt SSL免费证书的自动续期。
LNMP 提供了一个 ACME.SH 脚本,方便我们执行SSL续期。
查看cron计划任务:
crontab -l 52 0 * * * "/usr/local/acme.sh"/acme.sh --cron --home "/usr/local/acme.sh" > /dev/null
这个自动续期的脚本 acme.sh 会每天自动运行一次。
但是飘易发现有几个域名的SSL证书到期了之后,并没有自动续期成功。于是手动执行以下这个脚本:
"/usr/local/acme.sh"/acme.sh --cron --home "/usr/local/acme.sh"
发现报错如下:
[Fri Jan 17 14:16:21 CST 2020] Renew: 'dongfang.piaoyi.org' [Fri Jan 17 14:16:24 CST 2020] Single domain='dongfang.piaoyi.org' [Fri Jan 17 14:16:24 CST 2020] Getting domain auth token for each domain [Fri Jan 17 14:16:24 CST 2020] Getting webroot for domain='dongfang.piaoyi.org' [Fri Jan 17 14:16:24 CST 2020] Getting new-authz for domain='dongfang.piaoyi.org' [Fri Jan 17 14:16:27 CST 2020] The new-authz request is ok. [Fri Jan 17 14:16:27 CST 2020] Verifying:dongfang.piaoyi.org [Fri Jan 17 14:16:31 CST 2020] dongfang.piaoyi.org:Verify error:Invalid response from http://dongfang.piaoyi.org/.well-known/acme-challenge/JyjWREY2lkrcpAFAD5gkZ0LffaTB2xQxB9Id-my5Puw: [Fri Jan 17 14:16:31 CST 2020] Please check log file for more details: /usr/local/acme.sh/acme.sh.log [Fri Jan 17 14:16:32 CST 2020] Error renew dongfang.piaoyi.org.
从这个错误来看,可以看到SSL需要验证域名下的这个文件:
/.well-known/acme-challenge/JyjWREY2lkrcpAFAD5gkZ0LffaTB2xQxB9Id-my5Puw
然后,再查看日志:
[Fri Jan 17 14:16:27 CST 2020] _currentRoot='/storage/wwwroot/dongfang.piaoyi.org' [Fri Jan 17 14:16:27 CST 2020] wellknown_path='/storage/wwwroot/dongfang.piaoyi.org/.well-known/acme-challenge' [Fri Jan 17 14:16:27 CST 2020] writing token:JyjWREY2lkrcpAFAD5gkZ0LffaTB2xQxB9Id-my5Puw to /storage/wwwroot/dongfang.piaoyi.org/.well-known/acme-challenge/JyjWREY2lkrcpAFAD5gkZ0LffaTB2xQxB9Id-my5Puw [Fri Jan 17 14:16:27 CST 2020] Changing owner/group of .well-known to www:www ...... [Fri Jan 17 14:16:31 CST 2020] dongfang.piaoyi.org:Verify error:Invalid response from http://dongfang.piaoyi.org/.well-known/acme-challenge/JyjWREY2lkrcpAFAD5gkZ0LffaTB2xQxB9Id-my5Puw:
可以看到Let'sEncrypt ACME.sh 这个脚本在写验证文件时,写错路径了:
/storage/wwwroot/dongfang.piaoyi.org/.well-known/acme-challenge/JyjWREY2lkrcpAFAD5gkZ0LffaTB2xQxB9Id-my5Puw
这个路径正确的应该是
/storage/wwwroot/dongfang.piaoyi.org/public/.well-known/acme-challenge/JyjWREY2lkrcpAFAD5gkZ0LffaTB2xQxB9Id-my5Puw
注意,正确的路径是中间多了一个 public 目录。为什么是这样?
因为这个项目是一个 Laravel 项目,而 Laravel 项目默认的对外的目录就是 public 目录,而我在创建虚拟主机的时候,使用的是根目录,创建好主机之后,手工去修改了 nginx 的配置文件,把
root /storage/wwwroot/dongfang.piaoyi.org;
修改为
root /storage/wwwroot/dongfang.piaoyi.org/public;
这就导致 Let'sEncrypt 记录的原始路径是不带 public 的路径,要解决这个问题,就是把SSL的配置文件里的路径也改成有 public 目录的就可以了:
lnmp或lnmpa 的话 /usr/local/nginx/conf/ssl/域名/域名.conf lamp的话 /usr/local/apache/conf/ssl/域名/域名.conf
将
Le_Webroot='/storage/wwwroot/dongfang.piaoyi.org'
修改为
Le_Webroot='/storage/wwwroot/dongfang.piaoyi.org/public'
重新执行acme脚本:
"/usr/local/acme.sh"/acme.sh --cron --home "/usr/local/acme.sh"
返回:
[Fri Jan 17 14:36:05 CST 2020] Renew: 'dongfang.piaoyi.org' [Fri Jan 17 14:36:06 CST 2020] Single domain='dongfang.piaoyi.org' [Fri Jan 17 14:36:06 CST 2020] Getting domain auth token for each domain [Fri Jan 17 14:36:06 CST 2020] Getting webroot for domain='dongfang.piaoyi.org' [Fri Jan 17 14:36:06 CST 2020] Getting new-authz for domain='dongfang.piaoyi.org' [Fri Jan 17 14:36:10 CST 2020] The new-authz request is ok. [Fri Jan 17 14:36:10 CST 2020] Verifying:dongfang.piaoyi.org [Fri Jan 17 14:36:14 CST 2020] Success [Fri Jan 17 14:36:14 CST 2020] Verify finished, start to sign. [Fri Jan 17 14:36:15 CST 2020] Cert success. [Fri Jan 17 14:36:15 CST 2020] Your cert is in /usr/local/nginx/conf/ssl/dongfang.piaoyi.org/dongfang.piaoyi.org.cer [Fri Jan 17 14:36:15 CST 2020] Your cert key is in /usr/local/nginx/conf/ssl/dongfang.piaoyi.org/dongfang.piaoyi.org.key [Fri Jan 17 14:36:16 CST 2020] The intermediate CA cert is in /usr/local/nginx/conf/ssl/dongfang.piaoyi.org/ca.cer [Fri Jan 17 14:36:16 CST 2020] And the full chain certs is there: /usr/local/nginx/conf/ssl/dongfang.piaoyi.org/fullchain.cer [Fri Jan 17 14:36:16 CST 2020] Run reload cmd: /etc/init.d/nginx reload Reload service nginx... done [Fri Jan 17 14:36:16 CST 2020] Reload success
SSL 续期成功!
并且以后再过期的时候,CRON计划任务也会自动续期了。
【备注】:如果创建主机的时候,没有选择Let's Encrypt,后期通过 lnmp ssl add 补增,这个时候,如果你的项目是laravel或lumen,那么同样的需要在指定目录的时候,加上 public 目录:
Please enter the directory for domain api.dianshi.net: /home/wwwroot/api.dianshi.net/public
这样后面续期也会自动续上。
【其他问题】
1、如果之前在nginx的配置文件里启用了 http 强制跳转到 https 的配置,这个地方也会续期失败,需要临时先将这段配置注释掉:
# http重定向301跳转https #if ($server_port !~ 443){ # rewrite ^(.*)$ https://$host$1 permanent; #}
原因很简单,现在https已经过期了,再强制跳转到https去验证下面的文件,当然是不成功的。
2、如果你有PC站和移动站自动跳转适配过,也需要注意临时取消自动跳转,比如 移动端的配置:
# PC客户端跳转 if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) { #rewrite ^(.*) https://tai.test.cn$1 redirect; }
否则验证文件会跳转到对应的PC端路径下去拉取,这个当然是错误的。
3、频率限制错误
new-authz error: {"type":"urn:acme:error:rateLimited","detail":"Error creating new authz :: too many failed authorizations recently: see https://letsencrypt.org/docs/rate-limits/","status": 429}
这个是说明触发了SSL认证的频率限制了,目前有一个限制是:每个账户每小时每域名有最多验证失败 5 次的限制。我们稍微等一会就好了。关于这个频率限制,具体参考:
https://letsencrypt.org/docs/rate-limits/