Files
wordloop/deploy/install-production.sh
T
John bd7635986a Initial commit: WordLoop 单词学习应用
Vue 前端 + FastAPI 后端,含部署脚本与词典数据。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-04 14:30:53 -07:00

99 lines
2.8 KiB
Bash
Executable File
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
# 在 105 服务器上执行(/root/wordloop
set -euo pipefail
ROOT="${WORDLOOP_ROOT:-/root/wordloop}"
cd "$ROOT"
MYSQL_HOST="${MYSQL_HOST:?MYSQL_HOST required}"
MYSQL_PORT="${MYSQL_PORT:-3306}"
MYSQL_USER="${MYSQL_USER:?MYSQL_USER required}"
MYSQL_PASSWORD="${MYSQL_PASSWORD:?MYSQL_PASSWORD required}"
MYSQL_DATABASE="${MYSQL_DATABASE:-wordloop}"
WORDLOOP_SECRET_KEY="${WORDLOOP_SECRET_KEY:-$(openssl rand -hex 32)}"
echo ">>> 写入 backend/.env"
cat > "$ROOT/backend/.env" <<EOF
MYSQL_HOST=${MYSQL_HOST}
MYSQL_PORT=${MYSQL_PORT}
MYSQL_USER=${MYSQL_USER}
MYSQL_PASSWORD=${MYSQL_PASSWORD}
MYSQL_DATABASE=${MYSQL_DATABASE}
WORDLOOP_SECRET_KEY=${WORDLOOP_SECRET_KEY}
EOF
chmod 600 "$ROOT/backend/.env"
echo ">>> Python 虚拟环境与依赖"
cd "$ROOT/backend"
if [ ! -d venv ]; then
python3 -m venv venv
fi
source venv/bin/activate
pip install -q -r requirements.txt
echo ">>> 创建数据库(若不存在)并建表"
python - <<'PY'
import pymysql
from config import get_settings
s = get_settings()
conn = pymysql.connect(
host=s.mysql_host,
port=s.mysql_port,
user=s.mysql_user,
password=s.mysql_password,
charset="utf8mb4",
)
try:
with conn.cursor() as cur:
cur.execute(
f"CREATE DATABASE IF NOT EXISTS `{s.mysql_database}` "
"CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
)
conn.commit()
print(f"database `{s.mysql_database}` ready")
finally:
conn.close()
from database import engine, Base
from sqlalchemy import text
import models # noqa: F401
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
Base.metadata.create_all(bind=engine)
print("tables ready")
PY
echo ">>> 构建前端"
cd "$ROOT/frontend"
if ! command -v npm >/dev/null 2>&1; then
echo "请先安装 Node.js/npm"
exit 1
fi
npm ci --silent
npm run build
echo ">>> Nginx 可读静态目录"
chmod 755 /root /root/wordloop /root/wordloop/frontend /root/wordloop/frontend/dist 2>/dev/null || true
echo ">>> 配置 Nginx + HTTPS (w.tkmind.cn)"
if command -v nginx >/dev/null 2>&1; then
cp "$ROOT/deploy/wordloop-locations.inc" /etc/nginx/conf.d/wordloop-locations.inc
chmod +x "$ROOT/deploy/ssl-w.tkmind.cn.sh"
bash "$ROOT/deploy/ssl-w.tkmind.cn.sh"
systemctl enable nginx 2>/dev/null || true
else
echo "警告: 未安装 nginx,请手动安装后执行 deploy/ssl-w.tkmind.cn.sh"
fi
echo ">>> 配置 systemd 后端服务"
cp "$ROOT/deploy/systemd/wordloop-backend-root.service" /etc/systemd/system/wordloop-backend.service
systemctl daemon-reload
systemctl enable wordloop-backend
systemctl restart wordloop-backend
echo ">>> 完成"
systemctl --no-pager status wordloop-backend || true
curl -sf http://127.0.0.1:18004/ >/dev/null && echo "API: ok" || echo "API: 请检查 journalctl -u wordloop-backend"