lovart3/部署指南.md
2025-12-17 20:15:18 +08:00

6.9 KiB
Raw Blame History

服务器部署指南

服务器信息

  • 服务器地址: 156.225.27.52
  • SSH 端口: 5204
  • 用户: root

部署前准备

1. 本地准备

确保本地项目可以正常运行:

# 检查项目状态
pnpm install
pnpm db:migrate
pnpm build

2. 准备环境变量

创建 .env.production 文件,包含生产环境配置:

# 数据库
POSTGRES_URL=postgresql://user:password@host:port/database

# NextAuth
AUTH_SECRET=your-production-secret-key
AUTH_URL=https://your-domain.com

# AI Gateway (如果需要)
AI_GATEWAY_API_KEY=your-gateway-api-key

# Redis (可选)
REDIS_URL=redis://localhost:6379

# Vercel Blob (可选)
BLOB_READ_WRITE_TOKEN=your-blob-token

部署步骤

方法一:使用部署脚本(推荐)

  1. 在本地运行部署脚本:
chmod +x deploy.sh
./deploy.sh

方法二:手动部署

步骤 1: 连接到服务器

ssh -p 5204 root@156.225.27.52

步骤 2: 安装必要软件

# 更新系统
apt update && apt upgrade -y

# 安装 Node.js (使用 nvm 或直接安装)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20

# 安装 pnpm
npm install -g pnpm

# 安装 PostgreSQL (如果还没有)
apt install postgresql postgresql-contrib -y

# 安装 PM2 (进程管理)
npm install -g pm2

# 安装 Nginx (反向代理)
apt install nginx -y

步骤 3: 克隆项目

# 创建项目目录
mkdir -p /var/www/ai-chatbot
cd /var/www/ai-chatbot

# 如果使用 Git
git clone <your-repo-url> .

# 或者使用 scp 上传(从本地)
# scp -P 5204 -r . root@156.225.27.52:/var/www/ai-chatbot

步骤 4: 安装依赖

cd /var/www/ai-chatbot
pnpm install --production=false

步骤 5: 配置环境变量

# 创建 .env.local 文件
nano .env.local

# 粘贴环境变量内容(从 .env.production)
# 保存并退出 (Ctrl+X, Y, Enter)

步骤 6: 设置数据库

# 创建数据库和用户
sudo -u postgres psql

在 PostgreSQL 命令行中:

CREATE DATABASE ai_chatbot;
CREATE USER chatbot_user WITH PASSWORD 'your_secure_password';
GRANT ALL PRIVILEGES ON DATABASE ai_chatbot TO chatbot_user;
\q

更新 .env.local 中的 POSTGRES_URL:

POSTGRES_URL=postgresql://chatbot_user:your_secure_password@localhost:5432/ai_chatbot

步骤 7: 运行数据库迁移

cd /var/www/ai-chatbot
pnpm db:migrate

步骤 8: 构建项目

pnpm build

步骤 9: 使用 PM2 启动应用

# 创建 PM2 配置文件
cat > ecosystem.config.js << 'EOF'
module.exports = {
  apps: [{
    name: 'ai-chatbot',
    script: 'node_modules/next/dist/bin/next',
    args: 'start',
    cwd: '/var/www/ai-chatbot',
    instances: 1,
    exec_mode: 'fork',
    env: {
      NODE_ENV: 'production',
      PORT: 3000
    },
    error_file: '/var/log/pm2/ai-chatbot-error.log',
    out_file: '/var/log/pm2/ai-chatbot-out.log',
    log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
    merge_logs: true,
    autorestart: true,
    max_memory_restart: '1G'
  }]
};
EOF

# 启动应用
pm2 start ecosystem.config.js

# 保存 PM2 配置
pm2 save

# 设置开机自启
pm2 startup

步骤 10: 配置 Nginx 反向代理

# 创建 Nginx 配置
cat > /etc/nginx/sites-available/ai-chatbot << 'EOF'
server {
    listen 80;
    server_name your-domain.com;  # 替换为你的域名或 IP

    location / {
        proxy_pass http://localhost: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;
    }
}
EOF

# 启用站点
ln -s /etc/nginx/sites-available/ai-chatbot /etc/nginx/sites-enabled/

# 测试配置
nginx -t

# 重启 Nginx
systemctl restart nginx

步骤 11: 配置防火墙

# 允许 HTTP 和 HTTPS
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 5204/tcp  # SSH 端口

# 启用防火墙
ufw enable

常用命令

PM2 管理

# 查看应用状态
pm2 status

# 查看日志
pm2 logs ai-chatbot

# 重启应用
pm2 restart ai-chatbot

# 停止应用
pm2 stop ai-chatbot

# 删除应用
pm2 delete ai-chatbot

更新部署

cd /var/www/ai-chatbot

# 拉取最新代码
git pull

# 安装新依赖
pnpm install

# 运行数据库迁移
pnpm db:migrate

# 重新构建
pnpm build

# 重启应用
pm2 restart ai-chatbot

查看日志

# PM2 日志
pm2 logs ai-chatbot

# Nginx 日志
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log

# 应用日志
tail -f /var/log/pm2/ai-chatbot-out.log
tail -f /var/log/pm2/ai-chatbot-error.log

安全建议

  1. 使用 SSH 密钥而非密码:

    # 在本地生成密钥
    ssh-keygen -t rsa -b 4096
    
    # 复制公钥到服务器
    ssh-copy-id -p 5204 root@156.225.27.52
    
  2. 禁用 root 密码登录(配置 SSH 密钥后):

    # 编辑 SSH 配置
    nano /etc/ssh/sshd_config
    
    # 设置
    PasswordAuthentication no
    PermitRootLogin prohibit-password
    
    # 重启 SSH
    systemctl restart sshd
    
  3. 配置 SSL 证书(使用 Let's Encrypt):

    apt install certbot python3-certbot-nginx -y
    certbot --nginx -d your-domain.com
    
  4. 定期更新系统:

    apt update && apt upgrade -y
    

故障排查

应用无法启动

  1. 检查环境变量:

    pm2 env ai-chatbot
    
  2. 检查端口占用:

    netstat -tulpn | grep 3000
    
  3. 检查数据库连接:

    psql $POSTGRES_URL
    

502 Bad Gateway

  1. 检查应用是否运行:

    pm2 status
    
  2. 检查 Nginx 配置:

    nginx -t
    
  3. 检查防火墙:

    ufw status
    

性能优化

  1. 启用 Next.js 缓存:

    • 确保 next.config.ts 中 cacheComponents: true
  2. 使用 Redis 缓存(如果配置了 Redis):

    • 确保 REDIS_URL 环境变量正确
  3. 监控资源使用:

    pm2 monit
    

备份

数据库备份

# 创建备份脚本
cat > /root/backup-db.sh << 'EOF'
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/root/backups"
mkdir -p $BACKUP_DIR
pg_dump $POSTGRES_URL > $BACKUP_DIR/db_backup_$DATE.sql
# 保留最近 7 天的备份
find $BACKUP_DIR -name "db_backup_*.sql" -mtime +7 -delete
EOF

chmod +x /root/backup-db.sh

# 添加到 crontab(每天凌晨 2 点备份)
crontab -e
# 添加:0 2 * * * /root/backup-db.sh

联系支持

如遇到问题,请检查:

  1. 应用日志:pm2 logs ai-chatbot
  2. Nginx 日志:/var/log/nginx/error.log
  3. 系统日志:journalctl -u nginx