Files
qihuo/deploy.sh
T

85 lines
2.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 国内期货监控系统 - Ubuntu 一键部署
# root 用户 | 目录 /opt/qihuo | 端口 6600 | PM2
set -euo pipefail
APP_DIR="/opt/qihuo"
REPO_URL="https://git.bz121.com/dekun/qihuo.git"
SERVICE_NAME="qihuo"
if [ "$(id -u)" -ne 0 ]; then
echo "请使用 root 用户运行: sudo bash deploy.sh"
exit 1
fi
echo "==> 检查系统依赖..."
apt-get update -qq
need_install() {
if ! command -v "$1" &>/dev/null; then
apt-get install -y "$2"
fi
}
need_install python3 python3
need_install python3-venv python3-venv
need_install git git
if ! command -v pm2 &>/dev/null; then
echo "==> 安装 PM2..."
if ! command -v npm &>/dev/null; then
need_install nodejs nodejs
need_install npm npm
fi
npm install -g pm2
fi
echo "==> 准备应用目录 ${APP_DIR}..."
mkdir -p "$(dirname "$APP_DIR")"
if [ -d "$APP_DIR/.git" ]; then
echo "==> 更新已有仓库..."
cd "$APP_DIR"
git pull origin main || git pull origin master || true
else
if [ -d "$APP_DIR" ] && [ "$(ls -A "$APP_DIR" 2>/dev/null)" ]; then
echo "目录 ${APP_DIR} 已存在且非 git 仓库,请手动处理后重试"
exit 1
fi
git clone "$REPO_URL" "$APP_DIR"
cd "$APP_DIR"
fi
echo "==> 创建 Python 虚拟环境..."
python3 -m venv "$APP_DIR/venv"
source "$APP_DIR/venv/bin/activate"
pip install --upgrade pip -q
pip install -r "$APP_DIR/requirements.txt" -q
if [ ! -f "$APP_DIR/.env" ]; then
echo "==> 生成 .env(请编辑 ADMIN_PASSWORD、THS_REFRESH_TOKEN..."
cp "$APP_DIR/.env.example" "$APP_DIR/.env"
RAND_KEY=$(python3 -c "import secrets; print(secrets.token_hex(32))")
sed -i "s/change-this-to-a-random-secret-key/${RAND_KEY}/" "$APP_DIR/.env"
fi
mkdir -p "$APP_DIR/logs"
echo "==> PM2 启动/重启服务..."
cd "$APP_DIR"
pm2 delete "$SERVICE_NAME" 2>/dev/null || true
pm2 start ecosystem.config.cjs
pm2 save
echo ""
echo "=========================================="
echo " 部署完成"
echo " 目录: ${APP_DIR}"
echo " 用户: root"
echo " 端口: 6600"
echo " 访问: http://<服务器IP>:6600"
echo " 日志: pm2 logs ${SERVICE_NAME}"
echo " 开机自启: pm2 startup && pm2 save"
echo "=========================================="