#!/usr/bin/env bash # 整体部署:构建并部署 zMainPage(含子模块)+ zTools2(后端)到生产环境。 # # 生产拓扑: # Apache(:80) -> 静态文件 /var/www/html(zMainPage 构建产物) # -> 反代 /api、/pdf、/pdf-admin、/ws 等到 zTools2(127.0.0.1:6867) # zTools2(127.0.0.1:6867) -> MySQL(3306) + uploads/ 磁盘 # # 前提:zMainPage 与 zTools2 在同一主机(zMainPage 父项目,zTools2 同级目录)。 # 用法:./deploy.sh [--no-restart] (--no-restart 仅更新文件不重启 zTools2) set -euo pipefail cd "$(dirname "$0")" ROOT="$(pwd)" RESTART=1 [[ "${1:-}" == "--no-restart" ]] && RESTART=0 # 兄弟项目路径:zTools2 与 zMainPage 同级 ZTOOLS2="../zTools2" ZTOOLS2_DIR="$(cd "$ZTOOLS2" 2>/dev/null && pwd)" || { echo "未找到 zTools2(期望在 $ZTOOLS2);请确保与 zMainPage 同级目录" >&2 exit 1 } WEB_ROOT="${WEB_ROOT:-/var/www/html}" echo "==> [1/4] 构建 zMainPage(含子模块)" # 子模块依赖需各自安装(构建期编译其 .vue) [[ -d third_party/timeTableFix/node_modules ]] || (cd third_party/timeTableFix && npm install) [[ -d third_party/z449/node_modules ]] || (cd third_party/z449 && npm install) # zPDF_package 是 iframe 集成,不参与 mainPage 构建,但确保其源码存在 [[ -d third_party/zPDF_package ]] || git submodule update --init third_party/zPDF_package npm run build echo "==> [2/4] 部署 zMainPage 构建产物到 $WEB_ROOT" sudo mkdir -p "$WEB_ROOT/js/mainPage" "$WEB_ROOT/css/mainPage" sudo rsync -a --delete dist/js/mainPage/ "$WEB_ROOT/js/mainPage/" sudo rsync -a --delete dist/css/mainPage/ "$WEB_ROOT/css/mainPage/" sudo rsync -a dist/index.html "$WEB_ROOT/index.html" [[ -f dist/favicon.svg ]] && sudo rsync -a dist/favicon.svg "$WEB_ROOT/favicon.svg" # SPA 回退 .htaccess(history 模式) cat > /tmp/zmainpage_htaccess <<'HT' RewriteEngine On RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] RewriteRule ^ index.html [L] HT sudo cp /tmp/zmainpage_htaccess "$WEB_ROOT/.htaccess" echo "==> [3/4] 配置 Apache 反代(zMainPage -> zTools2)" # 确保 proxy / rewrite / headers 模块启用 sudo a2enmod proxy proxy_http rewrite headers >/dev/null 2>&1 || true # 写入 vhost 配置(仅当不存在时,避免覆盖已有 HTTPS 配置) VHOST="/etc/apache2/sites-available/zmainpage.conf" if [[ ! -f "$VHOST" ]]; then sudo tee "$VHOST" >/dev/null <<'VH' DocumentRoot /var/www/html # zTools2 反代:/api、/pdf、/pdf-admin、/static、/upload、/files、/wb、/ws 等转发到后端 ProxyPreserveHost On ProxyPass /api/ http://127.0.0.1:6867/api/ ProxyPassReverse /api/ http://127.0.0.1:6867/api/ ProxyPass /pdf http://127.0.0.1:6867/pdf ProxyPassReverse /pdf http://127.0.0.1:6867/pdf ProxyPass /pdf-admin http://127.0.0.1:6867/pdf-admin ProxyPassReverse /pdf-admin http://127.0.0.1:6867/pdf-admin ProxyPass /static/ http://127.0.0.1:6867/static/ ProxyPassReverse /static/ http://127.0.0.1:6867/static/ ProxyPass /upload http://127.0.0.1:6867/upload ProxyPassReverse /upload http://127.0.0.1:6867/upload ProxyPass /files http://127.0.0.1:6867/files ProxyPassReverse /files http://127.0.0.1:6867/files ProxyPass /wb http://127.0.0.1:6867/wb ProxyPassReverse /wb http://127.0.0.1:6867/wb ProxyPass /wb-admin http://127.0.0.1:6867/wb-admin ProxyPassReverse /wb-admin http://127.0.0.1:6867/wb-admin ProxyPass /health http://127.0.0.1:6867/health ProxyPassReverse /health http://127.0.0.1:6867/health # SPA 回退:非文件非目录的请求回退到 index.html AllowOverride All Require all granted VH sudo a2dissite 000-default >/dev/null 2>&1 || true sudo a2ensite zmainpage >/dev/null echo " 已写入 $VHOST 并启用" else echo " $VHOST 已存在,跳过(如需更新请手动编辑)" fi sudo systemctl reload apache2 echo "==> [4/4] 重启 zTools2 后端" if [[ $RESTART -eq 1 ]]; then (cd "$ZTOOLS2_DIR" && ./stop.sh >/dev/null 2>&1 || true) (cd "$ZTOOLS2_DIR" && ./start.sh) else echo " --no-restart:跳过 zTools2 重启" fi echo echo "部署完成。" echo " - zMainPage: http://localhost(Apache :80,静态文件 + SPA)" echo " - zTools2: http://127.0.0.1:6867(经 Apache 反代 /api /pdf 等)" echo " - PDF 页签: http://localhost/pdf -> iframe 同源嵌入 zTools2 /pdf"