3.6 KiB
3.6 KiB
快速部署指南
快速开始(5分钟部署)
步骤 1: 在服务器上初始化环境
# 连接到服务器
ssh -p 5204 root@156.225.27.52
# 上传并运行初始化脚本
# (在本地执行)
scp -P 5204 setup-server.sh root@156.225.27.52:/root/
ssh -p 5204 root@156.225.27.52 "bash /root/setup-server.sh"
步骤 2: 配置数据库
# 在服务器上执行
sudo -u postgres psql
在 PostgreSQL 中执行:
CREATE DATABASE ai_chatbot;
CREATE USER chatbot_user WITH PASSWORD '你的安全密码';
GRANT ALL PRIVILEGES ON DATABASE ai_chatbot TO chatbot_user;
\q
步骤 3: 上传项目并配置
# 在本地执行
# 1. 上传项目文件
scp -P 5204 -r . root@156.225.27.52:/var/www/ai-chatbot/
# 2. 连接到服务器
ssh -p 5204 root@156.225.27.52
# 3. 进入项目目录
cd /var/www/ai-chatbot
# 4. 创建环境变量文件
nano .env.local
在 .env.local 中添加:
POSTGRES_URL=postgresql://chatbot_user:你的密码@localhost:5432/ai_chatbot
AUTH_SECRET=$(openssl rand -base64 32)
AUTH_URL=http://156.225.27.52
保存文件(Ctrl+X, Y, Enter)
步骤 4: 安装和构建
# 在服务器上执行
cd /var/www/ai-chatbot
pnpm install
pnpm db:migrate
pnpm build
步骤 5: 启动应用
# 复制 PM2 配置文件
cp ecosystem.config.js /var/www/ai-chatbot/
# 启动应用
cd /var/www/ai-chatbot
pm2 start ecosystem.config.js
pm2 save
pm2 startup
步骤 6: 配置 Nginx
# 创建 Nginx 配置
cat > /etc/nginx/sites-available/ai-chatbot << 'EOF'
server {
listen 80;
server_name 156.225.27.52;
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
systemctl restart nginx
步骤 7: 测试
访问 http://156.225.27.52 查看应用是否正常运行。
使用自动部署脚本
如果你想使用自动部署脚本:
# 在本地执行
./deploy.sh
注意: 使用自动部署脚本前,需要:
- 在服务器上已经运行过
setup-server.sh - 已经配置好数据库和环境变量
- 已经创建好 PM2 配置文件
常见问题
1. 连接服务器失败
检查:
- SSH 端口是否正确(5204)
- 服务器 IP 是否正确
- 防火墙是否允许 SSH 连接
2. 应用无法启动
检查:
pm2 logs ai-chatbot
pm2 status
3. 数据库连接失败
检查:
.env.local中的POSTGRES_URL是否正确- PostgreSQL 服务是否运行:
systemctl status postgresql - 数据库用户权限是否正确
4. 502 Bad Gateway
检查:
- 应用是否运行:
pm2 status - Nginx 配置是否正确:
nginx -t - 端口 3000 是否被占用:
netstat -tulpn | grep 3000
更新部署
# 在服务器上执行
cd /var/www/ai-chatbot
git pull # 如果使用 Git
# 或者重新上传文件
pnpm install
pnpm db:migrate
pnpm build
pm2 restart ai-chatbot
安全建议
- 更改默认密码:首次登录后立即更改 root 密码
- 使用 SSH 密钥:配置 SSH 密钥登录,禁用密码登录
- 配置防火墙:只开放必要的端口
- 定期更新:
apt update && apt upgrade -y - 配置 SSL:使用 Let's Encrypt 配置 HTTPS