Python招聘数据分析平台 - Linux服务器部署完整指南

项目概述

本文档记录了将 Django 5.1.4 + MySQL + 机器学习模型的 Web 应用部署到 Ubuntu 22.04 Linux 服务器的完整过程。

一、服务器环境配置

1.1 系统信息

  • 操作系统:Ubuntu 22.04 LTS
  • CPU:4核
  • 内存:20GB
  • Python版本:3.10
  • 数据库:MySQL 8.0

1.2 安装基础软件

1
2
3
4
5
6
7
8
9
10
# 更新系统软件源
sudo apt update && sudo apt upgrade -y

# 安装必要的系统工具
sudo apt install -y python3 python3-pip python3-venv git nginx mysql-server curl wget

# 安装 SSH 服务(用于远程管理)
sudo apt install -y openssh-server
sudo systemctl start ssh
sudo systemctl enable ssh

二、MySQL 数据库配置

2.1 初始化 MySQL

1
2
# 进入 MySQL(Ubuntu 22.04 默认无密码)
sudo mysql

2.2 创建数据库和配置用户

1
2
3
4
5
6
7
8
9
-- 设置 root 密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '2022wlccj';

-- 创建项目数据库
CREATE DATABASE bossinfo CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 刷新权限
FLUSH PRIVILEGES;
EXIT;

2.3 导入数据

1
2
3
4
5
# 从 Windows 导出数据库
mysqldump -u root -p bossinfo > bossinfo.sql

# 在 Linux 服务器导入
mysql -u root -p bossinfo < bossinfo.sql

三、项目文件传输

3.1 使用 VMware 共享文件夹

Windows 端配置:

  1. 创建共享文件夹:D:\share
  2. VMware 菜单:虚拟机 → 设置 → 选项 → 共享文件夹
  3. 添加共享路径:D:\share,名称:share

Linux 端配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 安装 VMware Tools
sudo apt install -y open-vm-tools open-vm-tools-desktop

# 重启虚拟机
sudo reboot

# 挂载共享文件夹
sudo mkdir -p /mnt/hgfs
sudo vmhgfs-fuse .host:/ /mnt/hgfs -o allow_other -o uid=1000

# 设置开机自动挂载
sudo nano /etc/fstab
# 添加:.host:/ /mnt/hgfs fuse.vmhgfs-fuse allow_other,defaults 0 0

# 复制项目到本地
cp -r /mnt/hgfs/share/graduationdesign ~/graduationdesign

3.2 使用 SCP 传输(备选方案)

1
2
# 在 Windows PowerShell 执行
scp -r D:\JB\pycharm\bishe\graduationdesign 用户名@192.168.88.137:~/

四、Python 虚拟环境配置

4.1 创建虚拟环境

1
2
3
4
5
6
7
8
9
10
cd ~/graduationdesign

# 创建虚拟环境
python3 -m venv venv

# 激活虚拟环境
source venv/bin/activate

# 升级 pip
pip install --upgrade pip

4.2 安装项目依赖

1
2
3
# 安装所有依赖包
pip install django==5.1.4 pymysql pandas numpy scikit-learn xgboost \
joblib reportlab jieba wordcloud pillow gunicorn matplotlib

依赖包说明:

  • django==5.1.4:Web 框架
  • pymysql:MySQL 数据库驱动
  • pandas, numpy:数据处理
  • scikit-learn, xgboost:机器学习模型
  • joblib:模型序列化
  • reportlab:PDF 报告生成
  • jieba, wordcloud:中文分词和词云
  • gunicorn:WSGI 应用服务器

五、Django 项目配置

5.1 修改 settings.py

1
nano ~/graduationdesign/graduationdesign/settings.py

关键配置修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 1. 允许所有主机访问(生产环境应指定具体域名或IP)
ALLOWED_HOSTS = ['*']

# 2. 关闭调试模式(生产环境)
DEBUG = False # 开发调试时可设为 True

# 3. 数据库配置(确认密码正确)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': "bossinfo",
"USER": 'root',
"PASSWORD": "2022wlccj",
"HOST": "localhost",
"PORT": "3306",
}
}

# 4. 静态文件收集路径
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

5.2 修复数据库表名大小写问题

问题: Django 模型定义的表名与数据库实际表名大小写不一致。

1
nano ~/graduationdesign/myApp/models.py

修改第 31 行:

1
2
3
4
5
# 修改前
db_table = "jobInfo"

# 修改后
db_table = "jobinfo"

5.3 数据库迁移和静态文件收集

1
2
3
4
5
6
7
8
cd ~/graduationdesign
source venv/bin/activate

# 同步数据库结构
python manage.py migrate

# 收集静态文件到 staticfiles 目录
python manage.py collectstatic --noinput

六、应用服务器部署

6.1 使用 Django 开发服务器测试

1
2
# 启动开发服务器(仅用于测试)
python manage.py runserver 0.0.0.0:8000

访问:http://localhost:8000/myApp/login/

6.2 使用 Gunicorn 生产部署

1
2
3
4
5
# 前台运行测试
gunicorn --bind 0.0.0.0:8000 graduationdesign.wsgi:application

# 后台运行(生产环境)
gunicorn --bind 0.0.0.0:8000 --workers 4 --daemon graduationdesign.wsgi:application

Workers 数量计算:

1
2
推荐 workers 数 = 2 * CPU核心数 + 1
本项目:2 * 4 + 1 = 9(实际使用 4 个即可)

6.3 配置系统服务(开机自启)

1
sudo nano /etc/systemd/system/graduationdesign.service

写入配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=Graduation Design Gunicorn Service
After=network.target

[Service]
User=chengchujin
Group=chengchujin
WorkingDirectory=/home/chengchujin/graduationdesign
Environment="PATH=/home/chengchujin/graduationdesign/venv/bin"
ExecStart=/home/chengchujin/graduationdesign/venv/bin/gunicorn \
--workers 4 \
--bind 0.0.0.0:8000 \
graduationdesign.wsgi:application

[Install]
WantedBy=multi-user.target

启动服务:

1
2
3
sudo systemctl start graduationdesign
sudo systemctl enable graduationdesign
sudo systemctl status graduationdesign

七、Nginx 反向代理配置

7.1 创建 Nginx 配置文件

1
sudo nano /etc/nginx/sites-available/graduationdesign

配置内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
server {
listen 80;
server_name _;

# 静态文件
location /static/ {
alias /home/chengchujin/graduationdesign/staticfiles/;
expires 30d;
add_header Cache-Control "public, immutable";
}

# 媒体文件
location /media/ {
alias /home/chengchujin/graduationdesign/media/;
expires 30d;
}

# 反向代理到 Gunicorn
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;
}
}

7.2 启用配置并重启 Nginx

1
2
3
4
5
6
7
8
# 创建软链接
sudo ln -s /etc/nginx/sites-available/graduationdesign /etc/nginx/sites-enabled/

# 测试配置
sudo nginx -t

# 重启 Nginx
sudo systemctl restart nginx

八、防火墙配置

1
2
3
4
5
6
7
8
9
10
# 检查防火墙状态
sudo ufw status

# 允许 HTTP 和 SSH
sudo ufw allow 80/tcp
sudo ufw allow 22/tcp
sudo ufw allow 8000/tcp

# 启用防火墙(可选)
sudo ufw enable

九、部署架构图

1
2
3
4
5
6
7
8
9
客户端请求

Nginx (端口 80) - 反向代理

Gunicorn (端口 8000) - WSGI 服务器

Django 应用 - Web 框架

MySQL 数据库 (端口 3306)

十、常见问题排查

10.1 数据库连接失败

错误: Can't connect to MySQL server

解决方案:

1
2
3
4
5
6
7
8
# 检查 MySQL 服务状态
sudo systemctl status mysql

# 重启 MySQL
sudo systemctl restart mysql

# 检查密码是否正确
mysql -u root -p

10.2 表名不存在错误

错误: Table 'bossinfo.jobInfo' doesn't exist

原因: 数据库表名大小写不匹配

解决方案:

1
2
3
4
5
6
# 查看实际表名
mysql -u root -p -e "USE bossinfo; SHOW TABLES;"

# 修改 models.py 中的 db_table 配置
nano myApp/models.py
# 将 db_table = "jobInfo" 改为 db_table = "jobinfo"

10.3 静态文件 404

错误: 静态文件无法加载

解决方案:

1
2
3
4
5
6
7
8
# 重新收集静态文件
python manage.py collectstatic --noinput

# 检查 Nginx 配置中的路径是否正确
sudo nginx -t

# 检查文件权限
chmod -R 755 ~/graduationdesign/staticfiles/

10.4 Gunicorn 无法启动

错误: Address already in use

解决方案:

1
2
3
4
5
6
7
8
# 查找占用端口的进程
sudo lsof -i :8000

# 杀死进程
sudo kill -9 <PID>

# 或者更换端口
gunicorn --bind 0.0.0.0:8001 graduationdesign.wsgi:application

10.5 共享文件夹不显示

错误: /mnt/hgfs/share/ 目录为空

解决方案:

1
2
3
4
5
6
# 重新挂载
sudo umount /mnt/hgfs
sudo vmhgfs-fuse .host:/ /mnt/hgfs -o allow_other -o uid=1000

# 检查 VMware Tools 服务
sudo systemctl status vmtoolsd

10.6 Scikit-learn 版本警告

警告: InconsistentVersionWarning: Trying to unpickle estimator

原因: 模型训练时的 scikit-learn 版本与当前版本不一致

解决方案:

1
2
3
4
# 降级到训练时的版本
pip install scikit-learn==1.6.1

# 或者重新训练模型(推荐)

十一、性能优化建议

11.1 数据库优化

1
2
3
4
-- 为常用查询字段添加索引
CREATE INDEX idx_address ON jobinfo(address);
CREATE INDEX idx_educational ON jobinfo(educational);
CREATE INDEX idx_salary ON jobinfo(salary);

11.2 Gunicorn 优化

1
2
3
4
5
6
# 增加 workers 数量
gunicorn --bind 0.0.0.0:8000 --workers 8 --timeout 120 graduationdesign.wsgi:application

# 使用 gevent 异步模式
pip install gevent
gunicorn --bind 0.0.0.0:8000 --workers 4 --worker-class gevent graduationdesign.wsgi:application

11.3 Nginx 优化

1
2
3
4
5
6
# 启用 gzip 压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript;

# 增加缓存
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m;

十二、安全加固

12.1 修改 Django SECRET_KEY

1
2
3
# settings.py
import secrets
SECRET_KEY = secrets.token_urlsafe(50)

12.2 配置 HTTPS(可选)

1
2
3
4
5
# 安装 Certbot
sudo apt install certbot python3-certbot-nginx

# 获取 SSL 证书
sudo certbot --nginx -d yourdomain.com

12.3 限制数据库访问

1
2
3
4
-- 创建专用数据库用户
CREATE USER 'django_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON bossinfo.* TO 'django_user'@'localhost';
FLUSH PRIVILEGES;

十三、监控与日志

13.1 查看应用日志

1
2
3
4
5
6
7
8
# Gunicorn 日志
sudo journalctl -u graduationdesign -f

# Nginx 访问日志
sudo tail -f /var/log/nginx/access.log

# Nginx 错误日志
sudo tail -f /var/log/nginx/error.log

13.2 系统资源监控

1
2
3
4
5
6
7
8
# 查看内存使用
free -h

# 查看 CPU 使用
top

# 查看磁盘使用
df -h

十四、备份策略

14.1 数据库备份

1
2
3
4
5
# 手动备份
mysqldump -u root -p bossinfo > backup_$(date +%Y%m%d).sql

# 定时备份(crontab)
0 2 * * * mysqldump -u root -p2022wlccj bossinfo > /backup/bossinfo_$(date +\%Y\%m\%d).sql

14.2 项目文件备份

1
2
# 打包项目
tar -czf graduationdesign_backup_$(date +%Y%m%d).tar.gz ~/graduationdesign

十五、部署检查清单

  • [ ] 系统软件已更新
  • [ ] MySQL 已安装并配置
  • [ ] 数据库已创建并导入数据
  • [ ] Python 虚拟环境已创建
  • [ ] 项目依赖已安装
  • [ ] settings.py 已正确配置
  • [ ] 数据库表名大小写已修复
  • [ ] 静态文件已收集
  • [ ] Gunicorn 可正常启动
  • [ ] Nginx 已配置并启动
  • [ ] 防火墙规则已设置
  • [ ] 系统服务已配置开机自启
  • [ ] 项目可通过浏览器访问
  • [ ] 所有功能模块测试通过

十六、访问地址

本地访问

  • 登录页面:http://localhost:8000/myApp/login/
  • 首页:http://localhost:8000/myApp/home/

远程访问

  • 登录页面:http://192.168.88.137:8000/myApp/login/
  • 或通过 Nginx:http://192.168.88.137/myApp/login/

文档版本: v1.0
最后更新: 2026-03-08
部署环境: Ubuntu 22.04 LTS + Django 5.1.4 + MySQL 8.0