Files
ziwei-power/deploy.sh
mac 668576b866 v1.0.3 — 修复 gunicorn 多 worker session 丢失
- secret_key 从 os.urandom() 改为固定值/环境变量
- deploy.sh 首次部署自动生成 .env 并持久化
- 解决多 worker 下 session 解密失败导致保存 302
2026-06-02 23:40:14 +08:00

68 lines
1.5 KiB
Bash
Raw Permalink 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.
#!/bin/bash
# ziwei-power 一键部署脚本
# 用法: bash deploy.sh [port]
#
# 首次运行: 自动生成 .env (SECRET_KEY),之后不变
# 多 worker 安全: 所有 worker 共享同一密钥
set -e
PORT="${1:-5058}"
PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
LOG_FILE="/tmp/ziwei-power.log"
echo "=== ziwei-power 部署 ==="
echo "目录: $PROJECT_DIR"
echo "端口: $PORT"
# 1. 停止旧进程
OLD_PID=$(lsof -ti :$PORT 2>/dev/null || true)
if [ -n "$OLD_PID" ]; then
echo "停止旧进程 PID=$OLD_PID..."
kill $OLD_PID 2>/dev/null || true
sleep 1
fi
# 2. 创建/激活 venv
cd "$PROJECT_DIR"
if [ ! -d "venv" ]; then
echo "创建虚拟环境..."
python3 -m venv venv
fi
echo "安装依赖..."
venv/bin/pip install -q -r requirements.txt
# 3. 生成/读取固定 SECRET_KEY多 worker 共享)
if [ ! -f ".env" ]; then
echo "生成 SECRET_KEY..."
python3 -c "import os; print('SECRET_KEY=' + os.urandom(24).hex())" > .env
fi
set -a; source .env; set +a
export SECRET_KEY
# 4. 自动建表(首次运行)
echo "初始化数据库..."
venv/bin/python -c "
from database import init_db
init_db()
print('数据库就绪')
"
# 4. 启动服务
echo "启动服务 (PID=$$)..."
nohup venv/bin/python app.py > "$LOG_FILE" 2>&1 &
NEW_PID=$!
sleep 1
if kill -0 $NEW_PID 2>/dev/null; then
echo "部署完成!"
echo " 访问: http://localhost:$PORT"
echo " 日志: $LOG_FILE"
echo " PID : $NEW_PID"
else
echo "启动失败,查看日志:"
cat "$LOG_FILE"
exit 1
fi