Ai_GirlFriend/部署指南.md

691 lines
14 KiB
Markdown
Raw Normal View History

2026-02-03 18:00:47 +08:00
# 🚀 AI 女友项目部署指南
## 📋 项目架构
- **PHP 后端**FastAdmin + ThinkPHP用户管理、基础功能
- **Python 后端**FastAPIAI 功能、唱歌、跳舞等)
- **前端**uni-appH5/小程序/APP
- **数据库**MySQL
- **文件存储**:阿里云 OSS
## 🖥️ 服务器要求
### 最低配置
- CPU: 2核
- 内存: 4GB
- 硬盘: 40GB
- 带宽: 5Mbps
### 推荐配置
- CPU: 4核
- 内存: 8GB
- 硬盘: 100GB
- 带宽: 10Mbps
### 操作系统
- Ubuntu 20.04/22.04 LTS推荐
- CentOS 7/8
- Debian 10/11
---
## 📦 第一步:准备服务器环境
### 1.1 更新系统
```bash
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y
# CentOS
sudo yum update -y
```
### 1.2 安装必要软件
```bash
# Ubuntu/Debian
sudo apt install -y git curl wget vim unzip
# CentOS
sudo yum install -y git curl wget vim unzip
```
---
## 🐘 第二步:安装 PHP 环境
### 2.1 安装 PHP 8.0
```bash
# Ubuntu/Debian
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.0 php8.0-fpm php8.0-mysql php8.0-xml php8.0-mbstring \
php8.0-curl php8.0-zip php8.0-gd php8.0-bcmath php8.0-json
# CentOS
sudo yum install -y epel-release
sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm
sudo yum module reset php -y
sudo yum module enable php:remi-8.0 -y
sudo yum install -y php php-fpm php-mysql php-xml php-mbstring \
php-curl php-zip php-gd php-bcmath php-json
```
### 2.2 配置 PHP
```bash
# 编辑 php.ini
sudo vim /etc/php/8.0/fpm/php.ini
# 修改以下配置
upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 300
memory_limit = 256M
```
### 2.3 启动 PHP-FPM
```bash
# Ubuntu/Debian
sudo systemctl start php8.0-fpm
sudo systemctl enable php8.0-fpm
# CentOS
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
```
---
## 🐍 第三步:安装 Python 环境
### 3.1 安装 Python 3.10+
```bash
# Ubuntu/Debian
sudo apt install -y python3.10 python3.10-venv python3-pip
# CentOS
sudo yum install -y python3 python3-pip python3-devel
```
### 3.2 创建虚拟环境
```bash
cd /var/www/AI_GirlFriend
python3 -m venv venv
source venv/bin/activate
```
### 3.3 安装 Python 依赖
```bash
cd lover
pip install -r requirements.txt
```
---
## 🗄️ 第四步:安装和配置 MySQL
### 4.1 安装 MySQL 8.0
```bash
# Ubuntu/Debian
sudo apt install -y mysql-server
# CentOS
sudo yum install -y mysql-server
```
### 4.2 启动 MySQL
```bash
sudo systemctl start mysql
sudo systemctl enable mysql
```
### 4.3 安全配置
```bash
sudo mysql_secure_installation
```
### 4.4 创建数据库和用户
```bash
sudo mysql -u root -p
# 在 MySQL 中执行
CREATE DATABASE fastadmin CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'aiuser'@'localhost' IDENTIFIED BY '你的强密码';
GRANT ALL PRIVILEGES ON fastadmin.* TO 'aiuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```
### 4.5 导入数据库
```bash
# 导入 PHP 后端数据库
mysql -u aiuser -p fastadmin < 归档/xunifriend.sql
# 执行音乐库迁移
mysql -u aiuser -p fastadmin < lover/migrations/add_music_library.sql
# 执行邀请码功能迁移
mysql -u aiuser -p fastadmin < lover/migrations/add_invite_fields.sql
```
---
## 🌐 第五步:安装和配置 Nginx
### 5.1 安装 Nginx
```bash
# Ubuntu/Debian
sudo apt install -y nginx
# CentOS
sudo yum install -y nginx
```
### 5.2 创建 Nginx 配置
```bash
sudo vim /etc/nginx/sites-available/aigirlfriend
```
### 5.3 Nginx 配置内容
```nginx
# PHP 后端配置
server {
listen 30100;
server_name your-domain.com; # 改为你的域名或 IP
root /var/www/AI_GirlFriend/xunifriend_RaeeC/public;
index index.php index.html;
# 日志
access_log /var/log/nginx/php_access.log;
error_log /var/log/nginx/php_error.log;
# PHP 处理
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 静态文件
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# 禁止访问隐藏文件
location ~ /\. {
deny all;
}
}
# Python 后端配置
server {
listen 30101;
server_name your-domain.com; # 改为你的域名或 IP
# 日志
access_log /var/log/nginx/python_access.log;
error_log /var/log/nginx/python_error.log;
# 反向代理到 Python 应用
location / {
proxy_pass http://127.0.0.1:8000;
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;
# WebSocket 支持
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 超时设置
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
# 静态文件(如果有)
location /static/ {
alias /var/www/AI_GirlFriend/lover/static/;
}
}
```
### 5.4 启用配置
```bash
# Ubuntu/Debian
sudo ln -s /etc/nginx/sites-available/aigirlfriend /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
# CentOS
sudo cp /etc/nginx/sites-available/aigirlfriend /etc/nginx/conf.d/aigirlfriend.conf
sudo nginx -t
sudo systemctl restart nginx
```
---
## 📁 第六步:上传项目文件
### 6.1 创建项目目录
```bash
sudo mkdir -p /var/www/AI_GirlFriend
sudo chown -R $USER:$USER /var/www/AI_GirlFriend
```
### 6.2 上传文件(使用 Git 或 FTP
**方法 1使用 Git**
```bash
cd /var/www
git clone https://your-repo-url.git AI_GirlFriend
```
**方法 2使用 SCP**
```bash
# 在本地电脑执行
scp -r C:\Users\Administrator\Desktop\Project\AI_GirlFriend user@server-ip:/var/www/
```
**方法 3使用 FTP 工具**
- 使用 FileZilla 或 WinSCP
- 上传整个项目文件夹
### 6.3 设置权限
```bash
cd /var/www/AI_GirlFriend
# PHP 项目权限
sudo chown -R www-data:www-data xunifriend_RaeeC/
sudo chmod -R 755 xunifriend_RaeeC/
sudo chmod -R 777 xunifriend_RaeeC/runtime/
sudo chmod -R 777 xunifriend_RaeeC/public/uploads/
# Python 项目权限
sudo chown -R $USER:$USER lover/
sudo chmod -R 755 lover/
sudo mkdir -p public/tts public/music
sudo chmod -R 777 public/
```
---
## ⚙️ 第七步:配置环境变量
### 7.1 配置 PHP 后端
```bash
cd /var/www/AI_GirlFriend/xunifriend_RaeeC
# 编辑数据库配置
vim application/database.php
```
修改数据库配置:
```php
return [
'type' => 'mysql',
'hostname' => '127.0.0.1',
'database' => 'fastadmin',
'username' => 'aiuser',
'password' => '你的数据库密码',
'hostport' => '3306',
'charset' => 'utf8mb4',
];
```
### 7.2 配置 Python 后端
```bash
cd /var/www/AI_GirlFriend/lover
# 创建 .env 文件
vim .env
```
.env 文件内容:
```env
# 应用配置
APP_ENV=production
DEBUG=False
# 数据库配置
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=aiuser
DB_PASSWORD=你的数据库密码
DB_NAME=fastadmin
# JWT 配置
JWT_SECRET_KEY=你的随机密钥至少32位
JWT_ALGORITHM=HS256
JWT_EXPIRE_MINUTES=43200
# DashScope API阿里云通义千问
DASHSCOPE_API_KEY=sk-2473385fd6d54a58a703ce6b92a62074
# 阿里云 OSS 配置
OSS_ACCESS_KEY_ID=LTAI5tBzjogJDx4JzRYoDyEM
OSS_ACCESS_KEY_SECRET=43euicRkkzlLjGTYzFYkTupcW7N5w3
OSS_BUCKET=hello12312312
OSS_ENDPOINT=https://oss-cn-hangzhou.aliyuncs.com
CDN_DOMAIN=https://hello12312312.oss-cn-hangzhou.aliyuncs.com
```
---
## 🔄 第八步:配置 Systemd 服务Python 后端)
### 8.1 创建 Systemd 服务文件
```bash
sudo vim /etc/systemd/system/aigirlfriend-python.service
```
### 8.2 服务文件内容
```ini
[Unit]
Description=AI GirlFriend Python Backend
After=network.target mysql.service
[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/var/www/AI_GirlFriend
Environment="PATH=/var/www/AI_GirlFriend/venv/bin"
ExecStart=/var/www/AI_GirlFriend/venv/bin/uvicorn lover.main:app --host 0.0.0.0 --port 8000 --workers 4
Restart=always
RestartSec=10
# 日志
StandardOutput=append:/var/log/aigirlfriend-python.log
StandardError=append:/var/log/aigirlfriend-python-error.log
[Install]
WantedBy=multi-user.target
```
### 8.3 启动服务
```bash
sudo systemctl daemon-reload
sudo systemctl start aigirlfriend-python
sudo systemctl enable aigirlfriend-python
sudo systemctl status aigirlfriend-python
```
---
## 🔥 第九步:配置防火墙
### 9.1 开放端口
```bash
# Ubuntu/Debian (UFW)
sudo ufw allow 30100/tcp
sudo ufw allow 30101/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
# CentOS (Firewalld)
sudo firewall-cmd --permanent --add-port=30100/tcp
sudo firewall-cmd --permanent --add-port=30101/tcp
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --reload
```
---
## 📱 第十步:部署前端
### 10.1 修改 API 地址
```bash
cd /var/www/AI_GirlFriend/xuniYou
vim utils/request.js
```
修改为生产环境地址:
```javascript
// 生产环境
export const baseURL = 'http://your-domain.com:30100' // 或使用域名
export const baseURLPy = 'http://your-domain.com:30101'
```
### 10.2 编译 H5 版本
```bash
# 在本地开发机器上
cd xuniYou
npm install
npm run build:h5
```
### 10.3 上传 H5 文件
```bash
# 将 unpackage/dist/build/h5 目录上传到服务器
scp -r unpackage/dist/build/h5/* user@server-ip:/var/www/AI_GirlFriend/h5/
```
### 10.4 配置 Nginx 服务 H5
```bash
sudo vim /etc/nginx/sites-available/aigirlfriend-h5
```
添加配置:
```nginx
server {
listen 80;
server_name your-domain.com;
root /var/www/AI_GirlFriend/h5;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
# 静态资源缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}
```
启用配置:
```bash
sudo ln -s /etc/nginx/sites-available/aigirlfriend-h5 /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
```
---
## 🔒 第十一步:配置 HTTPS可选但推荐
### 11.1 安装 Certbot
```bash
# Ubuntu/Debian
sudo apt install -y certbot python3-certbot-nginx
# CentOS
sudo yum install -y certbot python3-certbot-nginx
```
### 11.2 获取 SSL 证书
```bash
sudo certbot --nginx -d your-domain.com
```
### 11.3 自动续期
```bash
sudo certbot renew --dry-run
```
---
## 📊 第十二步:监控和日志
### 12.1 查看日志
```bash
# Nginx 日志
sudo tail -f /var/log/nginx/php_access.log
sudo tail -f /var/log/nginx/python_access.log
# Python 应用日志
sudo tail -f /var/log/aigirlfriend-python.log
# PHP 日志
sudo tail -f /var/log/php8.0-fpm.log
# 系统日志
sudo journalctl -u aigirlfriend-python -f
```
### 12.2 性能监控
```bash
# 安装监控工具
sudo apt install -y htop iotop nethogs
# 查看资源使用
htop
```
---
## 🧪 第十三步:测试部署
### 13.1 测试 PHP 后端
```bash
curl http://your-domain.com:30100
```
### 13.2 测试 Python 后端
```bash
curl http://your-domain.com:30101/health
curl http://your-domain.com:30101/docs # API 文档
```
### 13.3 测试前端
```bash
# 在浏览器中访问
http://your-domain.com
```
---
## 🔧 常见问题排查
### 问题 1502 Bad Gateway
```bash
# 检查 PHP-FPM 状态
sudo systemctl status php8.0-fpm
# 检查 Python 服务状态
sudo systemctl status aigirlfriend-python
# 查看错误日志
sudo tail -f /var/log/nginx/error.log
```
### 问题 2数据库连接失败
```bash
# 检查 MySQL 状态
sudo systemctl status mysql
# 测试数据库连接
mysql -u aiuser -p fastadmin
```
### 问题 3权限问题
```bash
# 重新设置权限
sudo chown -R www-data:www-data /var/www/AI_GirlFriend/xunifriend_RaeeC/
sudo chmod -R 777 /var/www/AI_GirlFriend/xunifriend_RaeeC/runtime/
sudo chmod -R 777 /var/www/AI_GirlFriend/public/
```
### 问题 4Python 依赖问题
```bash
# 重新安装依赖
cd /var/www/AI_GirlFriend
source venv/bin/activate
pip install -r lover/requirements.txt --upgrade
```
---
## 🚀 性能优化建议
### 1. 启用 Gzip 压缩
在 Nginx 配置中添加:
```nginx
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml+rss application/json;
```
### 2. 配置 Redis 缓存
```bash
# 安装 Redis
sudo apt install -y redis-server
sudo systemctl start redis
sudo systemctl enable redis
```
### 3. 使用 CDN
- 将静态资源上传到阿里云 OSS
- 配置 CDN 加速
### 4. 数据库优化
```sql
-- 添加索引
ALTER TABLE nf_user ADD INDEX idx_token (token);
ALTER TABLE nf_chat_message ADD INDEX idx_user_session (user_id, session_id);
```
---
## 📋 部署检查清单
- [ ] 服务器环境准备完成
- [ ] PHP 8.0 安装并配置
- [ ] Python 3.10+ 安装并配置
- [ ] MySQL 8.0 安装并配置
- [ ] Nginx 安装并配置
- [ ] 项目文件上传完成
- [ ] 数据库导入完成
- [ ] 环境变量配置完成
- [ ] Systemd 服务配置完成
- [ ] 防火墙端口开放
- [ ] 前端编译并部署
- [ ] HTTPS 证书配置(可选)
- [ ] 所有服务测试通过
- [ ] 日志监控配置完成
---
## 📞 技术支持
如遇到问题,请检查:
1. 服务器日志:`/var/log/nginx/`, `/var/log/aigirlfriend-python.log`
2. 系统日志:`sudo journalctl -xe`
3. 服务状态:`sudo systemctl status [service-name]`
---
**部署完成!** 🎉
访问地址:
- 前端http://your-domain.com
- PHP 后端http://your-domain.com:30100
- Python 后端http://your-domain.com:30101
- API 文档http://your-domain.com:30101/docs