LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

给你的网站添加免费的Let’s Encrypt SSL 证书

admin
2025年8月14日 21:1 本文热度 41

导读:你将拥有一个不过期的SSL DV 免费证书,请看本文。

背景

之前网站使用的是免费证书,但是免费证书(默认证书)的签发有效期由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,从而为网站访问者提供安全通信。请定期检查你的配置和更新,以维护安全可靠的网络状态。

作者:场长

阅读原文:原文链接


该文章在 2025/8/15 11:39:57 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved