Files
GPA_Monitoring/Debian服务器部署指南.md
ChuXun 609b2334e8 1
2026-01-18 18:48:20 +08:00

409 lines
8.1 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.
# 成绩监控系统 - Debian服务器部署指南
## 📦 项目文件说明
### 必需文件(需要上传到服务器)
| 文件名 | 用途 | 何时使用 |
|--------|------|----------|
| `monitor.py` | **主程序** | 运行监控的核心文件 |
| `config.ini` | **配置文件** | 包含账号密码、邮箱配置(⚠️ 敏感文件) |
| `requirements.txt` | **Python依赖列表** | 安装Python包时使用 |
| `setup_python.sh` | **环境安装脚本** | 首次部署时执行,安装所有依赖 |
| `grade-monitor.service` | **systemd服务配置** | 设置开机自启和后台运行 |
### 可选文件(参考文档)
| 文件名 | 用途 |
|--------|------|
| `config模板.ini` | 配置文件模板,新用户参考 |
| `readme.md` | 项目说明文档 |
| `部署.md` | 详细部署步骤 |
| `常见问题解决.md` | 常见问题和解决方案 |
### 不需要的文件(不要上传)
- `venv/` - 虚拟环境(服务器上重新创建)
- `.git/` - Git仓库可选
- `.last_*` - 运行时生成的缓存文件
- `monitor.log` - 运行时生成的日志文件
---
## 🚀 Debian服务器部署步骤
### 第一步:准备文件
```bash
# 在本地打包必需文件
cd /mnt/e/50425/Documents/Github/GPA_Monitoring
# 创建压缩包(只包含必需文件)
tar -czf gpa_monitor.tar.gz \
monitor.py \
config.ini \
requirements.txt \
setup_python.sh \
grade-monitor.service \
readme.md
# 查看压缩包内容
tar -tzf gpa_monitor.tar.gz
```
### 第二步:上传到服务器
```bash
# 方法1使用 scp
scp gpa_monitor.tar.gz 用户名@服务器IP:/home/用户名/
# 方法2使用 rsync推荐
rsync -avz gpa_monitor.tar.gz 用户名@服务器IP:/home/用户名/
# 方法3使用 sftp
sftp 用户名@服务器IP
put gpa_monitor.tar.gz
```
### 第三步:在服务器上解压并安装
```bash
# 登录到Debian服务器
ssh 用户名@服务器IP
# 解压文件
cd ~
tar -xzf gpa_monitor.tar.gz
cd gpa_monitor # 或者你解压到的目录
# 给脚本添加执行权限
chmod +x setup_python.sh
# 运行安装脚本会自动安装Python、创建虚拟环境、安装依赖
./setup_python.sh
```
### 第四步:检查配置文件
```bash
# 编辑配置文件(如果需要修改)
nano config.ini
# 确认配置正确:
# - USERNAME 和 PASSWORD学号和密码
# - 邮箱配置SENDER_EMAIL、SENDER_PASSWORD、RECEIVER_EMAIL
# - CHECK_INTERVAL建议120秒以上
```
### 第五步:测试运行
```bash
# 激活虚拟环境
source venv/bin/activate
# 测试运行(获取一次成绩,不进行监控)
python3 monitor.py --test
# 如果测试成功,运行正式监控
python3 monitor.py
# 按 Ctrl+C 停止
```
---
## 🔧 设置后台运行(三选一)
### 方案A使用 tmux推荐简单易用
```bash
# 1. 安装 tmux如果没有
sudo apt update
sudo apt install tmux
# 2. 创建会话并运行
tmux new -s grade_monitor
source venv/bin/activate
python3 monitor.py
# 3. 离开会话(程序继续运行)
# 按 Ctrl+B然后按 D
# 4. 重新连接查看
tmux attach -t grade_monitor
# 5. 查看所有会话
tmux ls
```
**tmux 使用时机:**
- ✅ 测试阶段使用
- ✅ 需要随时查看程序输出
- ✅ 临时运行,不需要开机自启
---
### 方案B使用 systemd 服务(推荐,生产环境)
**`grade-monitor.service` 文件用途:**
这是 systemd 服务配置文件,告诉系统如何启动、管理和自动重启你的程序。
```bash
# 1. 编辑服务文件,修改用户名和路径
nano grade-monitor.service
# 确保这些路径正确:
# User=你的用户名
# WorkingDirectory=/home/你的用户名/gpa_monitor
# ExecStart=/home/你的用户名/gpa_monitor/venv/bin/python3 /home/你的用户名/gpa_monitor/monitor.py
# 2. 复制服务文件到系统目录
sudo cp grade-monitor.service /etc/systemd/system/
# 3. 重新加载 systemd
sudo systemctl daemon-reload
# 4. 启动服务
sudo systemctl start grade-monitor
# 5. 查看状态
sudo systemctl status grade-monitor
# 6. 设置开机自启
sudo systemctl enable grade-monitor
```
**systemd 常用命令:**
```bash
# 启动
sudo systemctl start grade-monitor
# 停止
sudo systemctl stop grade-monitor
# 重启
sudo systemctl restart grade-monitor
# 查看状态
sudo systemctl status grade-monitor
# 查看日志
journalctl -u grade-monitor -f
# 开机自启
sudo systemctl enable grade-monitor
# 禁用自启
sudo systemctl disable grade-monitor
```
**systemd 使用时机:**
- ✅ 生产环境长期运行
- ✅ 需要开机自启动
- ✅ 程序崩溃后自动重启
- ✅ 系统化管理
---
### 方案C使用 nohup最简单但不推荐
```bash
# 后台运行
source venv/bin/activate
nohup python3 monitor.py > output.log 2>&1 &
# 查看进程
ps aux | grep monitor.py
# 停止程序
pkill -f monitor.py
```
**nohup 使用时机:**
- ✅ 临时快速运行
- ❌ 不适合长期运行
- ❌ 程序崩溃不会自动重启
---
## 📊 监控和维护
### 查看日志
```bash
# 查看监控日志(程序自己的日志)
tail -f ~/gpa_monitor/monitor.log
# 查看最后100行
tail -n 100 ~/gpa_monitor/monitor.log
# 搜索关键词
grep "新增课程" ~/gpa_monitor/monitor.log
# 查看系统日志如果用systemd
sudo journalctl -u grade-monitor -f
sudo journalctl -u grade-monitor --since "1 hour ago"
```
### 检查运行状态
```bash
# 方法1查看进程
ps aux | grep monitor.py
# 方法2查看日志时间戳
ls -lh ~/gpa_monitor/monitor.log
# 方法3systemd状态如果用systemd
sudo systemctl status grade-monitor
```
### 更新程序
```bash
# 1. 停止程序
# tmux: Ctrl+C 或 tmux kill-session -t grade_monitor
# systemd: sudo systemctl stop grade-monitor
# 2. 备份配置
cp config.ini config.ini.backup
# 3. 上传新版本文件并解压
# 4. 恢复配置
cp config.ini.backup config.ini
# 5. 重启程序
# tmux: 重新运行
# systemd: sudo systemctl restart grade-monitor
```
---
## 🔒 安全建议
### 1. 保护配置文件
```bash
# 设置文件权限(只有所有者可读写)
chmod 600 config.ini
# 查看权限
ls -l config.ini
# 应该显示:-rw------- 1 用户名 用户名
```
### 2. 不要上传敏感文件到 GitHub
`.gitignore` 中添加:
```
config.ini
*.log
.last_*
venv/
```
### 3. 定期检查日志
```bash
# 检查是否有异常
grep -i "error\|fail\|warning" monitor.log
# 检查登录情况
grep "登录" monitor.log | tail -20
```
---
## ❓ 常见问题
### Q1: 如何确认程序在运行?
```bash
# 方法1查看进程
ps aux | grep monitor.py
# 方法2查看日志最后几行
tail monitor.log
# 方法3查看文件修改时间
ls -lh monitor.log
```
### Q2: 程序报错"请不要过快点击"怎么办?
```bash
# 编辑配置文件,增加检查间隔
nano config.ini
# 修改 CHECK_INTERVAL 为更大的值如300秒
CHECK_INTERVAL = 300
# 重启程序
```
### Q3: 如何在多台服务器部署?
```bash
# 每台服务器重复部署步骤,注意:
# 1. 每台服务器使用不同的监控账号(如果可能)
# 2. 适当增加 CHECK_INTERVAL 避免同时访问
# 3. 可以设置不同的邮件接收地址
```
### Q4: 忘记 tmux 会话名怎么办?
```bash
# 列出所有会话
tmux ls
# 连接到第一个会话
tmux attach
```
---
## 📝 快速命令参考
```bash
# === 部署 ===
tar -xzf gpa_monitor.tar.gz
cd gpa_monitor
chmod +x setup_python.sh
./setup_python.sh
# === 运行 ===
# 测试
source venv/bin/activate && python3 monitor.py --test
# tmux运行
tmux new -s grade_monitor
source venv/bin/activate && python3 monitor.py
# systemd运行
sudo cp grade-monitor.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl start grade-monitor
sudo systemctl enable grade-monitor
# === 监控 ===
# 查看日志
tail -f monitor.log
# 查看状态
sudo systemctl status grade-monitor
# === 停止 ===
# tmux: Ctrl+C
# systemd: sudo systemctl stop grade-monitor
# nohup: pkill -f monitor.py
```
---
## 📞 获取帮助
- 查看项目 README: `cat readme.md`
- 查看常见问题: `cat 常见问题解决.md`
- 查看详细部署: `cat 部署.md`
- 查看程序帮助: `python3 monitor.py --help`