免费申请https网站ssl证书--支持通配符

众所周知从某些公司购买HTTPS证书,一个域名每年都要几千个大洋(不是一般的黑)。那么有没有免费,答案是有。现隆重推荐(Let’s Encrypt 免费证书
EEF 电子前哨基金会、 Mozilla 基金会和美国密歇根大学成立了一个公益组织叫 ISRG ( Internet Security Research Group ),这个组织从 2015 年开始推出了 Let’s Encrypt 免费证书。这个免费证书不仅免费,而且还相当好用,所以我们就可以利用 Let’s Encrypt 提供的免费证书部署 https 了。
Let’s Encrypt 证书除了免费,还支持域名通配符或泛域名。好东西必须分享,走起。

准备工作
安装Certbot
以 centos7 为例

Certbot 的官方网站是 https://certbot.eff.org/ ,打开这个链接选择自己使用的 web server 和操作系统,EFF 官方会给出详细的使用方法。

下载certbot


wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto

注意:也可采用certbot官方 yum安装方式

申请通配符域名

./certbot-auto certonly  -d *.example.com --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory

当然你也可以将多个泛域名,放到一个证书里(亲测可行),将example.com 换成你自己的域名。

./certbot-auto certonly  -d *.example.com  -d *.a.com  --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory

这里我只用了一个泛域域名

执行以上命令得到如下
这里写图片描述

依次 输入 你的邮箱
再输入“A” 同意,“Y” 开始
最后出现如下图,此处先不动,很重要
这里写图片描述

配置DNS
要求配置 DNS TXT 记录,从而校验域名所有权,也就是判断证书申请者是否有域名的所有权。
给 _acme-challenge.example.com 配置一条 TXT 记录.

测试txt记录是否生效

$ dig  -t txt  _acme-challenge.example.com @8.8.8.8    

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;_acme-challenge.example.com.        IN      TXT

;; ANSWER SECTION:
_acme-challenge.example.com. 599 IN  TXT     "kd9kdjjXH8nYZ2unEViIbW52LhIqxkg6i9mcwsRvhQ"

确认生效后,回车执行,输出如下:
这里写图片描述

恭喜您,证书申请成功,证书和密钥保存在下列目录:

ll  /etc/letsencrypt/live/example.com

lrwxrwxrwx 1 root root  35 Jun  1 14:43 cert.pem -> ../../archive/duiniya.com/cert1.pem
lrwxrwxrwx 1 root root  36 Jun  1 14:43 chain.pem -> ../../archive/duiniya.com/chain1.pem
lrwxrwxrwx 1 root root  40 Jun  1 14:43 fullchain.pem -> ../../archive/duiniya.com/fullchain1.pem
lrwxrwxrwx 1 root root  38 Jun  1 14:43 privkey.pem -> ../../archive/duiniya.com/privkey1.pem

然后校验证书信息,输入如下命令:

openssl x509 -in  /etc/letsencrypt/archive/example.com/cert1.pem -noout -text 

如果输出中有

X509v3 Subject Alternative Name: 
    DNS:*.example.com

证明成功

配置Nginx

    server {  
           server_name  www.example.com;
           listen 443 http2 ssl;
           ssl on;
           ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
           ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        #access_log  logs/host.access.log  main;
        root   /var/www/demo;
        location / {
            index  index.html index.htm index.php l.php;
           autoindex  off;

        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php(.*)$  {
              fastcgi_pass 127.0.0.1:9000;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
        }
    }

证书续期

一般Let’s Encrypt 证书有效期为3个月,如果想续期执行如下命令

./certbot-auto renew

如果嫌麻烦,可以写到定时任务里,每天执行一次。

crontab -e

0 0 * * * /root/tar/certbot-auto renew --renew-hook "systemctl reload nginx"

续期说明:只用renew的话,会先检查证书是否需要更新,大概是距离到期还有三天或者十几天之内才会执行更新,否则会提示不需要更新。(昨天更新了证书,今天直接用renew,提示不允许更新)
注意:人总有犯晕的时候,比如你没有放开443端口,还想访问HTTPS网站。

猜你喜欢

转载自blog.csdn.net/guyan0319/article/details/80859830