宝塔部署python项目文档

发布于:2025-09-03 ⋅ 阅读:(13) ⋅ 点赞:(0)
d# 绩效管理系统部署指南

## 系统信息
- 域名:XXXX
- 服务器IP:XXXXX
- 管理面板:宝塔面板
- 后端:Django + SQLite
- 前端:React

## 部署步骤

### 第一步:准备服务器环境

1. **登录宝塔面板**
   - 访问:http://XXXXX:8888
   - 使用管理员账号登录

2. **安装必要软件**
   - Python 3.8+
   - Node.js 16+
   - Nginx
   - PM2(进程管理器)

   在宝塔面板 → 软件商店 → 搜索并安装:
   - Python项目管理器
   - Node.js版本管理器
   - Nginx
   - PM2管理器

### 第二步:上传项目文件

1. **创建项目目录**
   ```bash
   # 在宝塔面板 → 文件 → 创建目录
   /www/wwwroot/jx.aeauto-cms.com/
   ```

2. **上传项目文件**
   - 将整个项目文件夹压缩为 zip 文件
   - 通过宝塔面板文件管理上传到 `/www/wwwroot/jx.aeauto-cms.com/`
   - 解压文件

### 第三步:配置后端(Django)

1. **安装Python依赖**
   ```bash
   # 在宝塔面板 → 终端 中执行
   cd /www/wwwroot/jx.aeauto-cms.com/
   pip3 install -r requirements.txt
   ```

2. **创建requirements.txt文件**(如果没有)
   ```txt
   Django==5.2.4
   djangorestframework==3.15.2
   django-cors-headers==4.3.1
   djangorestframework-authtoken==1.3.0
   ```

3. **修改Django设置**
   编辑 `performance_system/settings.py`:
   ```python
   # 允许的主机
   ALLOWED_HOSTS = ['jx.aeauto-cms.com', '43.131.14.224', 'localhost']
   
   # 静态文件设置
   STATIC_URL = '/static/'
   STATIC_ROOT = '/www/wwwroot/jx.aeauto-cms.com/staticfiles/'
   
   # 跨域设置
   CORS_ALLOWED_ORIGINS = [
       "https://jx.aeauto-cms.com",
       "http://jx.aeauto-cms.com",
   ]
   ```

4. **数据库迁移**
   ```bash
   cd /www/wwwroot/jx.aeauto-cms.com/
   python3 manage.py migrate
   python3 manage.py collectstatic --noinput
   ```

5. **创建超级用户**
   ```bash
   python3 manage.py createsuperuser
   ```

### 第四步:配置前端(React)

1. **修改API地址**
   编辑前端代码中的API地址,将所有 `http://127.0.0.1:8000` 替换为 `https://jx.aeauto-cms.com`

2. **构建前端**
   ```bash
   cd /www/wwwroot/jx.aeauto-cms.com/frontend/
   npm install
   npm run build
   ```

### 第五步:配置Nginx

1. **在宝塔面板添加站点**
   - 宝塔面板 → 网站 → 添加站点
   - 域名:jx.aeauto-cms.com
   - 根目录:/www/wwwroot/jx.aeauto-cms.com/frontend/build

2. **配置Nginx反向代理**
   点击站点设置 → 配置文件,添加以下配置:
   ```nginx
   server {
       listen 80;
       server_name jx.aeauto-cms.com;
       root /www/wwwroot/jx.aeauto-cms.com/frontend/build;
       index index.html;
   
       # 前端路由
       location / {
           try_files $uri $uri/ /index.html;
       }
   
       # API代理到Django
       location /api/ {
           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;
       }
   
       # Django管理后台
       location /admin/ {
           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;
       }
   
       # 静态文件
       location /static/ {
           alias /www/wwwroot/jx.aeauto-cms.com/staticfiles/;
       }
   }
   ```

### 第六步:启动服务

1. **启动Django服务**
   ```bash
   cd /www/wwwroot/jx.aeauto-cms.com/
   nohup python3 manage.py runserver 127.0.0.1:8000 > django.log 2>&1 &
   ```

2. **或使用PM2管理Django进程**
   创建 `ecosystem.config.js`:
   ```javascript
   module.exports = {
     apps: [{
       name: 'django-perf',
       script: 'manage.py',
       args: 'runserver 127.0.0.1:8000',
       interpreter: 'python3',
       cwd: '/www/wwwroot/jx.aeauto-cms.com/',
       env: {
         DJANGO_SETTINGS_MODULE: 'performance_system.settings'
       }
     }]
   };
   ```
   
   启动:
   ```bash
   pm2 start ecosystem.config.js
   pm2 save
   pm2 startup
   ```

### 第七步:SSL证书配置(可选但推荐)

1. **申请SSL证书**
   - 宝塔面板 → 网站 → 站点设置 → SSL
   - 选择Let's Encrypt免费证书
   - 点击申请

2. **强制HTTPS**
   - 开启"强制HTTPS"

### 第八步:测试部署

1. **访问网站**
   - 前端:https://jx.aeauto-cms.com
   - 后台:https://jx.aeauto-cms.com/admin

2. **测试功能**
   - 用户登录
   - 创建计划
   - 评分功能

## 常见问题解决

### 1. 静态文件404
```bash
python3 manage.py collectstatic --noinput
```

### 2. 数据库权限问题
```bash
chmod 664 db.sqlite3
chown www:www db.sqlite3
```

### 3. 跨域问题
确保 `settings.py` 中的 `CORS_ALLOWED_ORIGINS` 包含正确的域名

### 4. 进程管理
```bash
# 查看Django进程
ps aux | grep python

# 重启Django
pm2 restart django-perf

# 查看日志
pm2 logs django-perf
```

## 维护命令

```bash
# 更新代码后
cd /www/wwwroot/jx.aeauto-cms.com/
git pull  # 如果使用git
python3 manage.py migrate
python3 manage.py collectstatic --noinput
pm2 restart django-perf

# 前端更新
cd frontend/
npm run build
```

## 备份建议

1. **数据库备份**
   ```bash
   cp db.sqlite3 db_backup_$(date +%Y%m%d).sqlite3
   ```

2. **定期备份**
   - 宝塔面板 → 计划任务 → 备份网站
   - 设置每日自动备份

## 监控建议

1. **服务器监控**
   - 宝塔面板自带监控
   - 关注CPU、内存、磁盘使用率

2. **应用监控**
   - 使用PM2监控Django进程
   - 定期检查日志文件

---

**部署完成后,您就可以通过 XXXX 访问管理系统了!**

网站公告

今日签到

点亮在社区的每一天
去签到