#!/bin/bash # ============================================================ # 加密货币前置匹配系统 — Ubuntu 一键部署脚本 # 仓库:https://git.bz121.com/dekun/crypto-pre-trade-system.git # 部署路径:/opt/crypto-pre-trade-system # 运行用户:root # 进程守护:PM2 # 访问端口:1125 # ============================================================ set -euo pipefail REPO_URL="https://git.bz121.com/dekun/crypto-pre-trade-system.git" INSTALL_DIR="/opt/crypto-pre-trade-system" PORT=1125 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' log() { echo -e "${GREEN}[INFO]${NC} $*"; } warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } err() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; } # ── 检查 root 权限 ── if [ "$(id -u)" -ne 0 ]; then err "请使用 root 用户运行:sudo bash deploy/install.sh" fi log "========== 开始部署 crypto-pre-trade-system ==========" # ── 1. 安装系统依赖 ── log "安装系统依赖..." export DEBIAN_FRONTEND=noninteractive apt-get update -qq apt-get install -y -qq git curl python3 python3-venv python3-pip # Node.js 18+(若未安装) if ! command -v node &>/dev/null; then log "安装 Node.js 20 LTS..." curl -fsSL https://deb.nodesource.com/setup_20.x | bash - apt-get install -y -qq nodejs fi # PM2(若未安装) if ! command -v pm2 &>/dev/null; then log "安装 PM2..." npm install -g pm2 fi log "Node $(node -v) | Python $(python3 --version) | PM2 $(pm2 -v)" # ── 2. 拉取代码 ── if [ -d "$INSTALL_DIR/.git" ]; then log "更新代码..." cd "$INSTALL_DIR" git pull origin main 2>/dev/null || git pull origin master 2>/dev/null || git pull else log "克隆仓库..." git clone "$REPO_URL" "$INSTALL_DIR" cd "$INSTALL_DIR" fi # ── 3. 创建日志目录 ── mkdir -p "$INSTALL_DIR/logs" mkdir -p "$INSTALL_DIR/backend/data" # ── 4. Python 虚拟环境 + 依赖 ── log "配置 Python 虚拟环境..." cd "$INSTALL_DIR/backend" if [ ! -d "venv" ]; then python3 -m venv venv fi venv/bin/pip install -q --upgrade pip venv/bin/pip install -q -r requirements.txt # ── 5. 构建前端 ── log "构建前端..." cd "$INSTALL_DIR/frontend" npm install --silent npm run build # ── 6. PM2 启动 / 重启 ── log "启动 PM2 守护进程..." cd "$INSTALL_DIR" pm2 delete crypto-pre-trade 2>/dev/null || true pm2 start ecosystem.config.cjs pm2 save # 设置 PM2 开机自启(已配置则跳过) pm2 startup systemd -u root --hp /root 2>/dev/null | tail -1 | bash 2>/dev/null || true # ── 7. 健康检查 ── log "等待服务启动..." sleep 3 if curl -sf "http://127.0.0.1:${PORT}/api/health" > /dev/null; then log "健康检查通过" else warn "健康检查未通过,请查看日志:pm2 logs crypto-pre-trade" fi echo "" log "========== 部署完成 ==========" echo -e " 访问地址:${GREEN}http://<服务器IP>:${PORT}${NC}" echo -e " API 文档:${GREEN}http://<服务器IP>:${PORT}/docs${NC}" echo -e " 安装目录:${INSTALL_DIR}" echo -e " 常用命令:" echo -e " pm2 status # 查看状态" echo -e " pm2 logs crypto-pre-trade # 查看日志" echo -e " pm2 restart crypto-pre-trade # 重启服务" echo -e " bash deploy/install.sh # 更新并重新部署" echo ""