Node.js项目云服务器部署全流程指南:从开发到上线

Node.js项目云服务器部署全流程指南:从开发到上线

一、部署前的准备工作

1.1 云服务器选型建议

  • 配置选择:根据项目类型推荐基础配置(如2核4G内存适用于中小型API服务,4核8G适合高并发应用)
  • 操作系统选择:推荐Ubuntu 22.04 LTS或CentOS 8,兼顾稳定性与社区支持
  • 网络配置要点:确保开放80/443端口(HTTP/HTTPS),建议配置安全组规则限制源IP

1.2 本地环境验证

在部署前需完成:

  1. # 本地运行测试
  2. NODE_ENV=production node app.js
  3. # 压力测试示例
  4. ab -n 1000 -c 100 http://localhost:3000/

确保项目在生产模式下稳定运行,记录关键性能指标(QPS、响应时间)

二、服务器环境搭建

2.1 系统环境配置

  1. # Ubuntu系统基础环境安装
  2. sudo apt update
  3. sudo apt install -y nginx curl git unzip
  4. # Node.js安装(推荐使用nvm)
  5. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
  6. source ~/.bashrc
  7. nvm install --lts

2.2 数据库部署方案

  • MySQL配置示例
    1. sudo apt install mysql-server
    2. sudo mysql_secure_installation
    3. # 安全配置后创建数据库
    4. CREATE DATABASE node_app CHARACTER SET utf8mb4;
    5. CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'strong_password';
    6. GRANT ALL PRIVILEGES ON node_app.* TO 'app_user'@'localhost';
  • MongoDB部署建议:使用官方仓库安装最新稳定版,配置认证与副本集

三、项目部署实施

3.1 代码传输与版本控制

推荐使用Git进行代码管理:

  1. # 服务器端Git仓库初始化
  2. mkdir -p ~/repos && cd ~/repos
  3. git init --bare node-app.git
  4. # 本地推送配置
  5. git remote add production user@server_ip:~/repos/node-app.git
  6. git push production master

3.2 依赖管理与环境配置

  1. # 在项目目录安装依赖
  2. cd ~/node-app
  3. npm install --production
  4. # 环境变量配置(推荐使用dotenv)
  5. echo "DB_HOST=localhost
  6. DB_USER=app_user
  7. DB_PASS=strong_password" > .env

3.3 进程管理方案对比

方案 优点 缺点
PM2 自动重启、负载均衡、日志管理 资源占用较高
Systemd 系统原生支持、资源控制精细 配置复杂
Forever 简单易用 功能单一

PM2配置示例

  1. // ecosystem.config.js
  2. module.exports = {
  3. apps: [{
  4. name: "node-app",
  5. script: "./app.js",
  6. instances: "max",
  7. exec_mode: "cluster",
  8. env: {
  9. NODE_ENV: "production",
  10. PORT: 3000
  11. },
  12. error_file: "/var/log/node-app/error.log",
  13. out_file: "/var/log/node-app/out.log"
  14. }]
  15. };

四、安全加固措施

4.1 防火墙配置

  1. # Ubuntu ufw配置示例
  2. sudo ufw allow 22/tcp # SSH
  3. sudo ufw allow 80/tcp # HTTP
  4. sudo ufw allow 443/tcp # HTTPS
  5. sudo ufw enable

4.2 SSL证书配置(Let’s Encrypt)

  1. # 安装Certbot
  2. sudo apt install certbot python3-certbot-nginx
  3. # 获取证书
  4. sudo certbot --nginx -d example.com -d www.example.com
  5. # 自动续期测试
  6. sudo certbot renew --dry-run

4.3 安全组策略建议

  • 限制SSH访问为特定IP段
  • 禁用ROOT用户远程登录
  • 定期更新系统补丁(sudo apt upgrade -y

五、性能优化方案

5.1 Nginx反向代理配置

  1. server {
  2. listen 80;
  3. server_name example.com;
  4. location / {
  5. proxy_pass http://localhost:3000;
  6. proxy_http_version 1.1;
  7. proxy_set_header Upgrade $http_upgrade;
  8. proxy_set_header Connection 'upgrade';
  9. proxy_set_header Host $host;
  10. proxy_cache_bypass $http_upgrade;
  11. }
  12. # 静态资源缓存
  13. location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  14. expires 30d;
  15. access_log off;
  16. }
  17. }

5.2 负载均衡实现

PM2集群模式配置

  1. pm2 start ecosystem.config.js
  2. pm2 scale node-app 4 # 启动4个工作进程

Nginx负载均衡示例

  1. upstream node_servers {
  2. server 127.0.0.1:3000;
  3. server 127.0.0.1:3001;
  4. server 127.0.0.1:3002;
  5. }
  6. server {
  7. listen 80;
  8. location / {
  9. proxy_pass http://node_servers;
  10. }
  11. }

六、监控与维护

6.1 日志管理方案

  1. # 日志轮转配置(/etc/logrotate.d/node-app)
  2. /var/log/node-app/*.log {
  3. daily
  4. missingok
  5. rotate 14
  6. compress
  7. delaycompress
  8. notifempty
  9. copytruncate
  10. }

6.2 监控工具推荐

  • PM2内置监控pm2 monit
  • Prometheus + Grafana:适合中大型系统
  • New Relic/Datadog:商业APM解决方案

七、常见问题解决方案

7.1 端口冲突处理

  1. # 查找占用端口的进程
  2. sudo lsof -i :3000
  3. # 终止进程
  4. sudo kill -9 <PID>

7.2 依赖安装失败处理

  • 检查Node.js版本兼容性
  • 清除npm缓存后重试:
    1. npm cache clean --force
    2. rm -rf node_modules package-lock.json
    3. npm install

7.3 内存泄漏排查

  • 使用node --inspect进行调试
  • PM2内存监控:
    1. pm2 list
    2. pm2 show node-app

八、持续集成建议

推荐部署流程:

  1. 代码提交触发Webhook
  2. 服务器自动拉取最新代码
  3. 执行构建脚本(npm run build
  4. 通过PM2重启应用

Git Hook示例post-receive):

  1. #!/bin/bash
  2. TARGET="/var/www/node-app"
  3. GIT_DIR="/home/user/repos/node-app.git"
  4. BRANCH="master"
  5. while read oldrev newrev ref
  6. do
  7. if [[ $ref = refs/heads/$BRANCH ]];
  8. then
  9. echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
  10. git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
  11. cd $TARGET
  12. npm install --production
  13. pm2 reload node-app
  14. else
  15. echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
  16. fi
  17. done

通过以上系统化的部署方案,开发者可以构建出稳定、高效、安全的Node.js云服务架构。实际部署时需根据项目特点调整参数,并建立完善的监控告警机制,确保服务持续可用。