feat: 添加腾讯云部署脚本和文档

This commit is contained in:
Sens 2026-02-20 22:53:50 +08:00
parent 689cf94d4b
commit 656cefa668
3 changed files with 688 additions and 0 deletions

337
DEPLOY_TENCENT_CLOUD.md Normal file
View File

@ -0,0 +1,337 @@
# Neural Sync 腾讯云部署指南
## 服务器要求
- **系统**: Ubuntu 20.04+ / CentOS 8+
- **配置**: 2核4GB RAM 以上
- **需要**: Nginx, Node.js, Python 3.9+, PostgreSQL (可选), Ollama (可选)
---
## 一、环境准备
### 1.1 创建腾讯云轻量应用服务器
1. 登录 [腾讯云控制台](https://console.cloud.tencent.com/)
2. 购买轻量应用服务器 (Lighthouse)
3. 选择 Ubuntu 20.04 系统镜像
4. 配置防火墙规则开放端口: 80, 443, 8000
### 1.2 SSH 连接服务器
```bash
ssh root@你的服务器IP
```
---
## 二、一键部署脚本
### 2.1 方式一:从本地直接上传(推荐开发阶段)
```bash
# 在本地项目目录执行
scp -r /path/to/neural-sync root@你的服务器IP:/opt/
```
### 2.2 方式二:从 GitHub 拉取代码
```bash
# SSH 登录服务器后执行
cd /opt
git clone https://github.com/你的用户名/neural-sync.git
cd neural-sync
```
### 2.3 方式三:从 Gitee (码云) 拉取代码
```bash
# 如果 GitHub 访问较慢,可以使用 Gitee
cd /opt
git clone https://gitee.com/你的用户名/neural-sync.git
cd neural-sync
```
### 2.4 运行部署脚本
#### 2.2 运行部署脚本
```bash
chmod +x deploy.sh
# 方式1: 交互式部署(会提示选择代码来源)
./deploy.sh
# 方式2: 指定参数直接部署
# 参数1: 数据库密码 参数2: Git仓库地址
./deploy.sh "你的数据库密码" "https://github.com/xxx/neural-sync.git"
```
脚本会自动安装所有依赖并启动服务。
---
## 三、部署脚本详解
### 手动部署步骤 (如果自动脚本失败)
#### 1. 安装系统依赖
```bash
# 更新系统
apt update && apt upgrade -y
# 安装基础工具
apt install -y curl wget git nginx postgresql postgresql-contrib
# 安装 Python
apt install -y python3.9 python3-pip python3-venv
# 安装 Node.js
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
apt install -y nodejs
```
#### 2. 配置 PostgreSQL 数据库
```bash
# 启动 PostgreSQL
systemctl start postgresql
systemctl enable postgresql
# 创建数据库和用户
sudo -u postgres psql
CREATE DATABASE neural_sync;
CREATE USER neuraluser WITH PASSWORD 'your_secure_password';
GRANT ALL PRIVILEGES ON DATABASE neural_sync TO neuraluser;
\q
```
#### 3. 安装 Python 依赖
```bash
cd /opt/neural-sync/backend
# 创建虚拟环境
python3 -m venv venv
source venv/bin/activate
# 安装依赖
pip install -r requirements.txt
# 配置环境变量
cp .env.example .env
nano .env
```
编辑 `.env` 文件:
```env
# 数据库 - PostgreSQL
DATABASE_URL=postgresql+asyncpg://neuraluser:your_secure_password@localhost:5432/neural_sync
# Ollama (如需本地AI功能)
OLLAMA_HOST=http://127.0.0.1:11434
# 业务上下文 (根据你的业务修改)
BUSINESS_CONTEXT=你的业务描述
```
#### 4. 运行数据库迁移
```bash
# 如果使用 PostgreSQL,需要先安装 pgvector
sudo -u postgres psql -d neural_sync -c "CREATE EXTENSION IF NOT EXISTS vector;"
# 运行迁移
alembic upgrade head
```
#### 5. 构建前端
```bash
cd /opt/neural-sync/frontend
# 安装依赖
npm install
# 构建生产版本
npm run build
```
#### 6. 配置 Nginx
创建 Nginx 配置文件:
```bash
nano /etc/nginx/sites-available/neural-sync
```
```nginx
server {
listen 80;
server_name 你的域名或IP;
# 前端静态文件
location / {
root /opt/neural-sync/frontend/dist;
index index.html;
try_files $uri $uri/ /index.html;
}
# 后端 API 代理
location /api {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade;
}
# WebSocket 支持 (如需要)
location /ws {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
```
启用配置:
```bash
ln -s /etc/nginx/sites-available/neural-sync /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
```
#### 7. 配置 Systemd 服务
创建后端服务文件:
```bash
nano /etc/systemd/system/neural-sync.service
```
```ini
[Unit]
Description=Neural Sync Backend
After=network.target postgresql.service
[Service]
Type=simple
User=root
WorkingDirectory=/opt/neural-sync/backend
Environment="PATH=/opt/neural-sync/backend/venv/bin"
EnvironmentFile=/opt/neural-sync/backend/.env
ExecStart=/opt/neural-sync/backend/venv/bin/uvicorn app.main:app --host 127.0.0.1 --port 8000
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
```
启动服务:
```bash
systemctl daemon-reload
systemctl enable neural-sync
systemctl start neural-sync
systemctl status neural-sync
```
---
## 四、验证部署
```bash
# 检查服务状态
systemctl status neural-sync
nginx -t
# 测试 API
curl http://127.0.0.1:8000/docs
# 访问前端
curl http://你的服务器IP
```
---
## 五、常用命令
```bash
# 重启后端
systemctl restart neural-sync
# 查看后端日志
journalctl -u neural-sync -f
# 重载 Nginx
nginx -s reload
# 停止所有服务
systemctl stop neural-sync
nginx stop
```
---
## 六、可选: 安装 Ollama (本地 AI)
```bash
# 安装 Ollama
curl -fsSL https://ollama.com/install.sh | sh
# 启动服务
ollama serve &
# 下载模型
ollama pull llama3
ollama pull nomic-embed-text
```
---
## 七、安全建议
1. 使用 HTTPS (配置 SSL 证书)
2. 修改默认数据库密码
3. 配置防火墙只开放 80/443 端口
4. 定期更新系统和依赖
---
## 八、更新部署
代码更新后,重新部署:
```bash
cd /opt/neural-sync
# 拉取最新代码
git pull
# 重新构建前端
cd frontend
npm install
npm run build
cd ..
# 重启后端服务
systemctl restart neural-sync
# 重载 Nginx
nginx -s reload
```
或使用一键更新脚本:
```bash
./update.sh
```

296
deploy.sh Normal file
View File

@ -0,0 +1,296 @@
#!/bin/bash
# Neural Sync 腾讯云一键部署脚本
set -e
echo "========================================"
echo " Neural Sync 腾讯云部署脚本"
echo "========================================"
# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
# 配置变量
PROJECT_DIR="/opt/neural-sync"
DB_PASSWORD=${1:-"neural_sync_2024"}
GIT_REPO=${2:-""}
# 检查是否为 root 用户
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}❌ 请使用 root 用户运行此脚本${NC}"
exit 1
fi
echo -e "${GREEN}✅ 开始部署...${NC}"
# ========== 1. 安装系统依赖 ==========
echo -e "${YELLOW}[1/8] 安装系统依赖...${NC}"
apt update
apt upgrade -y
apt install -y curl wget git nginx software-properties-common
apt install -y python3.9 python3-pip python3-venv python3-dev libpq-dev
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
apt install -y nodejs
python3 --version
node --version
npm --version
echo -e "${GREEN}✅ 系统依赖安装完成${NC}"
# ========== 2. 配置 PostgreSQL ==========
echo -e "${YELLOW}[2/8] 配置 PostgreSQL 数据库...${NC}"
apt install -y postgresql postgresql-contrib
systemctl start postgresql
systemctl enable postgresql
su - postgres -c "psql -c \"CREATE DATABASE neural_sync;\"" 2>/dev/null || true
su - postgres -c "psql -c \"CREATE USER neuraluser WITH PASSWORD '$DB_PASSWORD';\"" 2>/dev/null || true
su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE neural_sync TO neuraluser;\"" 2>/dev/null || true
su - postgres -c "psql -d neural_sync -c \"CREATE EXTENSION IF NOT EXISTS vector;\"" 2>/dev/null || true
echo -e "${GREEN}✅ PostgreSQL 配置完成${NC}"
# ========== 3. 拉取项目代码 ==========
echo -e "${YELLOW}[3/8] 拉取项目代码...${NC}"
cd /opt
if [ -d "$PROJECT_DIR" ]; then
mv "$PROJECT_DIR" "${PROJECT_DIR}_backup_$(date +%Y%m%d_%H%M%S)"
fi
if [ -n "$GIT_REPO" ]; then
echo "从 Git 仓库拉取: $GIT_REPO"
git clone "$GIT_REPO" neural-sync
else
echo -e "${YELLOW}请选择代码获取方式:${NC}"
echo " 1) 从 GitHub 拉取"
echo " 2) 从 Gitee (码云) 拉取"
echo " 3) 使用默认示例仓库"
echo ""
read -p "请输入选项 [1-3]: " choice
case $choice in
1)
echo -e "${YELLOW}请输入 GitHub 仓库地址:${NC}"
echo " 格式: https://github.com/用户名/仓库名.git"
read -p "仓库地址: " GIT_REPO
if [ -n "$GIT_REPO" ]; then
git clone "$GIT_REPO" neural-sync
else
echo -e "${RED}❌ 未输入仓库地址${NC}"
exit 1
fi
;;
2)
echo -e "${YELLOW}请输入 Gitee 仓库地址:${NC}"
echo " 格式: https://gitee.com/用户名/仓库名.git"
read -p "仓库地址: " GIT_REPO
if [ -n "$GIT_REPO" ]; then
git clone "$GIT_REPO" neural-sync
else
echo -e "${RED}❌ 未输入仓库地址${NC}"
exit 1
fi
;;
3)
echo -e "${YELLOW}克隆官方示例仓库...${NC}"
git clone https://github.com/anomalyco/neural-sync.git neural-sync 2>/dev/null || \
git clone https://gitee.com/ai-tools/neural-sync.git neural-sync 2>/dev/null || {
echo -e "${RED}❌ 无法拉取示例仓库,请手动配置仓库地址${NC}"
exit 1
}
;;
*)
echo -e "${RED}❌ 无效选项${NC}"
exit 1
;;
esac
fi
if [ ! -d "$PROJECT_DIR" ]; then
echo -e "${RED}❌ 项目代码拉取失败${NC}"
exit 1
fi
echo -e "${GREEN}✅ 项目代码拉取完成${NC}"
# ========== 4. 配置后端 ==========
echo -e "${YELLOW}[4/8] 配置后端服务...${NC}"
cd "$PROJECT_DIR/backend"
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
cp .env.example .env 2>/dev/null || true
if [ -f .env ]; then
cat > .env << EOF
DATABASE_URL=postgresql+asyncpg://neuraluser:${DB_PASSWORD}@localhost:5432/neural_sync
OLLAMA_HOST=http://127.0.0.1:11434
BUSINESS_CONTEXT=Pixelle-Video: An AI-powered automated short video generation platform.
EOF
fi
alembic upgrade head 2>/dev/null || echo "迁移完成或跳过"
deactivate
echo -e "${GREEN}✅ 后端配置完成${NC}"
# ========== 5. 构建前端 ==========
echo -e "${YELLOW}[5/8] 构建前端...${NC}"
cd "$PROJECT_DIR/frontend"
npm install
npm run build
if [ ! -d "dist" ]; then
echo -e "${RED}❌ 前端构建失败${NC}"
exit 1
fi
echo -e "${GREEN}✅ 前端构建完成${NC}"
# ========== 6. 配置 Nginx ==========
echo -e "${YELLOW}[6/8] 配置 Nginx...${NC}"
cat > /etc/nginx/sites-available/neural-sync << 'EOF'
server {
listen 80;
server_name _;
location / {
root /opt/neural-sync/frontend/dist;
index index.html;
try_files $uri $uri/ /index.html;
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
location /api {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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;
proxy_cache_bypass $http_upgrade;
}
location /ws {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
EOF
ln -sf /etc/nginx/sites-available/neural-sync /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
nginx -t
systemctl reload nginx
echo -e "${GREEN}✅ Nginx 配置完成${NC}"
# ========== 7. 配置 Systemd 服务 ==========
echo -e "${YELLOW}[7/8] 配置 Systemd 服务...${NC}"
cat > /etc/systemd/system/neural-sync.service << EOF
[Unit]
Description=Neural Sync Backend API
After=network.target postgresql.service
[Service]
Type=simple
User=root
WorkingDirectory=$PROJECT_DIR/backend
Environment="PATH=$PROJECT_DIR/backend/venv/bin"
Environment="PYTHONPATH=$PROJECT_DIR/backend"
EnvironmentFile=$PROJECT_DIR/backend/.env
ExecStart=$PROJECT_DIR/backend/venv/bin/uvicorn app.main:app --host 127.0.0.1 --port 8000
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable neural-sync
systemctl restart neural-sync
sleep 3
if systemctl is-active --quiet neural-sync; then
echo -e "${GREEN}✅ 后端服务启动成功${NC}"
else
echo -e "${RED}❌ 后端服务启动失败,查看日志:${NC}"
journalctl -u neural-sync -n 20
fi
# ========== 8. 开放防火墙端口 ==========
echo -e "${YELLOW}[8/8] 开放防火墙端口...${NC}"
# Ubuntu ufw
if command -v ufw &> /dev/null; then
ufw allow 80/tcp 2>/dev/null || true
ufw allow 443/tcp 2>/dev/null || true
ufw allow 8000/tcp 2>/dev/null || true
fi
echo -e "${GREEN}✅ 防火墙配置完成${NC}"
# 获取服务器 IP
SERVER_IP=$(hostname -I | awk '{print $1}')
echo ""
echo "========================================"
echo " 🎉 部署完成!"
echo "========================================"
echo ""
echo -e "${GREEN}访问地址:${NC}"
echo " - 前端: http://$SERVER_IP"
echo " - API: http://$SERVER_IP/api"
echo " - API文档: http://$SERVER_IP/api/docs"
echo ""
echo -e "${YELLOW}常用命令:${NC}"
echo " - 查看后端日志: journalctl -u neural-sync -f"
echo " - 重启后端: systemctl restart neural-sync"
echo " - 重载 Nginx: nginx -s reload"
echo ""
echo -e "${YELLOW}配置信息:${NC}"
echo " - 数据库: neural_sync"
echo " - 数据库用户: neuraluser"
echo " - 数据库密码: $DB_PASSWORD"
echo ""
echo -e "${YELLOW}后续步骤:${NC}"
echo " 1. 如需 HTTPS,请配置 SSL 证书 (使用 Let's Encrypt)"
echo " 2. 如需 AI 功能,请安装 Ollama"
echo " 3. 修改 .env 文件中的 BUSINESS_CONTEXT 为你的业务描述"
echo ""

55
update.sh Normal file
View File

@ -0,0 +1,55 @@
#!/bin/bash
# Neural Sync 更新脚本 - 用于代码更新后重新部署
set -e
PROJECT_DIR="/opt/neural-sync"
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
echo "========================================"
echo " Neural Sync 更新脚本"
echo "========================================"
if [ ! -d "$PROJECT_DIR" ]; then
echo -e "${RED}❌ 项目目录不存在${NC}"
exit 1
fi
cd "$PROJECT_DIR"
echo -e "${YELLOW}[1/4] 拉取最新代码...${NC}"
git pull
echo -e "${YELLOW}[2/4] 重新构建前端...${NC}"
cd frontend
npm install
npm run build
cd ..
echo -e "${YELLOW}[3/4] 更新 Python 依赖 (如有变更)...${NC}"
cd backend
source venv/bin/activate
pip install -r requirements.txt --upgrade
deactivate
cd ..
echo -e "${YELLOW}[4/4] 重启服务...${NC}"
systemctl restart neural-sync
nginx -s reload
sleep 2
if systemctl is-active --quiet neural-sync; then
echo -e "${GREEN}✅ 更新完成!${NC}"
else
echo -e "${RED}❌ 服务启动失败,查看日志:${NC}"
journalctl -u neural-sync -n 20
fi
SERVER_IP=$(hostname -I | awk '{print $1}')
echo ""
echo "访问地址: http://$SERVER_IP"