251 lines
3.8 KiB
Markdown
251 lines
3.8 KiB
Markdown
# 装备成本估算系统部署指南
|
|
|
|
## 一、系统打包
|
|
|
|
### 1. 创建部署包结构
|
|
|
|
```bash
|
|
mkdir -p deploy/equipment_cost_system/{backend,frontend,docs}
|
|
```
|
|
|
|
### 2. 准备部署文件
|
|
|
|
#### 2.1 后端文件
|
|
|
|
```bash
|
|
cd deploy/equipment_cost_system/backend
|
|
mkdir -p {src,scripts,config,data,logs,models}
|
|
cp -r ../../../src/* src/
|
|
cp ../../../requirements.txt ./
|
|
cp ../../../.env.example config/.env.template
|
|
```
|
|
|
|
#### 2.2 前端文件
|
|
|
|
```bash
|
|
cd ../frontend
|
|
mkdir -p {src,public,dist}
|
|
cp -r ../../frontend/src/* src/
|
|
cp -r ../../frontend/public/* public/
|
|
cp ../../../frontend/package.json ./
|
|
cp ../../../frontend/vite.config.js ./
|
|
cp ../../../frontend/.env.production ./
|
|
```
|
|
|
|
#### 2.3 复制文档
|
|
|
|
```bash
|
|
cd ../docs
|
|
cp -r ../../docs/deploy/* ./
|
|
```
|
|
|
|
#### 2.4 创建部署脚本
|
|
|
|
```bash
|
|
touch scripts/{install.sh,start.sh,stop.sh}
|
|
```
|
|
|
|
### 3. 部署脚本内容
|
|
|
|
#### 3.1 安装脚本 (install.sh)
|
|
|
|
```bash
|
|
#!/bin/bash
|
|
|
|
# 安装 Python 依赖
|
|
python3 -m venv venv
|
|
source venv/bin/activate
|
|
pip install -r requirements.txt
|
|
|
|
# 创建必要的目录
|
|
mkdir -p logs
|
|
mkdir -p data
|
|
mkdir -p models
|
|
|
|
# 配置文件
|
|
if [ ! -f config/.env ]; then
|
|
cp config/.env.template config/.env
|
|
echo "请修改 config/.env 中的配置"
|
|
fi
|
|
|
|
# 初始化数据库
|
|
read -p "请输入MySQL root密码: " mysqlpass
|
|
mysql -u root -p$mysqlpass < src/schema.sql
|
|
|
|
# 设置权限
|
|
chmod +x scripts/*.sh
|
|
```
|
|
|
|
#### 3.2 启动脚本 (start.sh)
|
|
|
|
```bash
|
|
#!/bin/bash
|
|
|
|
# 激活虚拟环境
|
|
source venv/bin/activate
|
|
|
|
# 检查配置文件
|
|
if [ ! -f config/.env ]; then
|
|
echo "错误: 配置文件不存在"
|
|
exit 1
|
|
fi
|
|
|
|
# 启动服务
|
|
export $(cat config/.env | xargs)
|
|
gunicorn -w 4 -b 0.0.0.0:5001 "src.app:create_app()" --daemon
|
|
|
|
echo "服务已启动,访问 http://localhost:5001"
|
|
```
|
|
|
|
#### 3.3 停止脚本 (stop.sh)
|
|
|
|
```bash
|
|
#!/bin/bash
|
|
|
|
# 查找并停止 gunicorn 进程
|
|
pkill -f gunicorn
|
|
|
|
echo "服务已停止"
|
|
```
|
|
|
|
### 4. 创建部署包
|
|
|
|
```bash
|
|
cd ..
|
|
tar -czf equipment_cost_system.tar.gz equipment_cost_system/
|
|
```
|
|
|
|
## 二、部署步骤
|
|
|
|
### 1. 系统要求
|
|
|
|
- Linux 服务器 (推荐 Ubuntu 20.04+)
|
|
- Python 3.8+
|
|
- MySQL 8.0+
|
|
|
|
### 2. 安装部署
|
|
|
|
```bash
|
|
# 解压部署包
|
|
tar -xzf equipment_cost_system.tar.gz
|
|
cd equipment_cost_system
|
|
|
|
# 运行安装脚本
|
|
bash scripts/install.sh
|
|
|
|
# 修改配置文件
|
|
vim config/.env
|
|
|
|
# 启动服务
|
|
bash scripts/start.sh
|
|
```
|
|
|
|
### 3. 验证部署
|
|
|
|
```bash
|
|
# 检查服务状态
|
|
curl http://localhost:5001/api/
|
|
|
|
# 检查日志
|
|
tail -f logs/api.log
|
|
```
|
|
|
|
## 三、维护说明
|
|
|
|
### 1. 日常维护
|
|
|
|
```bash
|
|
# 查看日志
|
|
tail -f logs/api.log
|
|
|
|
# 备份数据库
|
|
mysqldump -u root -p equipment_cost_db > backup/$(date +%Y%m%d).sql
|
|
|
|
# 清理旧日志
|
|
find logs/ -name "*.log" -mtime +30 -delete
|
|
```
|
|
|
|
### 2. 更新部署
|
|
|
|
```bash
|
|
# 停止服务
|
|
bash scripts/stop.sh
|
|
|
|
# 备份数据
|
|
cp -r data data_backup_$(date +%Y%m%d)
|
|
|
|
# 更新代码
|
|
# ... 更新相关文件 ...
|
|
|
|
# 重启服务
|
|
bash scripts/start.sh
|
|
```
|
|
|
|
### 3. 故障处理
|
|
|
|
```bash
|
|
# 检查服务状态
|
|
ps aux | grep gunicorn
|
|
|
|
# 检查数据库连接
|
|
mysql -u root -p -e "show databases;"
|
|
|
|
# 重启服务
|
|
bash scripts/stop.sh
|
|
bash scripts/start.sh
|
|
```
|
|
|
|
## 四、安全建议
|
|
|
|
1. 文件权限设置
|
|
|
|
```bash
|
|
# 设置适当的文件权限
|
|
chmod 755 scripts/*.sh
|
|
chmod 600 config/.env
|
|
chmod 700 logs models data
|
|
```
|
|
|
|
2. 数据库安全
|
|
|
|
- 使用强密码
|
|
- 限制数据库访问IP
|
|
- 定期备份数据
|
|
|
|
3. 服务器配置
|
|
|
|
- 配置防火墙规则
|
|
- 启用 SSL/TLS
|
|
- 定期更新系统
|
|
|
|
## 五、监控方案
|
|
|
|
### 1. 系统监控
|
|
|
|
```bash
|
|
# 检查CPU和内存使用
|
|
top -b -n 1
|
|
|
|
# 检查磁盘使用
|
|
df -h
|
|
|
|
# 检查网络连接
|
|
netstat -tunlp
|
|
```
|
|
|
|
### 2. 应用监控
|
|
|
|
```bash
|
|
# 检查API响应时间
|
|
curl -w "@curl-format.txt" -o /dev/null -s "http://localhost:5001/api/"
|
|
|
|
# 检查错误日志
|
|
grep "ERROR" logs/api.log
|
|
```
|
|
|
|
### 3. 告警设置
|
|
|
|
- 配置日志告警
|
|
- 设置资源使用阈值告警
|
|
- 配置服务可用性监控
|