您的位置:首页 > 理论基础 > 计算机网络

免费HTTPS证书不是梦!在Ubuntu(Linux)的VPS上使用Let's Encrypt为一堆域名申请并安装HTTPS证书

2017-12-14 16:46 1096 查看

免费HTTPS证书不是梦!在Ubuntu(Linux)的VPS上使用Let's Encrypt为一堆域名申请并安装HTTPS证书

情景:

你拥有一个VPS,能使用命令行ssh登陆到服务器,已经安装好了apache等httpd服务器,

你拥有一个(或多个)域名正在使用。

你需要为这些域名申请https证书,启用全站https。

须知(个人理解):

0. 一个lets encrypt证书可以包含多个域名(例如demo.com和download.demo.com),但不能作为“泛域名”使用,例如*.demo.com

1. certbot是lets encrypt的一个申请证书的工具,证书是由lets encrypt这个证书中心(CA)颁发。

2. certbot不是常驻内存的服务(Service)或者后台脚本,只是一次行执行的工具,需要手动启动certbot逐个申请证书,或者搭配crontab -e来定时启动certbot以进行证书更新。

3. certbot有多种申请证书的方法,包括:

   自动模式(使用命令certbot --apache -d example.com -d www.example.com -d other.example.net 然后它会帮你申请证书并且安装到apache并且启用这个证书)

   手动模式(使用命令certbot certonly --webroot -w /var/www/cms/ -d example.com -d www.example.com 这个命令只是帮你申请证书,并不会帮你在服务器例如apache设置这个域名的https证书,需要自己弄后续的https)

4. 不能一次性为所有的域名(上百个)申请同一张https证书,有限制,需要手动指定某些域名

步骤:

0. 打通http://demo.com/

1. ssh到linux远程服务器

2. 安装certbot,https://certbot.eff.org/docs/install.html
sudo apt-get update
sudo apt-get install certbot python-certbot-apache
3. 准备材料

域名 = demo.com, www.demo.com, blog.demo.com

路径 = /var/www/demo/

4. 申请证书
certbot certonly --webroot -w /var/www/demo/ -d demo.com -d www.demo.com -d blog.demo.com
提示成功:
Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/demo.com/fullchain.pem
...
5. 安装证书

6. 设置定时任务,自动更新证书:
sudo -i
crontab -e
0 0 1 * * certbot renew --post-hook "apache2ctl restart"
其中,post-hook的参数值是希望renew成功后 自动重启apache服务器,这里需要大家根据自己的情况修改下。

另外:letsencrypt证书有效期都是三个月,我们希望每个月1号凌晨都检查所有的证书,并自动更新证书。

常用命令:

certbot certificates
certbot --apache
certbot --nginx
certbot certonly
certbot certonly --webroot -w /var/www/cms/ -d example.com -d www.example.com
certbot certonly --webroot -w /var/www/cms/ -d example.com -d www.example.com --deploy-hook /path/to/script/update-apache.sh
certbot renew
certbot renew --dry-run
certbot renew --pre-hook "service nginx stop" --post-hook "service nginx start"
certbot --apache -d example.com -d www.example.com -d other.example.net
certbot certonly --cert-name example.com

su root
crontab -e
* 4 * * 3 /root/scripts/certbot/auto-update-vhost-conf.sh
certbot --apache (这是apache专用命令,会自动搜索vhost虚机信息,由你选择后进行申请安装证书,并自动在apache里添加其域名的https配置文件,适用于域名较少的情况)

错误:

1. 配置错误
您的连接不是私密连接 NET::ERR_CERT_COMMON_NAME_INVALID
此服务器无法证明它是demo1.com;其安全证书来自demo2.com。出现此问题的原因可能是配置有误或您的连接被拦截了。
解决办法: 需要自己配置这个域名的https信息,包括域名项目文件的路径,https证书的位置(3个),端口,IP等等

2. 申请失败
IMPORTANT NOTES:
- The following errors were reported by the server:

Domain: www.ultimatemag.net
Type:   connection
Detail: Could not connect to http://www.xxx.net/.well-known/acme-challenge/Gs8QJJuSHYPTjbNVGDuIBHQmwCClGgsbRmxkWTvepCo 
Domain: ultimatemag.net
Type:   connection
Detail: Could not connect to http://xxx.net/.well-known/acme-challenge/n3jevLcCJnH32SgOgLxlt0G1hjTeWEy2uuDlOZ46zuE 
To fix these errors, please make sure that your domain name was
entered correctly and the DNS A record(s) for that domain
contain(s) the right IP address. Additionally, please check that
your computer has a publicly routable IP address and that no
firewalls are preventing the server from communicating with the
client. If you're using the webroot plugin, you should also verify
that you are serving files from the webroot path you provided.
解决办法: 保证http://xxx.net/可以访问,再登陆域名提供商(例如万网),检查其DNS设定,再试一遍。

延伸阅读:

certbot/certbot
https://github.com/certbot/certbot/

User Guide for certbot
https://certbot.eff.org/docs/using.html

Linux定时任务Crontab命令详解
https://www.cnblogs.com/intval/p/5763929.html

使用 Certbot + Let's Encrypt 小步快跑的奔向 Https
https://laravel-china.org/topics/2525/using-certbot-lets-encrypt-small-step-run-towards-https

Let's Encrypt 给网站加 HTTPS 完全指南certbot
http://blog.csdn.net/cstopery/article/details/51911298

手把手教你在Nginx上使用CertBot
https://segmentfault.com/a/1190000005797776

利用Certbot工具快速给网站部署Let's Encrypt免费SSL证书
http://www.laozuo.org/9502.html

使用certbot为你的网站免费上https
http://www.jianshu.com/p/fb3733cd0d3b

shell script error [: 12: unexpected operator?
https://stackoverflow.com/questions/14934185/shell-script-error-12-unexpected-operator

Certbot and --apache modules looks for vhost conf files in sites-available causing missconfigurations in edge cases
https://github.com/certbot/certbot/issues/3791

linux shell脚本自动检测apache服务是否正常的诡异问题
http://blog.csdn.net/wangjunjun2008/article/details/50680381

[shell]对
4000
apache进程进行检测的脚本
http://blog.csdn.net/yagas/article/details/6748742

判断文件是否存在的shell脚本代码
http://www.jb51.net/article/34330.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息