diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md deleted file mode 100644 index 6a61f44..0000000 --- a/DEPLOYMENT.md +++ /dev/null @@ -1,190 +0,0 @@ -# 部署指南 - -## 📋 部署前准备 - -### 系统要求 -- Ubuntu 20.04+ / CentOS 8+ / Debian 11+ -- Python 3.8+ -- Node.js 16+ 或 Bun -- Nginx -- Supervisor 或 systemd - -### 安装依赖 - -```bash -# Ubuntu/Debian -sudo apt update -sudo apt install python3 python3-venv python3-pip nginx supervisor - -# 安装 Bun (推荐) -curl -fsSL https://bun.sh/install | bash - -# 或者安装 Node.js -curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - -sudo apt-get install -y nodejs -``` - -## 🚀 部署步骤 - -### 1. 克隆项目 -```bash -cd /var/www -sudo git clone ball-tracking-server -cd ball-tracking-server -``` - -### 2. 修改配置文件路径 -编辑以下文件,将路径替换为实际路径: -- `deploy.sh` - 第6行 PROJECT_ROOT -- `supervisor.conf` - command, directory, stdout_logfile, environment 中的路径 -- `gunicorn.conf.py` - 如果需要修改用户 -- `ball-tracking-server.service` - WorkingDirectory, Environment, ExecStart 中的路径 -- `nginx.conf` - root, server_name 等配置 - -### 3. 运行部署脚本 -```bash -# 给脚本执行权限 -chmod +x deploy.sh start.sh stop.sh - -# 运行部署 -sudo ./deploy.sh -``` - -### 4. 配置 Nginx -```bash -# 复制 Nginx 配置 -sudo cp nginx.conf /etc/nginx/sites-available/ball-tracking-server - -# 启用站点 -sudo ln -s /etc/nginx/sites-available/ball-tracking-server /etc/nginx/sites-enabled/ - -# 测试配置 -sudo nginx -t - -# 重启 Nginx -sudo systemctl restart nginx -``` - -## 🔧 服务管理 - -### 使用 Supervisor(推荐) -```bash -# 查看状态 -sudo supervisorctl status ball-tracking-server - -# 启动服务 -sudo supervisorctl start ball-tracking-server - -# 停止服务 -sudo supervisorctl stop ball-tracking-server - -# 重启服务 -sudo supervisorctl restart ball-tracking-server - -# 查看日志 -sudo supervisorctl tail -f ball-tracking-server - -# 或者使用脚本 -./start.sh # 启动 -./stop.sh # 停止 -``` - -### 使用 systemd(备选方案) -```bash -# 复制服务文件 -sudo cp ball-tracking-server.service /etc/systemd/system/ - -# 重载服务配置 -sudo systemctl daemon-reload - -# 启用开机自启 -sudo systemctl enable ball-tracking-server - -# 启动服务 -sudo systemctl start ball-tracking-server - -# 查看状态 -sudo systemctl status ball-tracking-server - -# 查看日志 -sudo journalctl -u ball-tracking-server -f -``` - -## 📝 日志文件位置 -- Supervisor 日志: `logs/supervisor.log` -- Gunicorn 访问日志: `logs/gunicorn_access.log` -- Gunicorn 错误日志: `logs/gunicorn_error.log` -- Nginx 访问日志: `/var/log/nginx/ball-tracking-server_access.log` -- Nginx 错误日志: `/var/log/nginx/ball-tracking-server_error.log` - -## 🔍 故障排查 - -### 检查端口占用 -```bash -sudo netstat -tulpn | grep :5000 -``` - -### 检查进程 -```bash -ps aux | grep gunicorn -ps aux | grep supervisord -``` - -### 检查防火墙 -```bash -sudo ufw status -sudo ufw allow 80 -sudo ufw allow 443 -``` - -### 权限问题 -```bash -sudo chown -R www-data:www-data /var/www/ball-tracking-server -sudo chmod -R 755 /var/www/ball-tracking-server -``` - -## 🔄 更新部署 - -```bash -# 简单更新(自动构建前端) -sudo ./deploy.sh - -# 手动更新 -git pull origin main -cd app/web && bun run build && cd ../.. -sudo supervisorctl restart ball-tracking-server -``` - -## 🌐 域名和 HTTPS - -### 配置域名 -1. 修改 `nginx.conf` 中的 `server_name` -2. 重启 Nginx - -### 启用 HTTPS (Let's Encrypt) -```bash -# 安装 Certbot -sudo apt install certbot python3-certbot-nginx - -# 获取证书 -sudo certbot --nginx -d your-domain.com -d www.your-domain.com - -# 自动续期 -sudo crontab -e -# 添加: 0 12 * * * /usr/bin/certbot renew --quiet -``` - -## 📊 监控建议 - -- 使用 `htop` 或 `glances` 监控系统资源 -- 设置日志轮转避免磁盘空间不足 -- 配置监控告警(如 Zabbix、Prometheus) -- 定期备份数据目录 - -## 🚨 安全建议 - -- 关闭不必要的端口 -- 启用防火墙 -- 定期更新系统和依赖 -- 使用 HTTPS -- 设置强密码和 SSH 密钥登录 diff --git a/deploy.sh b/deploy.sh deleted file mode 100755 index 8b6756f..0000000 --- a/deploy.sh +++ /dev/null @@ -1,148 +0,0 @@ -#!/bin/bash - -# 部署脚本 -# 使用方法: ./deploy.sh - -set -e # 遇到错误立即退出 - -PROJECT_NAME="ball-tracking-server" -USER="root" -PYTHON_VERSION="python3" - -echo "🚀 开始部署 $PROJECT_NAME..." - -# 1. 检查当前目录和权限 -echo "📍 当前目录: $(pwd)" -echo "👤 当前用户: $(whoami)" - -# 2. 更新代码 (如果使用 git) -if [ -d ".git" ]; then - echo "📥 更新代码..." - git pull origin main # 或者你的主分支名 -fi - -# 3. 检查 Python 版本 -echo "🐍 检查 Python 版本..." -$PYTHON_VERSION --version -which $PYTHON_VERSION - -# 4. 创建虚拟环境 (如果不存在) -if [ ! -d "venv" ]; then - echo "🐍 创建 Python 虚拟环境..." - $PYTHON_VERSION -m venv venv - if [ ! -f "venv/bin/activate" ]; then - echo "❌ 错误: 虚拟环境创建失败,检查 Python 安装" - echo "尝试使用以下命令手动创建:" - echo "$PYTHON_VERSION -m venv venv" - exit 1 - fi - echo "✅ 虚拟环境创建成功" -else - echo "✅ 虚拟环境已存在" -fi - -# 5. 检查虚拟环境激活脚本 -if [ ! -f "venv/bin/activate" ]; then - echo "❌ 错误: 找不到虚拟环境激活脚本" - echo "虚拟环境目录内容:" - ls -la venv/ || echo "venv 目录不存在或无法访问" - echo "尝试删除并重新创建虚拟环境..." - rm -rf venv - $PYTHON_VERSION -m venv venv -fi - -# 6. 激活虚拟环境并安装依赖 -echo "📦 安装 Python 依赖..." -source venv/bin/activate -echo "🐍 虚拟环境已激活: $(which python)" -pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/ -pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ -pip install gunicorn supervisor -i https://mirrors.aliyun.com/pypi/simple/ # 生产环境依赖 - -# 7. 构建前端 -echo "🏗️ 构建前端..." -cd app/web -if command -v bun &> /dev/null; then - echo "使用 Bun 构建..." - bun install - bun run build -elif command -v npm &> /dev/null; then - echo "使用 npm 构建..." - npm install - npm run build -else - echo "❌ 错误: 未找到 bun 或 npm" - exit 1 -fi - -# 8. 返回项目根目录 -cd "../.." - -# 9. 创建必要的目录 -echo "📁 创建必要目录..." -mkdir -p logs -mkdir -p data/sessions - -# 10. 设置权限 -echo "🔐 设置文件权限..." -chown -R $USER:$USER . -chmod +x deploy.sh -chmod +x start.sh -chmod +x stop.sh - -# 11. 检查并安装 Supervisor -echo "🔍 检查 Supervisor 安装状态..." -if ! command -v supervisorctl &> /dev/null; then - echo "📦 安装 Supervisor..." - if command -v apt-get &> /dev/null; then - # Debian/Ubuntu - sudo apt-get update - sudo apt-get install -y supervisor - elif command -v yum &> /dev/null; then - # CentOS/RHEL - sudo yum install -y supervisor - elif command -v dnf &> /dev/null; then - # Fedora - sudo dnf install -y supervisor - else - echo "❌ 错误: 无法自动安装 Supervisor,请手动安装" - echo "Ubuntu/Debian: sudo apt-get install supervisor" - echo "CentOS/RHEL: sudo yum install supervisor" - echo "Fedora: sudo dnf install supervisor" - exit 1 - fi -fi - -# 12. 检查 Supervisor 配置目录 -if [ ! -d "/etc/supervisor/conf.d" ]; then - echo "📁 创建 Supervisor 配置目录..." - sudo mkdir -p /etc/supervisor/conf.d -fi - -# 13. 启动 Supervisor 服务 -echo "🚀 启动 Supervisor 服务..." -if command -v systemctl &> /dev/null; then - sudo systemctl enable supervisor - sudo systemctl start supervisor -elif command -v service &> /dev/null; then - sudo service supervisor start -fi - -# 14. 复制配置文件到系统目录 (需要 sudo) -echo "⚙️ 配置系统服务..." -if [ -f "/etc/supervisor/conf.d/$PROJECT_NAME.conf" ]; then - sudo cp supervisor.conf /etc/supervisor/conf.d/$PROJECT_NAME.conf - sudo supervisorctl reread - sudo supervisorctl update - sudo supervisorctl restart $PROJECT_NAME -else - sudo cp supervisor.conf /etc/supervisor/conf.d/$PROJECT_NAME.conf - sudo supervisorctl reread - sudo supervisorctl update - sudo supervisorctl start $PROJECT_NAME -fi - -echo "✅ 部署完成!" -echo "📝 查看日志: sudo supervisorctl tail -f $PROJECT_NAME" -echo "🔄 重启服务: sudo supervisorctl restart $PROJECT_NAME" -echo "⏹️ 停止服务: sudo supervisorctl stop $PROJECT_NAME" diff --git a/gunicorn.conf.py b/gunicorn.conf.py deleted file mode 100644 index 82b3a88..0000000 --- a/gunicorn.conf.py +++ /dev/null @@ -1,53 +0,0 @@ -# Gunicorn 配置文件 -import os -import multiprocessing - -# 服务器绑定 -bind = "0.0.0.0:5000" -backlog = 2048 - -# 工作进程 -workers = multiprocessing.cpu_count() * 2 + 1 # 推荐的工作进程数 -worker_class = "sync" -worker_connections = 1000 -timeout = 30 -keepalive = 2 -max_requests = 1000 -max_requests_jitter = 50 - -# 日志 -accesslog = "logs/gunicorn_access.log" -errorlog = "logs/gunicorn_error.log" -loglevel = "info" -access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" %(D)s' - -# 进程命名 -proc_name = "ball-tracking-server" - -# 安全 -user = os.getenv('GUNICORN_USER', 'www-data') -group = os.getenv('GUNICORN_GROUP', 'www-data') -tmp_upload_dir = None - -# 预加载应用 -preload_app = True - -# SSL (如果需要) -# keyfile = "/path/to/ssl/private.key" -# certfile = "/path/to/ssl/certificate.crt" - -# 开发环境配置 (生产环境请注释掉) -# reload = True -# reload_extra_files = ["app/"] - -def when_ready(server): - server.log.info("Gunicorn server is ready. Listening on: %s", server.address) - -def worker_int(worker): - worker.log.info("worker received INT or QUIT signal") - -def pre_fork(server, worker): - server.log.info("Worker spawned (pid: %s)", worker.pid) - -def post_fork(server, worker): - server.log.info("Worker spawned (pid: %s)", worker.pid) diff --git a/start.sh b/start.sh deleted file mode 100755 index 13875e0..0000000 --- a/start.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash -# 启动服务 - -PROJECT_NAME="ball-tracking-server" - -echo "🚀 启动 $PROJECT_NAME..." - -# 检查 supervisor 是否运行 -if ! pgrep -x "supervisord" > /dev/null; then - echo "启动 supervisord..." - sudo systemctl start supervisor -fi - -# 启动应用 -sudo supervisorctl start $PROJECT_NAME - -# 检查状态 -sudo supervisorctl status $PROJECT_NAME - -echo "✅ 服务启动完成!" -echo "📝 查看日志: sudo supervisorctl tail -f $PROJECT_NAME" diff --git a/stop.sh b/stop.sh deleted file mode 100755 index f04f124..0000000 --- a/stop.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash -# 停止服务 - -PROJECT_NAME="ball-tracking-server" - -echo "⏹️ 停止 $PROJECT_NAME..." - -# 停止应用 -sudo supervisorctl stop $PROJECT_NAME - -# 检查状态 -sudo supervisorctl status $PROJECT_NAME - -echo "✅ 服务已停止!" diff --git a/supervisor.conf b/supervisor.conf deleted file mode 100644 index ef23e11..0000000 --- a/supervisor.conf +++ /dev/null @@ -1,29 +0,0 @@ -[program:ball-tracking-server] -; Supervisor 配置文件 -; 复制到 /etc/supervisor/conf.d/ 目录 - -; 基本配置 -command=/path/to/your/ball-tracking/server-neo/venv/bin/gunicorn --config gunicorn.conf.py wsgi:app -directory=/path/to/your/ball-tracking/server-neo -user=www-data -autostart=true -autorestart=true -startretries=3 -redirect_stderr=true - -; 日志配置 -stdout_logfile=/path/to/your/ball-tracking/server-neo/logs/supervisor.log -stdout_logfile_maxbytes=50MB -stdout_logfile_backups=5 - -; 环境变量 -environment=PATH="/path/to/your/ball-tracking/server-neo/venv/bin",FLASK_ENV="production" - -; 进程管理 -killasgroup=true -stopasgroup=true -stopsignal=TERM - -; 其他配置 -priority=999 -numprocs=1 diff --git a/wsgi.py b/wsgi.py deleted file mode 100644 index 7368d5a..0000000 --- a/wsgi.py +++ /dev/null @@ -1,3 +0,0 @@ -from app import create_app - -app = create_app() \ No newline at end of file