先把概念说清楚:微信小程序本身不要求 CVM 上装什么“小程序专用环境”。CVM 上跑的是后端 API,小程序前端通过 wx.request() 调你的接口。关键是:公网 HTTPS 域名、可信 SSL 证书、微信公众平台服务器域名配置、Node.js 进程守护、Nginx 反代。
推荐架构:
微信小程序
↓ HTTPS
https://api.example.com
↓ DNS 解析
腾讯云 CVM 公网 IP
↓ 安全组放行 443
Nginx :443
↓ proxy_pass
Node.js 127.0.0.1:3000
↓
MySQL / Redis / 腾讯云数据库等
1. 准备条件
你需要先准备这些:
-
一台腾讯云 CVM
系统建议:Ubuntu 22.04 LTS、TencentOS Server、OpenCloudOS。新项目不建议再用 CentOS 7。 -
一个已备案域名
例如:api.example.com。小程序正式环境要求服务器域名备案。 -
域名解析到 CVM 公网 IP
可以用腾讯云 DNSPod,添加 A 记录:api.example.com A记录 CVM公网IP -
SSL 证书
在腾讯云「SSL 证书」申请免费 DV 证书,下载 Nginx 版本。 -
小程序 AppID
后端如果做微信登录,会用到 AppID 和 AppSecret。AppSecret 只能放服务端,不能写进小程序前端。
2. CVM 安全组和系统防火墙
在腾讯云控制台找到 CVM 实例对应的安全组,入站规则建议这样配:
| 协议 | 端口 | 来源 | 说明 |
|---|---|---|---|
| TCP | 22 | 你的办公 IP/32 | SSH 登录,别开放给全网 |
| TCP | 80 | 0.0.0.0/0 | HTTP 跳转 HTTPS |
| TCP | 443 | 0.0.0.0/0 | 小程序 HTTPS 请求 |
Node.js 监听的 3000 端口不建议对公网开放,交给 Nginx 反代即可。
如果系统防火墙启用了,也要放行:
Ubuntu:
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw status
TencentOS / OpenCloudOS:
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
3. 安装 Node.js
建议安装当前 LTS 版本。下面用 22.x 举例,实际安装时可以按 Node 官方 LTS 版本替换。
Ubuntu / Debian:
sudo apt update
sudo apt install -y ca-certificates curl gnupg nginx
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v
TencentOS / OpenCloudOS:
sudo dnf install -y ca-certificates curl nginx
curl -fsSL https://rpm.nodesource.com/setup_22.x | sudo bash -
sudo dnf install -y nodejs
node -v
npm -v
国内拉 npm 包慢的话,可以切镜像源:
npm config set registry https://registry.npmmirror.com
npm config get registry
如果你一台机器上要跑多个 Node 项目,建议用 nvm 管理版本;如果就是单台 CVM 跑一个后端服务,包管理器安装就够了。
4. 部署一个最小 Node.js API
示例目录:
sudo mkdir -p /data/www/weapp-api
sudo chown -R $USER:$USER /data/www
cd /data/www/weapp-api
初始化项目:
npm init -y
npm install express
创建 app.js:
cat > app.js <<'EOF'
const express = require('express')
const app = express()
app.use(express.json())
app.get('/health', (req, res) => {
res.json({
ok: true,
time: Date.now()
})
})
app.post('/api/echo', (req, res) => {
res.json({
message: 'ok',
data: req.body
})
})
const port = process.env.PORT || 3000
app.listen(port, '127.0.0.1', () => {
console.log(`weapp-api listening on 127.0.0.1:${port}`)
})
EOF
先用前台跑一下:
node app.js
另开一个终端测试:
curl http://127.0.0.1:3000/health
返回类似:
{"ok":true,"time":1720000000000}
说明 Node 服务没问题。
5. 用 PM2 守护 Node 进程
正式环境不要让 Node 直接挂在 SSH 会话里跑,建议用 PM2:
sudo npm install -g pm2
pm2 start app.js --name weapp-api
pm2 status
pm2 logs weapp-api
设置开机自启:
pm2 startup
它会输出一条 sudo env PATH=... pm2 startup ... 之类的命令,按提示复制执行即可。然后保存进程列表:
pm2 save
后续重启 CVM 后,Node 服务会自动拉起来。
6. 配置 Nginx + HTTPS
先在腾讯云 SSL 证书服务下载 Nginx 证书,一般包括证书文件和私钥。上传到 CVM,例如:
sudo mkdir -p /etc/nginx/ssl
sudo chmod 700 /etc/nginx/ssl
假设文件如下:
/etc/nginx/ssl/api.example.com.fullchain.pem
/etc/nginx/ssl/api.example.com.key
注意:腾讯云证书包里文件命名可能不太一样,按下载包说明配置。ssl_certificate 要指向站点证书和中间证书链,ssl_certificate_key 指向私钥。
创建 Nginx 配置:
sudo vi /etc/nginx/conf.d/weapp-api.conf
写入:
server {
listen 80;
server_name api.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name api.example.com;
ssl_certificate /etc/nginx/ssl/api.example.com.fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/api.example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_timeout 10m;
client_max_body_size 10m;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
检查配置并启动:
sudo nginx -t
sudo systemctl enable --now nginx
sudo systemctl reload nginx
验证:
curl -I http://api.example.com
curl https://api.example.com/health
第一个应返回 301 跳 HTTPS,第二个应返回 Node 接口的 JSON。
7. 小程序后台配置服务器域名
进入微信公众平台:
开发 → 开发管理 → 开发设置 → 服务器域名
按实际使用配置:
request合法域名:https://api.example.com
uploadFile合法域名:https://api.example.com 如需要上传文件
downloadFile合法域名:https://api.example.com 如需要下载文件
socket合法域名:wss://api.example.com 如使用 WebSocket
几个细节:
- 合法域名只填域名,不要带路径,例如不要填
https://api.example.com/v1。 - 不要填 IP。
- 不要填
localhost。 - 正式环境不要依赖开发者工具的“不校验合法域名”。
- 证书必须是受信任 CA 证书,自签证书不行。
- 微信后台服务器域名修改有次数限制,上线前尽量一次性配好。
小程序端调用示例:
wx.request({
url: 'https://api.example.com/health',
method: 'GET',
success(res) {
console.log(res.data)
},
fail(err) {
console.error(err)
}
})
8. 微信登录相关注意点
如果小程序要登录,流程一般是:
- 小程序调用
wx.login()获取code - 小程序把
code发给你的 Node API - Node 服务端调用微信接口:
https://api.weixin.qq.com/sns/jscode2session
- 服务端拿到
openid、session_key - 服务端生成自己的登录态 token 返回小程序
重点:
AppSecret只能放服务端。session_key不要返回给小程序前端。- 不要把所有用户鉴权逻辑放在前端。
- 建议用环境变量保存密钥,不要写死在代码仓库里。
9. 常见坑
-
安全组开了 443,但系统防火墙没开
两边都要检查。 -
Node 监听公网 IP 或 0.0.0.0,又直接暴露 3000 端口
更合理的做法是 Node 只监听127.0.0.1:3000,由 Nginx 的 443 转发。 -
域名没备案
小程序正式环境要求服务器域名备案,大陆 CVM 也需要按腾讯云要求完成备案接入。 -
证书链不完整
浏览器能打开不代表微信环境一定能通过,Nginx 要配置完整证书链。 -
微信后台域名带了路径
服务器域名只能填域名,接口路径写在小程序请求 URL 里。 -
证书快过期或续期后没 reload Nginx
腾讯云证书续期后,要重新部署证书并 reload Nginx。 -
数据库端口别乱开放
如果装 MySQL、Redis,3306、6379 不建议对公网开放。生产项目更推荐用腾讯云数据库 MySQL、Redis,通过内网访问。
按上面这套配置,CVM 上的 Node.js 环境就可以稳定支撑微信小程序后端了。核心不是 Node 本身,而是:HTTPS 合法域名 + Nginx 反代 + 进程守护 + 微信后台域名配置。
云服务器