初次部署 https
使用 Let’s Encrypt给域名加把小绿锁
Let’s Encrypt 客户端
安装
1 | # yum install -y epel-release nginx git |
获取 SSL 证书
证书申请频率限制
- IP 限制:每注册 IP 每 3 个小时不超过 10 次
- 域名数量限制:每个域名(包含子域名)每 7 天不超过 5 个
Let’s Encrypt 验证方式有很多种,我使用的服务器已经运行了 nginx
且不想终止 web 服务,因此使用 webroot
模式。
webroot 模式
1 | ./letsencrypt-auto certonly --webroot --webroot-path /home/wwwroot/default -d caoxl.com --agree-tos --email code0809@163.com |
注意: /home/wwwroot/defaul
是项目根目录
certonly 模式
当然,除了 webroot
模式,也可以是用先手动只获取证书,然后配置 nginx
。
1 | service nginx stop |
唯一需要注意的一点就是,certonly
模式下获取证书需要先关闭 nginx。
获取成功后,会输出类似如下内容:
1 | Saving debug log to /var/log/letsencrypt/letsencrypt.log |
然后,注意上面成功提示信息中的证书和私钥文件的路径,用于接下来 nginx 的配置。
- 证书:
/etc/letsencrypt/live/caoxl.com/fullchain.pem
- 私钥文件:
/etc/letsencrypt/live/caoxl.com/privkey.pem
生成 dhparam
为了进一步提高安全性,建议为 nginx 再生成 2048 位 DH parameters
:
1 | openssl dhparam -out /etc/ssl/certs/dhparams.pem 2048 |
关于 DH Parameters 的解释,可以参考:What’s the purpose of DH Parameters?。
nginx 部署 ssl 证书
为域名配置 443 端口的虚拟主机
我的LNMP环境, 打开 /usr/local/nginx/conf/vhost/caoxl.com.conf
1 | # caoxl.com.conf |
http 强跳转 https(为同样的域名配置 80 端口的虚拟主机)
1 | # caoxl.com.conf |
重载配置后测试
1 | nginx -t |
然后访问 https://caoxl.com 和 http://caoxl.com,不出意外应该就能在浏览器地址栏最左端看到一把靠谱的小绿锁了。
证书续期
由于 Let’s Encrypt 颁发的服务器证书有效期只有 90 天,因此如果需要长期使用,就有必要设置好自动续期。
通过 letsencrypt-auto 工具,手动续期命令为:
1 | ./letsencrypt-auto renew |
而所谓的自动续期,就是自动定时执行上面手动获得证书的操作,对于 Linux 来说,可以使用 crontab。
1 | echo '@monthly root /path/to/letsencrypt-auto certonly --webroot --webroot-path /home/wwwroot/default -d caoxl.com --agree-tos --email code0809@163.com >> /var/log/letsencrypt/letsencrypt-auto-update.log' | tee --append /etc/crontab |
其他姿势
acme.sh
以 nginx 为例:
1 | # 0. 安装 acme.sh |
FAQ
- 已经成功安装配置好证书,部分 URL 仍然显示非安全?
显示非安全的 URL 页面里面含有 mixed content。
When you set a site to use SSL, all the contents, urls to load js, images, etc. should have https links instead of http.
简单来说:就是网站内包含不安全内容
- 使用 oneinstack 新建 vhost 时安装 let‘s Encrypt 提示:DNS problem: NXDOMAIN looking up A
这可能是因为在域名解析设置那里,给这个域名设置了 CDN 的 CNAME(出于加速静态文件和隐藏服务器真实地址的原因),而阿里云不能给相同主机头(RR)同时设置一个 CNAME 和 A 纪录,导致 Let’s Encrypt 检测不通过,只能删除 CNAME 纪录。
- To fix these errors, please make sure that your domain name was entered correctly and the DNS A/AAAA record(s) for that domain contain(s) the right IP address.
查看域名解析中 A
记录中的IP地址是否指向自己的服务器IP地址,且只能指向自己的服务器IP地址,不可多个指向.
我刚开始一直遇到这个问题就是因为的我A记录指向了两个不同的IP
此外,对于所申请 Let’s Encrypt 证书的域名来说,其顶级域名对应的 www 子域名必须要有一个 A 记录。可参考 LE 的该社区讨论:No WWW Record in DNS Means Certficate Cannot Be Issued。
- Problem binding to port 443: Could not bind to IPv4 or IPv6.. Skipping
执行证书续期命令的时候先,暂停 nginx,续期成功后再启动 nginx 即可。