插件窝 干货文章 NGINX PM2 VPS: 构建高性能的Web服务器

NGINX PM2 VPS: 构建高性能的Web服务器

NGINX sudo PM2 应用 309    来源:    2025-04-11

NGINX + PM2 + VPS: 构建高性能Web服务器指南

概述

本指南将介绍如何在VPS上使用NGINX和PM2搭建高性能的Web服务器。这种组合能够提供强大的静态内容服务、反向代理和Node.js应用管理能力。

1. 准备工作

1.1 选择VPS

  • 推荐使用Ubuntu 20.04/22.04 LTS或CentOS 7/8
  • 至少1GB RAM (Node.js应用建议2GB+)
  • 确保SSH访问权限

1.2 基本系统设置

# 更新系统
sudo apt update && sudo apt upgrade -y  # Ubuntu/Debian
sudo yum update -y                     # CentOS/RHEL

# 安装基本工具
sudo apt install -y git curl wget      # Ubuntu/Debian
sudo yum install -y git curl wget      # CentOS/RHEL

2. 安装Node.js和PM2

2.1 安装Node.js

# 使用NodeSource仓库安装最新LTS版本
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs         # Ubuntu/Debian

# 或者对于CentOS/RHEL
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -
sudo yum install -y nodejs

2.2 安装PM2

sudo npm install -g pm2

2.3 设置PM2开机启动

pm2 startup
# 按照提示执行生成的命令
pm2 save

3. 安装和配置NGINX

3.1 安装NGINX

sudo apt install -y nginx  # Ubuntu/Debian
sudo yum install -y nginx  # CentOS/RHEL

3.2 基本NGINX配置

# 启动NGINX并设置开机启动
sudo systemctl start nginx
sudo systemctl enable nginx

3.3 配置防火墙

# 允许HTTP/HTTPS流量
sudo ufw allow 'Nginx Full'  # Ubuntu
# 或
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload   # CentOS

4. 部署Node.js应用

4.1 使用PM2启动应用

cd /path/to/your/app
pm2 start app.js --name "my-app"  # 或使用npm脚本: pm2 start npm --name "my-app" -- start

4.2 常用PM2命令

pm2 list             # 查看运行中的应用
pm2 monit            # 监控应用
pm2 logs             # 查看日志
pm2 restart my-app   # 重启应用
pm2 stop my-app      # 停止应用
pm2 delete my-app    # 删除应用

5. 配置NGINX作为反向代理

5.1 创建NGINX配置文件

sudo nano /etc/nginx/sites-available/my-app

5.2 示例配置

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    location / {
        proxy_pass http://localhost:3000;  # 假设Node.js运行在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_cache_bypass $http_upgrade;
    }
}

5.3 启用配置

sudo ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled
sudo nginx -t  # 测试配置
sudo systemctl reload nginx

6. 高级配置

6.1 启用HTTPS (使用Let's Encrypt)

# 安装Certbot
sudo apt install -y certbot python3-certbot-nginx  # Ubuntu
sudo yum install -y certbot python3-certbot-nginx  # CentOS

# 获取证书
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

# 设置自动续期
sudo certbot renew --dry-run

6.2 负载均衡配置

upstream node_cluster {
    server localhost:3000;
    server localhost:3001;
    server localhost:3002;
}

server {
    location / {
        proxy_pass http://node_cluster;
        # 其他代理设置...
    }
}

6.3 静态文件缓存

location /static/ {
    alias /path/to/static/files/;
    expires 30d;
    add_header Cache-Control "public";
}

7. 监控与维护

7.1 监控工具

# 安装NGINX监控工具
sudo apt install -y nginx-module-njs  # Ubuntu

7.2 日志管理

# 设置日志轮转
sudo nano /etc/logrotate.d/nginx-custom

7.3 性能调优

  • 调整NGINX worker_processes (通常设置为CPU核心数)
  • 优化PM2集群模式: pm2 start app.js -i max

8. 常见问题解决

8.1 502 Bad Gateway

  • 检查Node.js应用是否运行
  • 检查PM2状态: pm2 list
  • 检查端口是否正确

8.2 连接被拒绝

  • 检查防火墙设置
  • 验证NGINX配置: sudo nginx -t

8.3 性能问题

  • 检查系统资源: htoptop
  • 检查NGINX和PM2日志

结论

通过结合NGINX、PM2和VPS,您可以构建一个高性能、可靠的Web服务器环境。NGINX处理静态内容和反向代理,PM2管理Node.js应用进程,VPS提供稳定的基础设施。这种组合特别适合需要高并发处理能力的现代Web应用。

记得定期更新系统和软件,监控服务器性能,并根据应用需求调整配置参数。