🦄 2024 独立开发者训练营,一起创业!查看介绍 / 立即报名 →

Let's Encrypt:用免费的 SSL 证书,让网站支持 HTTPS

申请 Let's Encrypt 提供的免费 SSL 证书,配置网站使用 HTTPS,让网站应用更安全。直接在运行网站的服务器上去申请 SSL 证书更方便一些,你也可以在一台服务器上去为在其它服务器上运行的网站申请 SSL 证书,这需要做额外的验证。操作都非常简单,不用紧张:)

我在一台 CentOS  7 服务器上申请了 Let's Encrypt 签发的 SSL 证书,我的网站的 Web 服务器用的是 Nginx

安装 EPEL 仓库

在 CentOS 的 EPEL 仓库里可以找到签发 Let's Encrypt 证书用的工具。所以先安装一下这个仓库,执行:

sudo yum install epel-release -y

安装签发证书工具

用 Certbot 工具申请 Let's Encrypt 证书,先安装一下:

sudo yum install certbot-nginx

运行 certbot 工具时报错:

ImportError: No module named 'requests.packages.urllib3'

执行下面的命令解决了上面的问题:

pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3

申请证书

网站运行在申请 SSL 证书用的服务器上

你要申请 SSL 证书的主机名已经指向了申请用的服务器的 IP 地址,在服务器上用的是 Nginx Web 服务器。保证 Nginx 正在运行,并且已经配置好了 Nginx ,绑定上了要申请 SSL 证书的主机名。就是你应该在 Nginx 的配置里面,有一个配置是为要申请 SSL 证书的主机名准备的。

一开始我们在服务器上安装的 Certbot 可以自动修改指定主机名的 Nginx 配置,为主机名申请证书,下载证书,并且会自动修改 Nginx 的配置,让它支持 HTTPS。

执行:

sudo certbot --nginx

Certbot 会自动查找当前服务器上的 Nginx 里面的虚拟主机。提示:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org

Which names would you like to activate HTTPS for?
-------------------------------------------------------------------------------
1: ninghao.co
2: ninghao.org
-------------------------------------------------------------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):

比如我的服务器上有两个 Nginx 虚拟主机,上面会提示想给哪个主机名申请 SSL 证书,输入序号选择,或留为空白直接回车选择所有。验证成功,Let's Encrypt 就会立即给你签发 SSL 证书,并且会自动修改主机的 Nginx 配置,让它支持 HTTPS。过程中会提示:

Deployed Certificate to VirtualHost /etc/nginx/conf.d/ninghao.org.conf for set(['ninghao.org'])

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

意思是,想不想把 HTTP 的请求重定向到 HTTPS ,如果选择 Redirect,Certbot 会自动在主机的 Nginx 配置里添加重定向用的配置。成功以后会提示:

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://ninghao.org

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=ninghao.org
-------------------------------------------------------------------------------

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/ninghao.org/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/ninghao.org/privkey.pem
   Your cert will expire on 2018-03-12. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot 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

上面会告诉你申请的 Let's Encrypt 证书保存的位置,一般是在:

/etc/letsencrypt/live/主机名

再打开网站的 Nginx 配置检查一下,配置里会有一些东西是 Certbot 自动添加的,配置的后面都会有注释 managed by Certbot

server {
  listen       80;
  server_name  ninghao.org;
  root         /mnt/web;
  index        index.php index.html index.htm;

  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/ninghao.org/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/ninghao.org/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


  if ($scheme != "https") {
    return 301 https://$host$request_uri;
  } # managed by Certbot
}

如果你只想为跟 Nginx 绑定的主机名申请证书,不想让 Certbot 修改 Nginx 的配置,可以执行:

sudo certbot --nginx certonly

网站运行在申请 SSL 证书以外的服务器上

我们在一个 CentOS 服务器上,也可以为运行在其它服务器上的网站申请 Let's Encrypt 证书,只需要多做一步验证。

执行:

sudo certbot certonly --manual

提示让你输入想要申请证书的主机名:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): ninghao.net

继续又会提示要记录服务器的 IP 地址,是不是同意这样:

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for ninghao.net

-------------------------------------------------------------------------------
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
-------------------------------------------------------------------------------
(Y)es/(N)o: Y

下面要做一步验证,把一串字符放在指定的文件里,而且要用指定的路径可以访问到这个文件,这样就可以完成验证了。

Create a file containing just this data:

KIad2acPdKiF5pLpjlv4fK19PJDchhO7ClJZI_P3zbw.dfvuyqod4KGCve8xM-wotdjimoIXlfb6a4dkaSFN7LI

And make it available on your web server at this URL:

http://ninghao.net/.well-known/acme-challenge/KIad2acPdKiF5pLpjlv4fK19PJDchhO7ClJZI_P3zbw

-------------------------------------------------------------------------------
Press Enter to Continue

一切正常,验证通过就会签发证书:

Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/ninghao.net/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/ninghao.net/privkey.pem
   Your cert will expire on 2018-03-12. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot 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

结语

宁皓网之前用的是在阿里云申请的免费的赛门铁克的证书,最近他们在做业务调整,之前申请的证书不能续约,得等到本月 17 号,不过我的证书在  14 号就过期了,所以就考虑用了 Let's Encrypt 的证书。另外我在调试应用的时候,在 Chrome 浏览器的控制台上看到,说他们将不信任赛门铁克签发的 SSL 证书了。

HTTPS

评论

之前赛门铁克的免费 DV 证书出现过乱签发的情况,已经被各大浏览器厂商列为不信任的状态。但是赛门铁克的证书签发业务已经转让给 DigiCert 了,所以要做调整。另外,360 旗下的 WoSign(沃通)也是不被信任的,各位注意及时替换。

另外 sudo certbot certonly --manual 这个手动签发的方法可以隔着 CDN 来操作。

还有,Let's Encrypt 的证书有效期只有 90 天,需要手动续期,或者在服务器内设置定时任务更新证书。

sudo crontab -u root -e
0 0 */85 * * certbot renew
sudo systemctl reload crond

还有,可以在这里评估开启 HTTPS 后网站的安全性:https://myssl.com

非常好 :)

[root@izj6cg6uh803qyzgft7zgjz /]# certbot --nginx
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Error while running nginx -c /etc/nginx/nginx.conf -t.

nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

您这台服务器上有没有安装 Nginx?

上面提示说没找到 /etc/nginx/nginx.conf 这个配置文件。有可能您是用的一键安装的运行环境,安装的 Nginx 的配置文件不在 /etc/nginx/nginx.conf 。这样,你可以使用手工申请 SSL 证书的方法。参考文章中 “网站运行在申请 SSL 证书以外的服务器上” 这块内容。这种方法需要做一步额外的验证,Certbot 会提示你一串随机字符,你要把它保存在一个文件里,放在申请证书的网站的某个特定的路径下面,这个路径 Certbot 也会给你提示。这样在验证的时候 Certbot 会请求这个路径,发现文件里的内容跟它给出的提示匹配,就会通过验证,然后为你签发 SSL 证书。

好的 我试试,我的是阿里云ecs服务器选的一个环境,linux+nginx的
配置文件在/www/server/nginx/conf/nginx.conf
虚拟机的配置文件在/www/server/panel/vhost/nginx

好的,我下午去试试,有问题再贴出来,非常感谢

Failed authorization procedure. www.wangyuanhao.xyz (http-01): urn:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://www.wangyuanhao.xyz/.well-known/acme-challenge/ErBGigyUWptd_wz7yO... "<pre>An Error occurred while handling another error:
exception &#039;yii\base\InvalidRouteException&#039; with message &#039;Una"

推荐两个小工具:
① acme.sh - https://github.com/Neilpang/acme.sh
特点: 可申请 ECC,支持 Automatic DNS API 验证
文档:https://github.com/Neilpang/acme.sh/wiki/说明
--
②acme-tiny.py - https://github.com/diafygi/acme-tiny
特点: 轻 ( 200行代码 ),可申请 ECC,就做了最后一步 http 验证

http://ninghao.net/.well-known/acme-challenge/KIad2acPdKiF5pLpjlv4fK19PJ...我想问一下,这个.well-known一定要有这个点
‘.’吗?

当然,必须得有点。

我试试有什么办法弄个点,标准的控制器路由,有点的话,是不是用重写规则来

不用路由,直接创建一个带点的目录就行了。

我就直接贴出来了,报错
Press Enter to Continue
Waiting for verification...
Cleaning up challenges
Failed authorization procedure. wangyuanhao.xyz (http-01): urn:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://wangyuanhao.xyz/.well-known/acme-challenge/idLK1eKCpC0gspBFSkq-HI... "<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">

.well-known/acme-challenge/KIad2acPdKiF5pLpjlv4fK19PJDchhO7ClJZI_P3zbw,在网站下面创建一个目录名字叫 .well-known,在这个目录下面再创建一个目录叫 acme-challenge,在这个 acme-challenge 目录下面创建一个文件叫 KIad2acPdKiF5pLpjlv4fK19PJDchhO7ClJZI_P3zbw,在这个文件里面把 Certbot 提示的字符串放进去保存一下就行了。

Waiting for verification...
Cleaning up challenges
Failed authorization procedure. www.wangyuanhao.xyz (http-01): urn:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://www.wangyuanhao.xyz/.well-known/acme-challenge/ErBGigyUWptd_wz7yO... "<pre>An Error occurred while handling another error:

还是这样。都能访问 ,但是还是抱错

我访问 http://www.wangyuanhao.xyz/.well-known/acme-challenge/ErBGigyUWptd_wz7yO... 是报错的。

我看看是不是我屏蔽了访问,谢谢您的提醒

难道xyz后缀的不支持?

不是,是你配置验证路径不对,网站报错,访问指定的路径并没有返回 Certbot 要求的字符串。在浏览器上打开:http://www.wangyuanhao.xyz/.well-known/acme-challenge/ErBGigyUWptd_wz7yO... ,正常应该返回 Certbot 要求的字符串,但是您现在这个路径并不正常:)

我访问这个url的时候是可以访问,并且还是有他的字符串密钥的
http://www.wangyuanhao.xyz/.well-known/acme-challenge/ErBGigyUWptd_wz7yO...
我访问这个路径,会是一个页面,我里面里面就写了这个字符串,别的没有写任何东西,页面中难道有其他的写法?不是直接把字符串写进去吗?

我在访问时提示:An Error occurred while handling another error:
exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request: site/error' in /www/wwwroot/www.wangyuanhao.xyz/vendor/yiisoft/yii2/base/Controller.php:128
Stack trace:

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.wangyuanhao.xyz/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.wangyuanhao.xyz/privkey.pem
Your cert will expire on 2018-03-13. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run

直接生成成功了,但是访问的时候还是没有出那个绿色的?我需要配置什么吗?谢谢了

如果在申请证书的时候没让 Certbot 修改你的 Nginx 配置,有了证书以后,需要再配置一下 Nginx,让它使用刚刚申请的 SSL 证书,配置在这篇文章里可以找到。检查一下你的 Nginx 配置文件,看看里面是不是有 Certbot 添加的配置信息。如果有,并且你的网站能通过 HTTPS 访问到,那应该就是配置成功了。

好的 非常感谢 如此耐心

别客气:)

有效期是3个月,怎么自动续?

参考第一条评论:)

之前域名安装过这个证书,但是后来把系统回滚了一下,就没有证书了. 要怎么办?

能重新再申请吗?

目前还不可以.. ): 还没过期.

我查了一下,应该可以重新生成。

好的,感谢.

微信好友

用微信扫描二维码,
加我好友。

微信公众号

用微信扫描二维码,
订阅宁皓网公众号。

240746680

用 QQ 扫描二维码,
加入宁皓网 QQ 群。

统计

14696
分钟
0
你学会了
0%
完成

社会化网络

关于

微信订阅号

扫描微信二维码关注宁皓网,每天进步一点