在腾讯云轻量应用服务器上使用 Node.js 搭建微信小程序后端服务,是一个非常常见且实用的场景。以下是详细的步骤指南,帮助你从零开始完成部署。
🌐 一、准备工作
1. 购买腾讯云轻量应用服务器
- 登录 腾讯云控制台
- 选择「轻量应用服务器」
- 选择镜像:推荐 Ubuntu 20.04 / 22.04(或选择「自定义镜像」后续手动安装)
- 地区、带宽根据需要选择(建议至少 1Mbps 起步)
- 安全组:确保开放以下端口:
22(SSH)80(HTTP)443(HTTPS)3000或8080(Node.js 服务端口)
🛠️ 二、连接服务器并安装 Node.js
1. 使用 SSH 连接服务器
ssh root@你的服务器公网IP
# 输入密码
2. 更新系统包
sudo apt update && sudo apt upgrade -y
3. 安装 Node.js(推荐使用 nvm)
# 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# 重新加载配置
source ~/.bashrc
# 安装 Node.js(推荐 LTS 版本)
nvm install 18
# 或 node 20
nvm install 20
# 验证安装
node -v
npm -v
📦 三、创建 Node.js 后端服务(Express 示例)
1. 创建项目目录
mkdir /home/myapp && cd /home/myapp
npm init -y
npm install express cors dotenv
2. 创建 app.js
// app.js
const express = require('express');
const cors = require('cors');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
// 小程序接口示例
app.get('/api/hello', (req, res) => {
res.json({ message: 'Hello from Tencent Cloud Light Server!' });
});
app.post('/api/user', (req, res) => {
const { name } = req.body;
res.json({ code: 0, data: { id: 1, name: name, avatar: 'https://example.com/avatar.png' } });
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on http://0.0.0.0:${PORT}`);
});
3. 创建 .env 文件(可选)
PORT=3000
🔁 四、使用 PM2 守护进程运行 Node.js
1. 安装 PM2
npm install -g pm2
2. 启动项目
pm2 start app.js --name "miniapp-backend"
3. 设置开机自启
pm2 startup
pm2 save
4. 常用 PM2 命令
pm2 list # 查看运行进程
pm2 logs # 查看日志
pm2 restart app # 重启
pm2 stop app # 停止
🌍 五、配置 Nginx 反向(可选但推荐)
1. 安装 Nginx
sudo apt install nginx -y
2. 配置站点(替换为你的域名或 IP)
sudo nano /etc/nginx/sites-available/miniapp
内容如下:
server {
listen 80;
server_name your-domain.com; # 替换为你的域名或公网IP
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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;
proxy_cache_bypass $http_upgrade;
}
}
3. 启用配置
sudo ln -s /etc/nginx/sites-available/miniapp /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default # 删除默认站点
sudo nginx -t # 测试配置
sudo systemctl reload nginx
🔐 六、绑定域名 + HTTPS(可选)
1. 域名解析
- 在腾讯云「域名管理」中,将域名 A 记录指向服务器公网 IP。
2. 申请免费 SSL 证书(Let’s Encrypt)
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your-domain.com
按提示操作,自动配置 HTTPS。
🧪 七、小程序前端调用测试
在微信小程序中发起请求:
// pages/index/index.js
wx.request({
url: 'https://your-domain.com/api/hello',
method: 'GET',
success(res) {
console.log(res.data); // 输出:{ message: "Hello from Tencent Cloud Light Server!" }
}
})
⚠️ 注意:
- 小程序要求接口必须是 HTTPS(正式上线)
- 域名需在「小程序管理后台」的「开发设置」中配置
request合法域名
✅ 八、安全建议
- 不要暴露 Node.js 直接端口(如 3000),使用 Nginx 反向
- 开启腾讯云防火墙,只开放必要端口
- 定期更新系统和依赖
- 使用环境变量管理敏感信息
📚 九、后续扩展
- 接入数据库(MongoDB / MySQL / PostgreSQL)
- 使用 JWT 实现用户登录
- 部署前端静态资源(Vue/React 小程序 H5 版)
- 使用 CI/CD 自动部署(GitHub Actions + 腾讯云)
✅ 总结
你已经成功在腾讯云轻量服务器上:
- 安装 Node.js
- 搭建 Express 后端服务
- 使用 PM2 守护进程
- 配置 Nginx 反向 + HTTPS
- 与微信小程序通信
如需更完整项目模板(含用户登录、数据库、Token 验证),可以留言,我可以提供 GitHub 模板地址或代码结构。
祝你开发顺利!🚀
PHPWP博客