小坚的技术博客

LNMP下配置免费SSL安全证书(https)

本文作者:陈进坚
博客地址:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
简书:https://www.jianshu.com/u/8ba9ac5706b6
联系方式:jian1098@qq.com

环境要求

  • LNMP
  • 域名

安装依赖

1
2
3
4
$ cd /root/
$ wget https://dl.eff.org/certbot-auto --no-check-certificate
$ chmod +x ./certbot-auto
$ ./certbot-auto -n

生成证书

注意将 www.vpser.net 替换成你的域名

单域名生成证书:

1
$ ./certbot-auto certonly --email youemail@vpser.net --agree-tos --no-eff-email --webroot -w /home/wwwroot/www.vpser.net -d www.vpser.net

多域名单目录生成单证书:(即一个网站多个域名使用同一个证书)

1
$ ./certbot-auto certonly --email youemail@vpser.net --agree-tos --no-eff-email --webroot -w /home/wwwroot/www.vpser.net -d www.vpser.net -d bbs.vpser.net

多域名多目录生成一个证书:(即一次生成多个域名的一个证书)

1
$ ./certbot-auto certonly --email youemail@vpser.net --agree-tos --no-eff-email --webroot -w /home/wwwroot/www.vpser.net -d www.vpser.net -d bbs.vpser.net -w /home/wwwroot/lnmp.org -d www.lnmp.org -d lnmp.org

提示以下信息表示生成成功

1
2
3
4
5
6
7
8
9
10
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/www.vpser.net/fullchain.pem. Your cert will
expire on 2016-10-01. To obtain a new or tweaked version of this
certificate in the future, simply run certbot-auto again. To
non-interactively renew *all* of your certificates, run
"certbot-auto renew"
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

生成的证书会存在:/etc/letsencrypt/live/www.vpser.net/ 目录下,检查是否有两个.pom的证书文件

修改配置

链接:https://pan.baidu.com/s/1HC1ErFjtoDe8akzzWys29A 密码:i2fi

  • 上传至/usr/local/nginx/conf/vhost 并替换原来的文件

  • 执行:/etc/init.d/nginx reload 重新载入配置使其生效

到这里你的域名应该就已经变成https了

证书续期

因为证书只有90天,所以建议使用crontab进行自动续期,间隔5天就可以了,不能太频繁

1
2
$ crontab -e
0 3 */5 * * /root/certbot-auto renew --disable-hook-validation --renew-hook "/etc/init.d/nginx reload"

本文参考:https://www.vpser.net/build/letsencrypt-free-ssl.html

-------------本文结束感谢您的阅读-------------
🐶 您的支持将鼓励我继续创作 🐶