Files
fatemaster/docs/DEPLOYMENT.md
jiangdong d81cf186b0 init
2025-10-03 11:24:11 +08:00

277 lines
5.4 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.
# 部署脚本示例
## Linux服务器部署步骤
### 1. 安装必要软件
```bash
# 更新系统
sudo apt update && sudo apt upgrade -y
# 安装Nginx
sudo apt install nginx -y
# 安装.NET 8 Runtime
wget https://dot.net/v1/dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --channel 8.0 --runtime aspnetcore
# 安装Node.js
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install nodejs -y
# 安装MySQL
sudo apt install mysql-server -y
```
### 2. 创建数据库
```bash
sudo mysql -u root -p
CREATE DATABASE fatemaster CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'fatemaster'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON fatemaster.* TO 'fatemaster'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```
### 3. 部署后端
```bash
# 构建项目
cd backend/FateMaster.API
dotnet publish -c Release -o /var/www/fatemaster-api
# 创建systemd服务
sudo nano /etc/systemd/system/fatemaster-api.service
```
服务配置内容:
```ini
[Unit]
Description=FateMaster API
After=network.target
[Service]
WorkingDirectory=/var/www/fatemaster-api
ExecStart=/root/.dotnet/dotnet /var/www/fatemaster-api/FateMaster.API.dll
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=fatemaster-api
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
```
启动服务:
```bash
sudo systemctl enable fatemaster-api
sudo systemctl start fatemaster-api
sudo systemctl status fatemaster-api
```
### 4. 部署前端
```bash
# 构建用户端
cd frontend/fatemaster-web
npm install
npm run build
# 部署到Nginx
sudo cp -r dist/* /var/www/fatemaster-web/
# 构建管理后台
cd ../fatemaster-admin
npm install
npm run build
# 部署到Nginx
sudo cp -r dist/* /var/www/fatemaster-admin/
```
### 5. 配置Nginx
```bash
sudo nano /etc/nginx/sites-available/fatemaster
```
配置内容:
```nginx
# 用户端
server {
listen 80;
server_name yourdomain.com;
root /var/www/fatemaster-web;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# 管理后台
server {
listen 80;
server_name admin.yourdomain.com;
root /var/www/fatemaster-admin;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
```
启用配置:
```bash
sudo ln -s /etc/nginx/sites-available/fatemaster /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
```
### 6. 配置SSL证书使用Let's Encrypt
```bash
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d admin.yourdomain.com
```
### 7. 配置防火墙
```bash
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 22/tcp
sudo ufw enable
```
### 8. 设置定时备份
```bash
sudo nano /usr/local/bin/backup-fatemaster.sh
```
备份脚本:
```bash
#!/bin/bash
BACKUP_DIR="/backups/fatemaster"
DATE=$(date +%Y%m%d_%H%M%S)
# 创建备份目录
mkdir -p $BACKUP_DIR
# 备份数据库
mysqldump -u fatemaster -p'your_password' fatemaster > $BACKUP_DIR/db_$DATE.sql
# 备份上传的文件(如果有)
# tar -czf $BACKUP_DIR/files_$DATE.tar.gz /var/www/uploads
# 删除7天前的备份
find $BACKUP_DIR -name "*.sql" -mtime +7 -delete
echo "Backup completed: $DATE"
```
设置定时任务:
```bash
sudo chmod +x /usr/local/bin/backup-fatemaster.sh
sudo crontab -e
# 添加每天凌晨2点执行备份
0 2 * * * /usr/local/bin/backup-fatemaster.sh >> /var/log/fatemaster-backup.log 2>&1
```
## 更新部署
### 更新后端
```bash
cd /path/to/source/backend/FateMaster.API
git pull
dotnet publish -c Release -o /var/www/fatemaster-api
sudo systemctl restart fatemaster-api
```
### 更新前端
```bash
# 用户端
cd /path/to/source/frontend/fatemaster-web
git pull
npm install
npm run build
sudo cp -r dist/* /var/www/fatemaster-web/
# 管理后台
cd ../fatemaster-admin
git pull
npm install
npm run build
sudo cp -r dist/* /var/www/fatemaster-admin/
```
## 监控和维护
### 查看服务日志
```bash
# 查看API日志
sudo journalctl -u fatemaster-api -f
# 查看Nginx日志
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
```
### 性能监控
```bash
# 查看系统资源
htop
# 查看数据库性能
sudo mysqladmin -u root -p processlist
sudo mysqladmin -u root -p status
```
## 故障排查
### API无法启动
```bash
# 检查服务状态
sudo systemctl status fatemaster-api
# 检查端口占用
sudo netstat -tlnp | grep 5000
# 检查数据库连接
mysql -u fatemaster -p -h localhost fatemaster
```
### 前端访问404
```bash
# 检查Nginx配置
sudo nginx -t
# 检查文件权限
ls -la /var/www/fatemaster-web/
```