neural-sync/DEPLOY_TENCENT_CLOUD.md

338 lines
5.9 KiB
Markdown
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.

# 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
```