slot deposit pulsa slot mahjong slot gacor slot gacor slot gacor resmi slot gacor 2025 slot gacor terpercaya slot gacor 2025 slot gacor hari ini slot gacor hari ini slot gacor hari ini
给你的网站添加免费的 SSL 证书
17611538698
webmaster@21cto.com

给你的网站添加免费的 SSL 证书

运维 1 407 2025-03-20 05:18:28

图片

背景

之前网站使用的是免费证书,但是免费证书(默认证书)的签发有效期由12个月缩短至3个月,很快就到期,这是要让大多数企业购买付费证书的节奏啊,所以我更换了证书的签发机构。本文主要讲的就是如何使用 Let’s Encrypt的证书让自己的网站免费从HTTP升级为HTTPS。 

目前比较流行的免费 SSL 证书有 Let’s encrypt 和 Cloudflare。这次我们先尝试一下 Let’s encrypt。

Let's Encrypt 是一个由非营利性组织 互联网安全研究小组(ISRG)提供的免费、自动化和开放的证书颁发机构(CA)。

图片


Let's Encrypt 简介

简单的说,借助 Let's Encrypt 颁发的证书可以为我们的网站免费启用 HTTPS(SSL/TLS) 。Let’s encrypt 证书是 DV(Domain Validation)证书,只验证域名所有权,不验证公司信息。

其证书的签发/续签都是脚本自动化的,官方提供了几种证书的申请方式方法,网址为 https://letsencrypt.org/zh-cn/docs/client-options/。

安装Certbot客户端

[root@aliyun-www ~]# yum install certbot -y

等待它下载安装完毕,检测是否成功。使用如下命令:

[root@aliyun-www ~]# certbot --versioncertbot 2.11.0

现在已经安装成功,接着就可以申请SSL证书了。

比如我们要为static.example.com申请证书(你可以更换为真实的域名),使用如下命令:

[root@aliyun-www ~]# certbot certonly --nginx -d static.example.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
The requested nginx plugin does not appear to be installed
Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile /var/log/letsencrypt/letsencrypt.log or re-run Certbot with -v for more details.

执行时,如果出现上面的提示,需要 nginx plugin 插件,可以运行如下命令,安装相应的插件解决:

[root@aliyun-www ~]# yum install python3-certbot-nginx

接着,请让我们再次运行申请SSL的指令:

[root@aliyun-www ~]# certbot certonly --nginx -d static.example.com

接下来,是一个交互的过程。

是否同意 Let's Encrypt 协议要求=>需要同意,是否分享你的邮箱,询问是否对域名和机器(IP)进行绑定=>需要同意。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): youname@example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.5-February-24-2025.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: y
Account registered.
Requesting a certificate for static.example.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/static.example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/static.example.com/privkey.pem
This certificate expires on 2025-06-18.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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/ 目录下看到对应域名的文件夹,里面存放了指向证书的一些快捷方式。

证书续签:自动更新 SSL 证书

Let’s Encrypt 提供的证书只有90天的有效期,证书在到期前30天才会续签成功,我们必须在证书到期之前,重新获取这些证书,certbot 给我们提供了一个很方便的命令,那就是 certbot renew。通过这个命令,他会自动检查系统内的证书,并且自动更新这些证书。可以运行这个命令测试一下:

[root@aliyun-www ~]# certbot renew --dry-run

Certbot 会检查证书是否过期,如果过期会自动续期。可以将 certbot renew 添加到Cron定时任务,定期检查证书是否过期。

Nginx 开启 https

证书生成完成后可以到 /etc/letsencrypt/live/ 目录下查看对应域名的证书文件。编辑 nginx 配置文件监听 443 端口,启用 SSL,并配置 SSL 的公钥、私钥证书路径:

server {
listen 443 ssl;
server_name example.cn;
root /home/www/example.com;
index index.html index.htm index.php;
ssl_certificate /etc/letsencrypt/live/example/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
...
}

添加 HTTP 自动跳转到 HTTPS:

server {
listen 80;
server_name example.com;
location / {
rewrite ^(.*)$ https://$host$1 permanent;
}
}

配置好Nginx后,重新启动:

systemctl restart nginx

这样,你就可以一直有不过期的证书了。

结语

通过执行这些步骤,我们已在 CentOS 9 服务器上成功安装和配置 Let's Encrypt SSL,从而为网站访问者提供安全通信。请定期检查你的配置和更新,以维护安全可靠的网络状态。

评论