使用腾讯云服务器搭建 Node.js 后端以支持微信小程序开发,可以按照以下详细步骤进行操作。整个过程包括购买服务器、配置环境、部署项目、域名与 HTTPS 设置等。
一、准备工作
-
注册腾讯云账号
- 访问 腾讯云官网 注册并完成实名认证。
-
购买云服务器(CVM)
- 进入「云服务器 CVM」控制台。
- 选择:
- 地域:靠近目标用户(如广州、上海)
- 操作系统:推荐 Ubuntu Server 20.04 LTS 或 CentOS 7.x
- 实例规格:入门级(如 1核2G)
- 带宽:建议 1Mbps 起(可后续升级)
- 设置登录密码或密钥对
- 完成支付购买
-
获取公网 IP
- 购买后在控制台查看服务器的公网 IP 地址。
二、连接到服务器(SSH)
使用 SSH 工具连接服务器:
ssh root@你的公网IP
输入密码或使用密钥登录。
三、安装 Node.js 环境
方法一:使用 NodeSource 安装(推荐 Ubuntu)
# 更新系统包
sudo apt update
# 安装 curl(如果未安装)
sudo apt install -y curl
# 添加 NodeSource 仓库(以 Node.js 18 为例)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
# 安装 Node.js 和 npm
sudo apt install -y nodejs
# 验证安装
node -v # 应输出 v18.x.x
npm -v # 输出版本号
方法二:CentOS 使用 yum
curl -sL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo yum install -y nodejs
四、安装 PM2(进程管理工具)
PM2 可以让 Node.js 应用后台运行并自动重启。
npm install -g pm2
# 验证
pm2 --version
五、上传并部署 Node.js 项目
1. 创建项目目录
mkdir /var/www/myapp
cd /var/www/myapp
2. 上传代码(三种方式任选其一)
-
方式 A:使用 Git 克隆
git clone https://github.com/yourname/your-node-project.git . -
方式 B:本地打包上传(scp)
在本地终端执行:
scp -r ./dist root@你的IP:/var/www/myapp -
方式 C:使用 SFTP 工具(如 FileZilla)上传文件
3. 安装依赖
npm install
4. 启动应用(示例)
假设入口文件是 app.js 或 server.js:
pm2 start app.js --name "my-wechat-app"
5. 设置开机自启
pm2 startup
pm2 save
六、配置防火墙和安全组
1. 腾讯云控制台设置安全组
进入 CVM 控制台 → 找到实例 → 安全组 → 编辑规则,添加:
| 协议类型 | 端口范围 | 授权对象 |
|---|---|---|
| TCP | 80 | 0.0.0.0/0 |
| TCP | 443 | 0.0.0.0/0 |
| TCP | 3000 | 0.0.0.0/0(测试用) |
生产环境建议只开放 80 和 443,Node.js 服务监听 3000 端口时,可通过 Nginx 反向X_X。
2. 服务器内部防火墙(Ubuntu/CentOS)
# Ubuntu 使用 ufw
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
# CentOS 使用 firewalld
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --reload
七、配置域名和 HTTPS(必须用于小程序)
微信小程序要求接口必须使用 HTTPS 协议。
1. 购买并解析域名
- 在腾讯云「域名注册」购买
.com或.cn域名。 - 进入「DNS 解析」,添加 A 记录指向你的服务器公网 IP。
2. 申请免费 SSL 证书(Let’s Encrypt)
推荐使用 Nginx + Certbot:
安装 Nginx
sudo apt install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
安装 Certbot
sudo apt install -y certbot python3-certbot-nginx
获取 SSL 证书
sudo certbot --nginx -d yourdomain.com
按提示填写邮箱,同意协议,自动配置 HTTPS。
证书有效期 90 天,可设置自动续期:
sudo crontab -e添加一行:
0 12 * * * /usr/bin/certbot renew --quiet
八、配置 Nginx 反向X_X
编辑 Nginx 配置文件:
sudo nano /etc/nginx/sites-available/default
添加如下内容(假设 Node.js 监听 3000 端口):
server {
listen 80;
server_name yourdomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
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;
}
}
保存后测试并重载 Nginx:
sudo nginx -t
sudo systemctl reload nginx
九、小程序前端调用 API
在微信小程序中,使用 request 请求你的 HTTPS 接口:
wx.request({
url: 'https://yourdomain.com/api/user',
method: 'GET',
success(res) {
console.log(res.data)
}
})
确保在小程序管理后台「开发管理」→「开发设置」中将 yourdomain.com 添加到 request 合法域名。
十、维护与监控
- 查看日志:
pm2 logs my-wechat-app - 重启服务:
pm2 restart my-wechat-app - 监控状态:
pm2 list
总结流程图
购买服务器 → SSH 登录 → 安装 Node.js → 上传项目 → PM2 启动
↓
配置安全组 → 绑定域名 → Nginx 反向X_X → Let's Encrypt HTTPS
↓
小程序配置合法域名 → 调用 HTTPS 接口
✅ 完成以上步骤后,你的腾讯云服务器就已经成功运行了一个支持微信小程序的 Node.js 后端服务。
如有需要,可进一步集成数据库(如 MongoDB、MySQL)、Redis 缓存、CI/CD 自动部署等。
CDNK博客