5.申请Let’s encrypt通配符证书

为什么需要证书,为了通迅过程安全!
哪里必要使用,浏览器和反代端用信任证书,反代与上游可以用自创证书。

利用certbot工具配置Let’s encrypt通配符证书,
所有的子域名都能方便的使用https证书,而且免费。
此通配符证书只是针对子域名
如*.freebasic.cn和freebasic.cn
被认为是两个域名,两个都要申请。

配置环境
操作系统:Ubuntu16.04 LTS 18.04
配置域名:freebasic.cn,*.freebasic.cn
1. 获取Certbot
# 下载,这就是官方的脚本,虽然丑
wget https://dl.eff.org/certbot-auto
# 设为可执行权限
chmod u+x certbot-auto

2.申请证书过程

配置认证有两种方式,一种是通过验证域名所有者,二是验证服务器所有者,二选一。

认证方式一:域名所有人有效(通过添加域名TXT记录)
./certbot-auto certonly -d "*.freebasic.cn" -d "freebasic.cn" --manual --preferred-challenges dns-01 --server https://acme-v02.api.letsencrypt.org/directory --email=cnfreebasic@hotmail.com

经过一堆的下载安装,其中有次确认Y,安装完后各种确认
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): cnfreebasic@hotmail.com

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: 我选 A

-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: 我选 Y
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for freebasic.cn
dns-01 challenge for freebasic.cn

-------------------------------------------------------------------------------
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
————————————————
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.freebasic.cn with the following value:

5JNDWusssA0bTBzAWCCv8O8hrSNbcpsvOxrZOgsGJgE

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

查询 _acme-challenge.freebasic.cn txt记录命令
nslookup -qt=txt _acme-challenge.freebasic.cn

认证方式二:服务器创文件
配置nginx
修改你的服务器配置,在server模块添加:
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /usr/share/nginx/html;
}

注:
因为,CertBot在验证服务器域名的时候,会生成一个随机文件,然后CertBot的服务器会通过HTTP访问你的这个文件,因此要确保你的Nginx配置好,以便可以访问到这个文件。
location = /.well-known/acme-challenge/ {
return 404;
}

接着重新加载Nginx配置:
sudo service nginx reload
# 运行之前,请确保你的域名是可访问的
./certbot-auto certonly --webroot -w /usr/share/nginx/html/ -d "*.freebasic.cn" -d "freebasic.cn" --agree-tos --email=cnfreebasic@hotmail.com

成功以后提示
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/freebasic.cn/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/freebasic.cn/privkey.pem
Your cert will expire on 2018-08-12. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again. To non-interactively renew *all* of your certificates, run
"certbot-auto renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- 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
————————————————

相关文件会默认保存到/etc/letsencrypt/live/ 文件夹下
/etc/letsencrypt/live/freebasic.cn

nginx配置ssl证书方法

server_name freebasic.cn;
listen 443 http2 ssl;
# ssl on # 新版nginx已经不使用这个定义;
ssl_certificate /etc/letsencrypt/live/freebasic.cn/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/freebasic.cn/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/freebasic.cn/chain.pem;

关于证书自动更新
Let’s encrypt 的免费证书默认有效期为 90 天,到期后如果要续期可以执行:
crontab -e
./certbot-auto renew
0 2 * * * sudo ./certbot-auto -q renew
0 2 * * * sudo /root/certbot-auto -q renew
sudo service cron restart