一、服务器环境准备
1. 安装 Node.js 和 NPM
bash
# Ubuntu/Debian
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
# CentOS/RHEL
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo yum install -y nodejs
# 验证安装
node -v
npm -v
2. 安装 PM2
bash
sudo npm install -g pm2
3. 安装 Nginx
bash
# Ubuntu/Debian
sudo apt update
sudo apt install -y nginx
# CentOS/RHEL
sudo yum install -y nginx
# 启动 Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
二、部署 Nuxt 应用
1. 上传文件到服务器
确保 /www/root/test 目录包含以下结构:
text
/www/root/test/
├── .output/
│ ├── server/
│ │ └── index.mjs
│ └── public/
│ └── _nuxt/
└── package.json
2. 安装生产依赖
bash
cd /www/root/test
npm install --production
三、配置 PM2 启动 Nuxt
1. 创建 PM2 配置文件 ecosystem.config.js
javascript
module.exports = {
apps: [{
name: 'nuxt-app',
script: '.output/server/index.mjs',
instances: 'max', // 使用所有CPU核心
exec_mode: 'cluster',
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 3000,
HOST: '0.0.0.0'
}
}]
}
2. 启动应用
bash
cd /www/root/test
# 使用配置文件启动
pm2 start ecosystem.config.js
# 或直接启动
pm2 start .output/server/index.mjs --name nuxt-app --watch --time
# 设置开机自启
pm2 startup
# 按照提示执行生成的命令
pm2 save
3. 管理 PM2 进程
bash
# 查看所有应用
pm2 list
# 查看日志
pm2 logs nuxt-app
pm2 logs nuxt-app --lines 100
# 监控
pm2 monit
# 重启应用
pm2 restart nuxt-app
# 停止应用
pm2 stop nuxt-app
# 删除应用
pm2 delete nuxt-app
# 保存当前进程列表
pm2 save
四、配置 Nginx 反向代理
1. 创建 Nginx 配置文件
bash
sudo nano /etc/nginx/sites-available/nuxt-app
2. 配置内容如下:
nginx
server {
listen 80;
server_name your-domain.com www.your-domain.com; # 修改为你的域名
root /www/root/test;
# 设置日志
access_log /var/log/nginx/nuxt-app-access.log;
error_log /var/log/nginx/nuxt-app-error.log;
# 反向代理到 Nuxt
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;
# 超时设置
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# 直接提供静态文件(提高性能)
location /_nuxt/ {
alias /www/root/test/.output/public/_nuxt/;
expires 365d;
add_header Cache-Control "public, immutable";
access_log off;
}
# 阻止访问敏感文件
location ~ /\. {
deny all;
}
# 压缩配置
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
}
3. 启用站点配置
bash
# 创建软链接
sudo ln -s /etc/nginx/sites-available/nuxt-app /etc/nginx/sites-enabled/
# 测试配置
sudo nginx -t
# 重新加载 Nginx
sudo systemctl reload nginx
五、防火墙配置
bash
# 开放 80 端口(HTTP)
sudo ufw allow 80/tcp
# 如果使用 SSL,开放 443 端口
sudo ufw allow 443/tcp
# 启用防火墙
sudo ufw enable
六、SSL 证书配置(可选,推荐)
1. 安装 Certbot
bash
# Ubuntu/Debian
sudo apt install -y certbot python3-certbot-nginx
# CentOS/RHEL
sudo yum install -y certbot python3-certbot-nginx
2. 获取 SSL 证书
bash
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
3. 自动续期测试
bash
sudo certbot renew --dry-run
七、部署脚本
创建 deploy.sh 脚本:
bash
#!/bin/bash
set -e # 遇到错误立即退出
echo "=== 开始部署 Nuxt 应用 ==="
# 进入项目目录
cd /www/root/test
echo "1. 停止当前应用..."
pm2 stop nuxt-app || true
echo "2. 安装依赖..."
npm ci --production
echo "3. 重启应用..."
pm2 restart nuxt-app || pm2 start .output/server/index.mjs --name nuxt-app --time
echo "4. 等待应用启动..."
sleep 5
echo "5. 检查应用状态..."
pm2 list
echo "6. 查看应用日志..."
pm2 logs nuxt-app --lines 10
echo "=== 部署完成 ==="
# 测试访问
echo "测试访问..."
curl -f http://localhost:3000 || echo "应用启动失败,请检查日志"
给脚本执行权限:
bash
chmod +x deploy.sh
./deploy.sh
八、常用维护命令
bash
# 查看 PM2 状态
pm2 status
# 查看实时日志
pm2 logs nuxt-app --lines 100
# 查看 CPU/内存使用
pm2 monit
# 重启所有应用
pm2 restart all
# 重新加载 Nginx
sudo systemctl reload nginx
# 查看 Nginx 错误日志
sudo tail -f /var/log/nginx/nuxt-app-error.log
九、故障排除
1. 如果应用无法启动
bash
# 查看详细错误
cd /www/root/test
node .output/server/index.mjs
# 检查端口占用
netstat -tlnp | grep :3000
# 使用不同端口
PORT=3001 pm2 start ecosystem.config.js
2. 如果 Nginx 返回 502 错误
bash
# 检查 PM2 是否运行
pm2 list
# 检查应用是否监听端口
curl http://localhost:3000
# 查看 PM2 日志
pm2 logs nuxt-app
3. 更新 Nginx 配置后
bash
# 测试配置
sudo nginx -t
# 重新加载
sudo systemctl reload nginx
# 重启
sudo systemctl restart nginx
这样就完成了使用 PM2 + Nginx 的完整部署流程。应用将通过 Nginx 在 80 端口提供服务,PM2 负责管理 Node.js 进程。