说明
日期:2025年5月30日
与以纯文本形式发送和接收消息的标准 HTTP 不同,HTTPS 使用SSL/TLS等协议对服务器进行身份验证、加密通信内容和检测篡改。 这样可以防止欺骗、中间人攻击和窃听等攻击。
证书很重要,如果用户主动信任了伪造证书,那https的优势就荡然无存了,所以不要自己添加证书。
示例
日期:2025年5月30日。
操作系统:Alibaba Cloud Linux 3.2104 LTS 64位。
应用:
- node.js v22.15.1
- npm 11.4.1
- Certbot
安装SSL证书
公开的SSL证书必须使用固定域名或固定公网ip。
示例中使用域名fxxkrock.top
。
修改时区,有的linux失去不是中国,SSL证书含有证书起止时间,所以申请SLL证书后修改时间可能导致证书失效。
sudo timedatectl set-timezone Asia/Shanghai
添加 EPEL 仓库(Certbot 依赖)
sudo yum install -y epel-release
报错
[ecs-user@iZbp18zmckjuyndr4e25koZ server_crystalsearch]$ sudo yum install -y epel-release
Last metadata expiration check: 15:18:43 ago on Thu 29 May 2025 08:17:50 PM CST.
Error:
Problem: problem with installed package epel-aliyuncs-release-8-15.1.al8.noarch
- package epel-aliyuncs-release-8-15.1.al8.noarch from @System conflicts with epel-release provided by epel-release-8-22.el8.noarch from epel
- package epel-aliyuncs-release-8-15.1.al8.noarch from alinux3-updates conflicts with epel-release provided by epel-release-8-22.el8.noarch from epel
- conflicting requests
(try to add '--allowerasing' to command line to replace conflicting packages or '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)
这个错误是因为系统中已经存在名为 epel-aliyuncs-release
的包,与标准的 epel-release
包冲突。
从包的名称来看,是阿里云自己兼容的epel,所以直接进行下一步。
安装certbot。
[ecs-user@iZbp18zmckjuyndr4e25koZ server_crystalsearch]$ sudo yum install -y certbot
使用 Standalone 模式(需临时停止占用 80/443 端口的服务),获取SSL证书。
[ecs-user@iZbp18zmckjuyndr4e25koZ ~]$ sudo certbot certonly --standalone -d fxxkrock.top
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): 18015262576@189.cn
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: n
Account registered.
Requesting a certificate for fxxkrock.top
Certbot failed to authenticate some domains (authenticator: standalone). The Certificate Authority reported these problems:
Domain: fxxkrock.top
Type: connection
Detail: 47.97.27.78: Fetching http://fxxkrock.top/.well-known/acme-challenge/DqitmUHBr-4eEkBcNWoEZWzAf10C8Gwpm5ipvFxoY1U: Timeout during connect (likely firewall problem)
Hint: The Certificate Authority failed to download the challenge files from the temporary standalone webserver started by Certbot on port 80. Ensure that the listed domains point to this machine and that it can accept inbound connections from the internet.
Some challenges have failed.
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.
报错了。从提示看,也就是网不通,要从80端口访问。
Certbot 的 Webroot 模式 和 Standalone 模式的区别
Webroot 模式
工作原理:
- 在您的网站根目录创建临时验证文件(
/.well-known/acme-challenge/
)- Let’s Encrypt 服务器通过 HTTP 访问这些文件验证域名所有权
- 需要指定网站根目录路径(如
-w /var/www/your-site
)为什么需要路径:
- Certbot 需要将验证文件写入指定目录
- Web 服务器(Nginx/Apache/Node.js)必须能通过 HTTP 80 端口访问这些文件
- 例如:
http://your-domain.com/.well-known/acme-challenge/xxxxx
优势:
- 无需停止正在运行的 Web 服务
- 适合生产环境(服务持续可用)
- 可与现有 Web 服务器配合使用
适用场景:
- 已有 Web 服务器运行中
- 无法临时停止服务
- 可访问网站根目录
Standalone 模式
工作原理:
- Certbot 自己启动临时 Web 服务器(监听 80/443 端口)
- 直接响应 Let’s Encrypt 的验证请求
- 无需指定路径
为什么不需要路径:
- Certbot 完全自己处理验证请求
- 不依赖现有 Web 服务器或文件系统
- 验证过程在内存中完成,不写入文件
优势:
- 简单快速,无需配置 Web 服务器
- 不依赖现有网站目录结构
- 适合无 Web 服务器的环境
代价:
- 需临时停止占用 80/443 端口的服务
- 获取证书期间服务短暂中断
- 不适合 7x24 小时运行的关键服务
技术对比表
特性 Webroot 模式 Standalone 模式 需要路径 ✓ (必须指定网站根目录) ✗ (完全自包含) 需停止服务 ✗ ✓ (释放80/443端口) 验证方式 通过现有Web服务器访问文件 自建临时Web服务器响应请求 适合场景 生产环境/服务不可中断 测试环境/简单快速获取证书 依赖Web服务器 ✓ (需配置访问.well-known目录) ✗ 证书续期影响 零中断 短暂中断(约10-30秒)
Node.js 场景下的最佳实践
推荐方案:Webroot 模式
# 1. 创建验证目录 sudo mkdir -p /var/www/ssl_validation/.well-known/acme-challenge # 2. 在Node.js中添加路由处理验证 const app = express(); app.get('/.well-known/acme-challenge/:token', (req, res) => { res.sendFile(`/var/www/ssl_validation/.well-known/acme-challenge/${req.params.token}`); }); # 3. 获取证书 sudo certbot certonly --webroot -w /var/www/ssl_validation -d your-domain.com
Standalone 模式应急使用:
# 1. 停止Node.js服务 sudo systemctl stop node-app # 2. 获取证书(Standalone模式) sudo certbot certonly --standalone -d your-domain.com --preferred-challenges http # 3. 重启服务 sudo systemctl start node-app
为什么 Standalone 不需要路径?深层解释
Standalone 模式的工作流程:
Certbot 启动内置的微型 HTTP 服务器(Python实现)
监听 80 端口(HTTP验证)或 443 端口(TLS-ALPN验证)
当 Let’s Encrypt 发送验证请求时:
验证通过后立即销毁临时服务器
全程不涉及文件系统操作,所有验证内容在内存中动态生成
这种设计使得 Standalone 模式:
- 完全独立于现有 Web 基础设施
- 不需要配置任何文件路径
- 但要求独占端口(需临时停止其他服务)
实际应用建议
对于 Node.js 项目,推荐组合方案:
首次获取证书:使用 Standalone 模式(简单快速)
sudo systemctl stop node-app sudo certbot certonly --standalone -d your-domain.com sudo systemctl start node-app
证书续期:改用 Webroot 模式(无中断)
// 在Express中添加验证路由 app.use('/.well-known/acme-challenge', express.static('/etc/letsencrypt/webrootauth'));
# 续期配置 sudo certbot renew --webroot -w /etc/letsencrypt/webrootauth
终极方案:用 Nginx 处理验证(最佳实践)
server { listen 80; server_name your-domain.com; location /.well-known/acme-challenge { root /var/www/certbot; } location / { return 301 https://$host$request_uri; } }
这样 Node.js 只需专注 443 端口 HTTPS 服务,完全解耦证书管理。
从资料看,在过程中,80/443 端口都要使用。
安全设备进出都开启80/443。
[ecs-user@iZbp18zmckjuyndr4e25koZ ~]$ sudo certbot certonly --standalone -d fxxkrock.top
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for fxxkrock.top
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/fxxkrock.top/fullchain.pem
Key is saved at: /etc/letsencrypt/live/fxxkrock.top/privkey.pem
This certificate expires on 2025-08-28.
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
需要记录其中 Certificate 和 Key 的保存位置,web应用需要加载这两个文件。
修改web应用的代码
// 添加https模块
const https = require('https');
// 读取SSL证书文件
const credentials = {
key: fs.readFileSync('/etc/letsencrypt/live/fxxkrock.top/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/fxxkrock.top/fullchain.pem')
};
// 设置端口号
const port = 3080;
// 创建HTTPS服务器
const server = https.createServer(credentials, app).listen(port, () => {
console.log(`HTTPS server successfully launched: https://localhost:${port}`);
});
启动web应用
权限不够,无法读取SSL证书
[ecs-user@iZbp18zmckjuyndr4e25koZ server_crystalsearch]$ node server.js
node:fs:562
return binding.open(
^
Error: EACCES: permission denied, open '/etc/letsencrypt/live/fxxkrock.top/privkey.pem'
at Object.openSync (node:fs:562:18)
at Object.readFileSync (node:fs:446:35)
at Object.<anonymous> (/home/ecs-user/server_crystalsearch/server.js:103:13)
at Module._compile (node:internal/modules/cjs/loader:1730:14)
at Object..js (node:internal/modules/cjs/loader:1895:10)
at Module.load (node:internal/modules/cjs/loader:1465:32)
at Function._load (node:internal/modules/cjs/loader:1282:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5) {
errno: -13,
code: 'EACCES',
syscall: 'open',
path: '/etc/letsencrypt/live/fxxkrock.top/privkey.pem'
}
Node.js v22.15.1
复制这两证书到网站目录,并修改js代码,把文件路径改为新路径。
[ecs-user@iZbp18zmckjuyndr4e25koZ server_crystalsearch]$ sudo cp /etc/letsencrypt/live/fxxkrock.top/privkey.pem /home/ec
s-user/server_crystalsearch/privkey.pem
[ecs-user@iZbp18zmckjuyndr4e25koZ server_crystalsearch]$ sudo cp /etc/letsencrypt/live/fxxkrock.top/fullchain.pem /home/
ecs-user/server_crystalsearch/fullchain.pem
启动过程无报错
[ecs-user@iZbp18zmckjuyndr4e25koZ server_crystalsearch]$ node server.js
Web folder: /home/ecs-user/server_crystalsearch/crystalsearch
HTTPS server successfully launched: https://localhost:3060
浏览器访问http://fxxkrock.top:3060/
,浏览器显示“响应时间过长”。
访问https://fxxkrock.top:3060/
,浏览器显示“响应时间过长”。
忘记在安全设备打开3060端口了。
打开后,https正常,控制台无报错。
访问http会自动跳转到https。