lnmp SSL证书使用教程
lnmp1.5使用Let'sEncrypt创建SSL证书自动续期问题飘易有一台服务器安装了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证书到期了之后,并没有自动续期成功。于是手动执行以下这个脚本:
[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需要验证域名下的这个文件:
[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 这个脚本在写验证文件时,写错路径了:
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脚本:
[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 successSSL 续期成功!
并且以后再过期的时候,CRON计划任务也会自动续期了。
【其他问题】
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/