免费申请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

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

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

申请通配符域名

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

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

1
./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记录:

测试txt记录是否生效

1
2
3
4
5
6
7
8
9
$ 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"

确认生效后,回车执行,输出如下:

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

1
2
3
4
5
6
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

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

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

如果输出中有

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

证明成功
也可以通过

1
./certbot-auto certificates

配置Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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个月,如果想续期执行如下命令

1
./certbot-auto renew

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

1
2
3
crontab -e

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

续期说明:只用renew的话,会先检查证书是否需要更新,大概是距离到期还有三天或者十几天之内才会执行更新,否则会提示不需要更新。(昨天更新了证书,今天直接用renew,提示不允许更新)
注意:人总有犯晕的时候,比如你没有放开443端口,还想访问HTTPS网站。
错误解决
如果执行续订,出现
ReadTimeoutError: HTTPSConnectionPool(host=‘files.pythonhosted.org’, port=443): Read timed out.
这个是pip下载国内站点超时或被限造成的,可通过配置pip.conf解决。

1
2
3
4
5
6
vim /root/.pip/pip.conf
#内容
[global]
timeout = 6000
index-url = http://e.pypi.python.org/simple
trusted-host = pypi.douban.com

同时给大家推荐比较好的免费获取HTTPS证书的开源的项目,安装简单,自动续订。
https://github.com/Neilpang/acme.sh