Certbot让网站拥有免费https证书

certbot certonly --webroot -w /var/www/html/awaimai -d awaimai.com -d www.awaimai.com

网站使用http协议,在chrome浏览器中总是报不安全,看着就让人不爽,自己建的网站,不安全总是会让自己心慌慌。看到有头有脸的网站都是https开头,心中自然也想装逼一把,让自己的网站高端大气上档次。搞一搞吧!Let's Encrypt(certbot)!

Let's Encrypt是一个免费的SSL证书发行项目,自动化发行证书,证书有90天的有效期,适合我们个人或者临时使用。certbot是Let's Encrypt发布的新的工具,生成证书的使用方法和证书续签就变得更加简单了。但是目前看certbot在一些老版本的Linux发行版上的兼容性还是有问题的,特别是在CentOS 5上因为python版本过低是无法用的,CentOS 6上需要先安装epel才行,当然也有很多第三方的工具你也可以自己去尝试一下。 

运行环境centos7.4 nginx 1.14.0

1、安装certbot客户端

[root@izuf68g6a94fj32w0afx00z nginx]# yum install certbot
Loaded plugins: fastestmirror
base                                                                                                                                                                                        | 3.6 kB  00:00:00     
epel                                                                                                                                                                                        | 3.2 kB  00:00:00     
extras                                                                                                                                                                                      | 3.4 kB  00:00:00     
nginx                                                                                                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                                                                                                     | 3.4 kB  00:00:00     
(1/4): extras/7/x86_64/primary_db                                                                                                                                                           | 156 kB  00:00:00     
(2/4): epel/x86_64/updateinfo                                                                                                                                                               | 937 kB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                                                                                                                                          | 1.3 MB  00:00:00     
(4/4): epel/x86_64/primary                                                                                                                                                                  | 3.6 MB  00:00:00     
Loading mirror speeds from cached hostfile
epel                                                                                                                                                                                                   12738/12738
Package certbot-0.27.1-1.el7.noarch already installed and latest version
Nothing to do

我已经安装过了。

2、获取证书

certbot certonly --webroot -w /home/zengfp/www/blog/public -d zengfanping.com -d www.zengfanping.com

这个命令会为 zengfanping.com  和 www.zengfanping 这两个域名生成一个证书。

使用 --webroot 模式会在 /home/zengfp/www/blog/public 中创建.well-know文件夹。

这个文件夹里面包含了一些验证文件,certbot 会通过访问 example.com/.well-known/acme-challenge 来验证你的域名是否绑定的这个服务器。

证书生成完毕后,我们可以在/etc/letsencrypt/live/目录下看到对应域名的文件夹,里面存放了指向证书的一些快捷方式。

3、standalone模式

上面--webroot这个命令在大多数情况下都可以满足需求。

但是有些时候我们的一些服务并没有根目录,例如一些微服务,这时候使用 --webroot 就走不通了。

certbot 还有另外一种模式--standalone , 这种模式不需要指定网站根目录,他会自动启用服务器的443端口,来验证域名的归属。

我们有其他服务(例如nginx)占用了443端口,就必须先停止这些服务,在证书生成完毕后,再启用。

certbot certonly --standalone -d example.com -d www.example.com

4、获取证书出错

类似的如下:

IMPORTANT NOTES:

The following errors were reported by the server:

Domain: www.zengfanping.com
Type: connection
Detail: Fetching
http://www.zengfanping.com/.well-known/acme-challenge/xOiPmxEoy3bm8SZkNOskBj83OPOCmRHN6vvIQFSL-8I: 2
connection refused

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. 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.

这个时候你就需要检查自己的项目目录是否正确,域名解析是否正确,如果你在你的server_name处写了多个域名,就需要对每个域名都进行解析。还有你的项目路径一定要正确

server {
      listen       80 default_server;
      server_name  zengfanping.com  www.zengfanping.com;
      rewrite ^/(.*) https://www.zengfanping.com/$1 permanent;
      root /home/zengfp/www/blog/public;
      index index.html;
      location ~ /.well-known {
            allow all;
      }
    #  location / {
#    root  /home/zengfp/www/blog/public;
#    index  index.html  index.htm;
 #     }
      access_log  /var/log/nginx/host.access.log  main;
      error_log  /var/log/nginx/host.error.log;
}

在进行--webroot这个模式获取证书的时候,我把我的nginx服务关了,一直获取不来证书,而且一直报上面那个错误。报错之后还是需要点耐心的,因为如果你一直重复上面那个获取证书的命令,就会提示你获取证书太频繁,耐心啊。最后我把nginx服务启动之后,再次获取证书,就成功了。本人的一次坑,希望伙伴们不要走。

5、启用https

本人配置:

server {
      listen       80 default_server;
      server_name  zengfanping.com  www.zengfanping.com;
      rewrite ^/(.*) https://www.zengfanping.com/$1 permanent;
      root /home/zengfp/www/blog/public;
      index index.html;
      location ~ /.well-known {
            allow all;
      }
    #  location / {
#    root  /home/zengfp/www/blog/public;
#    index  index.html  index.htm;
 #     }
      access_log  /var/log/nginx/host.access.log  main;
      error_log  /var/log/nginx/host.error.log;
}
server {
        listen       443 ssl;
        ssl          on;
        server_name  www.zengfanping.com;
        root         /home/zengfp/www/blog/public;
        index        index.html;

        ssl_certificate /etc/letsencrypt/live/www.zengfanping.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.zengfanping.com/privkey.pem;

    
        location ~ /\.ht {
            deny all;
        }

}

上面的配置中有个rewrite,就是让输入上面域名中的任何一个,都让其指向 https://www.zengfanping.com 这个地址。

最终效果:

6、跨过每一个小坑,让自己每天都进步一点点!您的点赞是我前进的鼓励!希望你不要吝啬哦。个人博客网址:https://www.zengfanping.com。不吝赐教

猜你喜欢

转载自www.cnblogs.com/zengfp/p/10107496.html