西梧 runtime

如何免费申请 SSL 证书

  • 2023-05-16 17:10:29
  • qtsunami

如果想要在自己的网站启用 HTTPS,我们需要从证书颁发机构(CA)获取证书,并部署到自己的服务器上,而这张证书则被称为 SSL 证书。

为什么要使用 SSL 证书?

HTTP协议无法加密数据,数据传输可能产生泄露、篡改或钓鱼攻击等问题,而启用HTTPS后,可帮助Web服务器和网站间建立可信的HTTPS协议加密链接,为您的网站安全加锁,保证数据安全传输。 另外,像微信小程序、支付宝小程序等,在我们配置项目域名时,也都需要使用 https。所以,有时候使用ssl证书也是一种强制行为。

获取SSL证书的渠道

既然 SSL 证书能提升安全性,我们当然可以部署SSL证书了,获取ssl证书的渠道有以下两种方式:

  • 各云服务厂商获取,诸如阿里云、腾讯云、华为云等
  • 使用 Let's Encrypt 生成 SSL 证书

SSL 证书有三种类型:

  • DV 证书:一般用于个人网站或测试使用,公信等级一般
  • OV 证书:企业级SSL证书,适用于企业、政府组织、教育机构等,公信等级比较高
  • EV 证书:企业增强型 SSL 证书,适用于大型企业、金融机构,公信等级最高,而且认证强度最为严格。

所以, 证书等级越高,需要的费用也就越高。而目前,SSL 证书最长有效期为一年(之前为两年),需要每年进行购买与替换。

当然,也有免费获取证书的渠道。

我们可以通过阿里云申请免费的证书,注意,通过阿里云申请免费的SSL证书,无法申请通配符域名证书,只能申请单域名证书,而且每年限制申请20张,每张证书有效期为1年,不过对于个人来说应该是绰绰有余了。

如果我们即不想付费,还想申请通配符域名证书,就需要通过 Let's Encrypt 来生成自己的通配符域名。Let's Encrypt 是免费、开放和自动化的证书颁发机构。由非盈利组织互联网安全研究小组(ISRG)运营。

不过 Let's Encrypt 生成的 SSL 证书有个限制,证书有效期为 3 个月,所以需要格外注意,定时生成证书和替换。

Let's Encrypt 生成证书

工具安装

curl -o /etc/yum.repos.d/epel-7.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum install -y certbot

生成证书操作

certbot certonly  -d *.dev.evente.cn --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
  • certonly,表示安装模式,Certbot 有安装模式和验证模式两种类型的插件
  • --manual 表示手动安装插件,Certbot 有很多插件,不同的插件都可以申请证书,用户可以根据需要自行选择
  • -d 为那些主机申请证书,如果是通配符,输入 *.dev.evente.cn
  • --preferred-challenges dns,使用 DNS 方式校验域名所有权
  • --server,Let's Encrypt ACME v2 版本使用的服务器不同于 v1 版本,需要显示指定

验证证书操作

openssl x509 -in  /etc/letsencrypt/live/dev.evente.cn/cert.pem -noout -text 

#X509v3 Subject Alternative Name:
#      DNS:*.dev.evente.cn

将证书复制应用即可

ssl on;
ssl_certificate /etc/letsencrypt/live/dev.evente.cn/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/dev.evente.cn/privkey.pem;

执行流程

[root@mosh-dev-1 cert]# certbot certonly  -d *.dev.evente.cn --manual \
> --preferred-challenges dns \
> --server https://acme-v02.api.letsencrypt.org/directory
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): tianjingwen@eventmosh.com
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, 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: N
Account registered.
Requesting a certificate for *.dev.evente.cn
Performing the following challenges:
dns-01 challenge for dev.evente.cn

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.dev.evente.cn with the following value:

cXsppHLitr-_9SL4RoPq8A5A2fvIYJlNILsJAPqSjHc

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
Waiting for verification...
Cleaning up challenges

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

通过上面流程,我们就可以生成一张自己域名的 SSL 证书了,然后将证书部署到我们的服务器即可使用了。

参考文章:

© 2023 By 西梧Runtime.    本站博客未经授权禁止转载   |   京ICP备15032626号-1