申请Let's Encrypt免费SSL证书

1、安装前需要安装git和python环境,git用于下载letsencrypt项目脚本,python可以不装,执行脚本是会自动安装,安装git命令:

yum install git

检查下有没有占用80端口,不然执行脚本生成证书会报错,提示80端口被占用:

Performing the following challenges:
http-01 challenge for lichunjie.com
http-01 challenge for www.lichunjie.com
Cleaning up challenges
Problem binding to port 80: Could not bind to IPv4 or IPv6.
IMPORTANT NOTES:
- 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.

2、下载脚本并执行生成证书,将--email后面的admin@lichunjie.com改为自己的邮箱,-d后面参数为自己的域名

git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
./letsencrypt-auto certonly --standalone --email admin@lichunjie.com -d lichunjie.com -d www.lichunjie.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)gree/(C)ancel: (A)gree/(C)ancel: (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 our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y

脚本执行完成,看到如下界面表示申请成功,证书路径默认在/etc/letsencrypt/live/域名下

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

在nginx配置文件中配置证书:

ssl_certificate "/etc/letsencrypt/live/lichunjie.com/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/lichunjie.com/privkey.pem";

启动nginx即可。

证书默认3个月,3个月后需要手动执行脚本重新申请证书,也可以自己写脚本定期更新证书。

./letsencrypt-auto certonly --standalone --no-self-upgrade --renew-by-default --email admin@lichunjie.com -d lichunjie.com -d www.lichunjie.com