如何快速添加LNMP Let’sEncrypt免费通配符&泛域名SSL证书

目前为网站添加SSL已经是形势所趋,本站使用LNMP建站,既然Let’sEncrypt也提供免费的带通配符/泛域名SSL证书,我们不好好利用此资源,也浪费了资源;所以详细阅读了官方教程”LNMP Let’sEncrypt免费通配符/泛域名SSL证书添加使用教程“,但我没有成功过,总是报错。折腾了两天吧,这免费资源还真是贵,时间成本呀。可能是lnmp已经将命令全部写好了,也不知道怎么去查找原因,索性绕道直取源代码解决吧。下面以此记录成功过程。以下代码均在Debian9的命令执行,其它系统请自行修改。

 

一,下载ACME.SH证书安装客户端

acme.sh已经很好地支持Let’sEncrypt的申请和安装,而且还会自动更新证书,所以也就不用担心Let’sEncrypt证书过期了(acme.sh官上是这么说的,有待验证)。

wget -O -  https://get.acme.sh | sh
curl https://get.acme.sh | sh

安装好后,执行以下代码,也就是增加快捷代码,不需要到路径下或写全路径执行命令

alias acme.sh=~/.acme.sh/acme.sh
最后就是建议将acme.sh设置为自动更新,因为acme.sh软件会经常更新。

acme.sh –upgrade –auto-upgrade[/html]

二,生成证书

acme.sh 实现了 acme 协议支持的所有验证协议. 一般有两种方式验证: http 和 dns 验证,这里使用dns,因为这种方式的好处是, 你不需要任何服务器, 不需要任何公网 ip, 只需要 dns 的解析记录即可完成验证. 坏处是,如果不同时配置 Automatic DNS API,使用这种方式 acme.sh 将无法自动更新证书,每次都需要手动再次重新解析验证域名所有权。dns 方式的真正强大之处在于可以使用域名解析商提供的 api 自动添加 txt 记录完成验证.acme.sh 目前支持 cloudflare, dnspod, cloudxns, godaddy 以及 ovh 等数十种解析商的自动集成. 每个解析商的做法不一样,具体请查询https://github.com/Neilpang/acme.sh/blob/master/dnsapi/README.md

本站使用的是name.com域名,所以需要将以下内容export到变量中待用

export Namecom_Username="用户名"

export Namecom_Token="申请的API"

acme.sh --issue --dns dns_namecom -d domain.com -d *.domain.com

这里不要加其它域名了,也不需要增加www的,上面就是申请通配符的方式,如果一切OK,你会看到以下成功信息

root@localhost:~# acme.sh --issue --dns dns_namecom -d domain.com -d *.domain.com
[Wed Jun 20 17:36:52 CST 2018] Registering account
[Wed Jun 20 17:36:52 CST 2018] Registered
[Wed Jun 20 17:36:52 CST 2018] ACCOUNT_THUMBPRINT='8Kk_tTN3DuZOTfnzBQk_9yue-F1XxjB6QOcx-L1RJFA'
[Wed Jun 20 17:36:52 CST 2018] Creating domain key
[Wed Jun 20 17:36:52 CST 2018] The domain key is here: /root/.acme.sh/domain.com/domain.com.key
[Wed Jun 20 17:36:52 CST 2018] Multi domain='DNS:domain.com,DNS:*.domain.com'
[Wed Jun 20 17:36:52 CST 2018] Getting domain auth token for each domain
[Wed Jun 20 17:36:53 CST 2018] Getting webroot for domain='domain.com'
[Wed Jun 20 17:36:53 CST 2018] Getting webroot for domain='*.domain.com'
[Wed Jun 20 17:36:53 CST 2018] Found domain api file: /root/.acme.sh/dnsapi/dns_namecom.sh
[Wed Jun 20 17:36:53 CST 2018] Successfully logged in.
[Wed Jun 20 17:36:54 CST 2018] Successfully added TXT record, ready for validation.
[Wed Jun 20 17:36:54 CST 2018] Found domain api file: /root/.acme.sh/dnsapi/dns_namecom.sh
[Wed Jun 20 17:36:54 CST 2018] Successfully logged in.
[Wed Jun 20 17:36:55 CST 2018] Successfully added TXT record, ready for validation.
[Wed Jun 20 17:36:55 CST 2018] Sleep 120 seconds for the txt records to take effect
[Wed Jun 20 17:38:57 CST 2018] Verifying:domain.com
[Wed Jun 20 17:38:59 CST 2018] Success
[Wed Jun 20 17:38:59 CST 2018] Verifying:*.domain.com
[Wed Jun 20 17:39:01 CST 2018] Success
[Wed Jun 20 17:39:01 CST 2018] Removing DNS records.
[Wed Jun 20 17:39:02 CST 2018] Successfully logged in.
[Wed Jun 20 17:39:02 CST 2018] Successfully retrieved the record id for ACME challenge.
[Wed Jun 20 17:39:03 CST 2018] Successfully removed the TXT record.
[Wed Jun 20 17:39:03 CST 2018] Successfully logged in.
[Wed Jun 20 17:39:04 CST 2018] Successfully retrieved the record id for ACME challenge.
[Wed Jun 20 17:39:04 CST 2018] Successfully removed the TXT record.
[Wed Jun 20 17:39:04 CST 2018] Verify finished, start to sign.
[Wed Jun 20 17:39:05 CST 2018] Cert success.

DNS会自动在域名DNS控制面页增加TXT值,成功后又会自动删除,所以不用手动增加。看到以上信息,表示你已经成功申请到Let’sEncrypt的免费通配SSL证书,保存在/root/.acme.sh/目录下,或者是acme.sh的安装目录下,请自己额外备份这些证书。

[Wed Jun 20 17:39:05 CST 2018] Your cert is in /root/.acme.sh/domain.com/domain.com.cer
[Wed Jun 20 17:39:05 CST 2018] Your cert key is in /root/.acme.sh/domain.com/domain.com.key
[Wed Jun 20 17:39:05 CST 2018] The intermediate CA cert is in /root/.acme.sh/domain.com/ca.cer
[Wed Jun 20 17:39:05 CST 2018] And the full chain certs is there: /root/.acme.sh/domain.com/fullchain.cer

三,安装SSL证书到网站

默认生成的证书都放在安装目录下: ~/.acme.sh/, 请不要直接使用此目录下的文件,

正确的使用方法是使用 –installcert 命令,并指定目标位置, 然后证书文件会被copy到相应的位置。

acme.sh  --installcert  -d  <domain>.com   \
        --key-file   /etc/nginx/ssl/<domain>.key \
        --fullchain-file /etc/nginx/ssl/fullchain.cer \
        --reloadcmd  "service nginx force-reload"

据官方说明,一定要使用force-reload才能使新地址的SSL证书生效,但是在Debian 9下,我的Service nginx force-reload失效,所以我就换成 /etc/init.d/nginx restart,当看到以下信息就表示新地址的SSL证书生效了

[Wed Jun 20 19:19:11 CST 2018] Reload success

这个时候就可以可以使用lnmp的官方命令增加SSL证书了,命令lnmp vhost add是新增加网站,命令lnmp ssl add是让原来的网站增加SSL支持;虽然命令不同,但后续的内容是一样,如下

root@hostname:/usr/local/acme.sh# lnmp ssl add
+-------------------------------------------+
| Manager for LNMP, Written by Licess |
+-------------------------------------------+
| https://lnmp.org |
+-------------------------------------------+
Please enter domain(example: www.lnmp.org): www.domain.com
Your domain: www.bijoutoy.com
Enter more domain name(example: lnmp.org *.lnmp.org): domain.com *.domain.com
domain list: domain.com *.domain.com
Please enter the directory for domain www.domain.com: /home/wwwroot/www.domain.com
Allow access log? (y/n) y
Enter access log filename(Default:www.domain.com.log):
You access log filename: www.domain.com.log
Please enter Administrator Email Address: admin@domain.com
Server Administrator Email:admin@domain.com
1: Use your own SSL Certificate and Key
2: Use Let's Encrypt to create SSL Certificate and Key
Enter 1 or 2: 1
Please enter full path to SSL Certificate file: /usr/local/nginx/ssl/domain/fullchain.cer
Please enter full path to SSL Certificate Key file: /usr/local/nginx/ssl/domain/domain.com.key
Create dhparam.pem...
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
............................................................................................................................................................................................++*++*
Test Nginx configure file......
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Reload Nginx......

说明:前半部分没什么说的,增加新网站一样填写,选择SSL证书时,选方案1,因为我们已经有SSL证书了,然后将fullchain.cer和domain.com.key的地址输入,一切正常即OK。

 

到这里已经安装完毕,命令会自动更新/etc/local/nginx/conf/vhost/下的conf配置。不过lnmp官方配置没有强制将http转换成https访问,如果需要,更改conf下内容


server

{

listen 80;

server_name www.domain.com domain.com *.domain.com;

return 301 https://$host$request_uri;

}

 

到止所有安装完成,重新启动lnmp即可,

Previous/Next

1 Reply

Leave a Reply

Comment List