DeltaFStationl量化工具部署启动脚本
deploy_deltafstation.sh
#!/bin/bash# ===========================================================================# DeltaFStation 宝塔 Linux 部署脚本# 适用:CentOS 7+/Ubuntu 18.04+ 已安装宝塔面板# 方式:Supervisor 守护 python run.py + Nginx 反向代理(含 SSE 配置)# 用法:bash deploy_deltafstation.sh# ===========================================================================set-e# ---------------------------- 可配置变量 ----------------------------# 项目安装目录(宝塔默认站点根目录习惯放在 /www/wwwroot/)INSTALL_DIR="/www/wwwroot/deltafstation"# Python 解释器(优先宝塔 Python 管理器的 3.12;找不到则回退系统 python3)PYTHON_BIN="${PYTHON_BIN:-}"# 对外监听端口(Flask 内部端口,Nginx 反代到这里)APP_PORT=5000# Git 仓库(优先 gitcode 国内快;github 备选)GIT_REPO="${GIT_REPO:-https://gitcode.com/weixin_48616345/deltafstation.git}"# 运行用户(宝塔默认 www)RUN_USER="www"# ---------------------------- 颜色输出 ----------------------------RED='\033[0;31m';GREEN='\033[0;32m';YELLOW='\033[1;33m';NC='\033[0m'info(){echo-e"${GREEN}[INFO]${NC}$1";}warn(){echo-e"${YELLOW}[WARN]${NC}$1";}error(){echo-e"${RED}[ERROR]${NC}$1";exit1;}# ---------------------------- 0. root 检查 ----------------------------[[$EUID-ne0]]&&error"请用 root 执行:sudo bash deploy_deltafstation.sh"info"开始部署 DeltaFStation(宝塔 Linux)"# ---------------------------- 1. 检测包管理器 ----------------------------ifcommand-vapt-get&>/dev/null;thenPKG_MGR="apt";info"检测到 Debian/Ubuntu 系"elifcommand-vyum&>/dev/null;thenPKG_MGR="yum";info"检测到 CentOS/RHEL 系"elseerror"未识别的系统,仅支持 apt/yum"fi# ---------------------------- 2. 安装系统级依赖 ----------------------------info"安装编译工具与 Python 开发库(TA-Lib 编译需要)"if[["$PKG_MGR"=="apt"]];thenapt-getupdate-yapt-getinstall-ybuild-essential python3-dev python3-venv python3-pip\gitwgetcurllibssl-dev libffi-develseyum groupinstall-y"Development Tools"yuminstall-ypython3-devel python3-pipgitwgetcurlopenssl-devel libffi-develfi# ---------------------------- 3. 选择 Python 解释器 ----------------------------# 优先宝塔 Python 管理器安装的 3.12,其次系统 python3.12,最后系统 python3if[[-z"$PYTHON_BIN"]];thenforcandin/www/server/pyproject_evn/versions/3.12*/bin/python3.12\/usr/local/bin/python3.12 /usr/bin/python3.12;doif[[-x"$cand"]];thenPYTHON_BIN="$cand";break;fidone[[-z"$PYTHON_BIN"]]&&PYTHON_BIN=$(command-vpython3)fiPY_VER=$($PYTHON_BIN-c'import sys;print("%d.%d"%sys.version_info[:2])')info"使用 Python:$PYTHON_BIN(版本$PY_VER)"[["$PY_VER"<"3.10"]]&&warn"Python 版本偏低,项目推荐 3.12,建议在宝塔软件商店安装 Python 3.12 后重跑"# ---------------------------- 4. 编译安装 TA-Lib C 库 ----------------------------# 这是最容易踩坑的依赖,必须先装 C 库再 pip install TA-Libif!ldconfig-p|grep-q"libta_lib";theninfo"未检测到 TA-Lib C 库,开始源码编译(约 1-2 分钟)"cd/usr/local/src[[-fta-lib-0.4.0-src.tar.gz]]||wget-qhttp://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz[[-dta-lib]]||tar-xzfta-lib-0.4.0-src.tar.gzcdta-lib ./configure--prefix=/usrmake-j$(nproc)&&makeinstallldconfig info"TA-Lib C 库安装完成"elseinfo"TA-Lib C 库已存在,跳过编译"fi# ---------------------------- 5. 克隆项目 ----------------------------if[[!-d"$INSTALL_DIR/.git"]];theninfo"克隆项目到$INSTALL_DIR"mkdir-p"$(dirname$INSTALL_DIR)"[[-d"$INSTALL_DIR"]]&&rm-rf"$INSTALL_DIR"gitclone"$GIT_REPO""$INSTALL_DIR"||{warn"gitcode 克隆失败,回退 GitHub"gitclone https://github.com/Delta-F/deltafstation.git"$INSTALL_DIR"}elseinfo"项目目录已存在,执行 git pull 更新"cd"$INSTALL_DIR"&&gitpull--rebase||warn"git pull 失败,继续使用现有代码"ficd"$INSTALL_DIR"# ---------------------------- 6. 创建虚拟环境 ----------------------------VENV_DIR="$INSTALL_DIR/venv"info"创建虚拟环境:$VENV_DIR"$PYTHON_BIN-mvenv"$VENV_DIR"VENV_PY="$VENV_DIR/bin/python"$VENV_PY-mpipinstall--upgradepip-q# ---------------------------- 7. 安装 Python 依赖 ----------------------------info"安装 Python 依赖(TA-Lib 此处为 wheel 绑定,已装好 C 库)"$VENV_PY-mpipinstall-rrequirements.txt-ihttps://pypi.tuna.tsinghua.edu.cn/simple# ---------------------------- 8. 创建数据目录 ----------------------------info"创建数据与日志目录"mkdir-pdata/raw data/processed data/results data/strategies data/simulations logs# 目录归属给 www 用户,避免宝塔 Nginx/Supervisor 权限问题chown-R$RUN_USER:$RUN_USER"$INSTALL_DIR"2>/dev/null||warn"chown 失败(可忽略,若用 root 运行)"# ---------------------------- 9. 生成 Supervisor 配置 ----------------------------info"生成 Supervisor 配置"SUPERVISOR_CONF_DIR="/www/server/supervisor/conf.d"mkdir-p"$SUPERVISOR_CONF_DIR"2>/dev/null||truecat>"$SUPERVISOR_CONF_DIR/deltafstation.ini"<<EOF [program:deltafstation] command=$VENV_PY$INSTALL_DIR/run.py directory=$INSTALL_DIRuser=$RUN_USERautostart=true autorestart=true startsecs=5 stopwaitsecs=30 stopsignal=TERM ; 环境变量:生产模式关闭 debug,固定端口 environment=FLASK_ENV="production",PORT="$APP_PORT",HOST="127.0.0.1",FLASK_USE_RELOADER="0" stdout_logfile=$INSTALL_DIR/logs/supervisor_out.log stderr_logfile=$INSTALL_DIR/logs/supervisor_err.log stdout_logfile_maxbytes=20MB stdout_logfile_backups=5 redirect_stderr=false EOFinfo"Supervisor 配置已写入:$SUPERVISOR_CONF_DIR/deltafstation.ini"# ---------------------------- 10. 生成 Nginx 反代配置片段 ----------------------------# SSE 关键:关闭缓冲、长超时、HTTP/1.1NGINX_SNIPPET="$INSTALL_DIR/logs/nginx_deltafstation.conf"cat>"$NGINX_SNIPPET"<<'EOF' # ===== DeltaFStation Nginx 反向代理配置(含 SSE 支持)===== # 在宝塔「网站 → 你的站点 → 配置文件」中,把 server{} 里的 location / 替换为以下内容 # 或在宝塔「网站 → 反向代理」中新增后,手动补充 SSE 相关指令 location / { proxy_pass http://127.0.0.1:5000; 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; # ===== SSE / 流式响应关键配置 ===== proxy_http_version 1.1; proxy_set_header Connection ""; # 启用长连接 proxy_buffering off; # 关闭响应缓冲,SSE 必需 proxy_cache off; # 关闭缓存 proxy_request_buffering off; chunked_transfer_encoding on; # SSE/长连接超时(默认 60s 会断流,调大到 1 天) proxy_read_timeout 86400s; proxy_send_timeout 86400s; # WebSocket 支持(如未来需要) proxy_set_header Upgrade $http_upgrade; } EOFinfo"Nginx 配置片段已生成:$NGINX_SNIPPET(稍后按提示配置到宝塔站点)"# ---------------------------- 11. 完成,输出后续操作指引 ----------------------------echo""echo-e"${GREEN}==========================================================${NC}"echo-e"${GREEN}DeltaFStation 部署脚本执行完成${NC}"echo-e"${GREEN}==========================================================${NC}"echo""echo"项目目录:$INSTALL_DIR"echo"虚拟环境 Python:$VENV_PY"echo"Flask 监听: 127.0.0.1:$APP_PORT(仅本机,由 Nginx 对外)"echo""echo-e"${YELLOW}【后续手动操作】${NC}"echo""echo"1) 在宝塔软件商店安装「Supervisor管理器」(如未安装),然后:"echo" - 进入 Supervisor 管理器 → 配置 → 重载配置"echo" - 或命令行: supervisorctl update && supervisorctl restart deltafstation"echo" - 查看状态: supervisorctl status deltafstation"echo" - 查看日志: tail -f$INSTALL_DIR/logs/supervisor_out.log"echo""echo"2) 在宝塔「网站」添加站点(纯静态即可,仅用其域名 + Nginx),然后:"echo" - 打开站点「设置 → 配置文件」"echo" - 参考$NGINX_SNIPPET的内容替换 location / 段(注意 SSE 指令必须保留)"echo" - 保存后重载 Nginx: nginx -s reload"echo""echo"3) 放行端口/绑定域名后,浏览器访问 http://你的域名 即可"echo""echo"4) AI Agent / LLM 配置:"echo" - 首次启动后,在 Web 界面的 AI 设置里配置 OpenAI 兼容 API Key"echo" - 或编辑 config/config.py 里的 LLM_* 配置项"echo""echo"5) 实盘 miniQMT(可选):"echo" - 需在一台 Windows 机器上运行 miniQMT 客户端"echo" - 详细见项目 docs/qmt-strategy-live.md"echo""