完成Gunicorn启动服务器的指南文档和快速启动停止脚本
This commit is contained in:
56
docs/deployment/Gunicorn快速参考.md
Normal file
56
docs/deployment/Gunicorn快速参考.md
Normal file
@@ -0,0 +1,56 @@
|
||||
# Gunicorn快速参考
|
||||
|
||||
## 🚀 一键操作命令
|
||||
|
||||
### 启动服务
|
||||
```bash
|
||||
./scripts/start_gunicorn.sh
|
||||
```
|
||||
|
||||
### 停止服务
|
||||
```bash
|
||||
./scripts/stop_gunicorn.sh
|
||||
```
|
||||
|
||||
### 重启服务
|
||||
```bash
|
||||
./scripts/restart_gunicorn.sh
|
||||
```
|
||||
|
||||
### 查看状态
|
||||
```bash
|
||||
./scripts/status_gunicorn.sh
|
||||
```
|
||||
|
||||
## 📋 常用命令
|
||||
|
||||
| 操作 | 命令 |
|
||||
|------|------|
|
||||
| 启动 | `./scripts/start_gunicorn.sh` |
|
||||
| 停止 | `./scripts/stop_gunicorn.sh` |
|
||||
| 重启 | `./scripts/restart_gunicorn.sh` |
|
||||
| 状态 | `./scripts/status_gunicorn.sh` |
|
||||
| 查看日志 | `tail -f logs/gunicorn_access.log` |
|
||||
| 测试访问 | `curl http://localhost:5002/` |
|
||||
|
||||
## 🌐 访问信息
|
||||
|
||||
- **本地访问**: http://localhost:5002
|
||||
- **外网访问**: http://101.43.95.130:5002
|
||||
- **服务端口**: 5002
|
||||
- **Python环境**: 3.12.7 (myenv)
|
||||
|
||||
## 📁 重要文件
|
||||
|
||||
- **配置文件**: `gunicorn.conf.py`
|
||||
- **PID文件**: `logs/gunicorn.pid`
|
||||
- **访问日志**: `logs/gunicorn_access.log`
|
||||
- **错误日志**: `logs/gunicorn_error.log`
|
||||
- **管理脚本**: `scripts/`
|
||||
|
||||
## ⚠️ 注意事项
|
||||
|
||||
1. 确保在项目目录下执行命令
|
||||
2. 确保conda环境已激活
|
||||
3. 确保5002端口未被占用
|
||||
4. 确保防火墙已开放5002端口
|
||||
241
docs/deployment/Gunicorn服务管理指南.md
Normal file
241
docs/deployment/Gunicorn服务管理指南.md
Normal file
@@ -0,0 +1,241 @@
|
||||
# Gunicorn服务管理指南
|
||||
|
||||
## 概述
|
||||
|
||||
本文档介绍如何使用Gunicorn管理Flask提示词大师应用的生产环境服务。
|
||||
|
||||
## 环境信息
|
||||
|
||||
- **Python版本**: 3.12.7
|
||||
- **Conda环境**: myenv
|
||||
- **项目路径**: /home/renjianbo/aitsc
|
||||
- **服务端口**: 5002
|
||||
- **外网地址**: http://101.43.95.130:5002
|
||||
|
||||
## 快速启动命令
|
||||
|
||||
### 一键启动脚本
|
||||
|
||||
```bash
|
||||
# 进入项目目录
|
||||
cd /home/renjianbo/aitsc
|
||||
|
||||
# 激活conda环境并启动服务
|
||||
eval "$(/home/renjianbo/miniconda3/bin/conda shell.bash hook)" && conda activate myenv && gunicorn -c gunicorn.conf.py run_dev:app
|
||||
```
|
||||
|
||||
### 后台启动
|
||||
|
||||
```bash
|
||||
# 后台启动服务
|
||||
cd /home/renjianbo/aitsc
|
||||
eval "$(/home/renjianbo/miniconda3/bin/conda shell.bash hook)" && conda activate myenv && nohup gunicorn -c gunicorn.conf.py run_dev:app > logs/gunicorn.log 2>&1 &
|
||||
```
|
||||
|
||||
## 服务管理命令
|
||||
|
||||
### 1. 启动服务
|
||||
|
||||
```bash
|
||||
# 方法1: 直接启动
|
||||
cd /home/renjianbo/aitsc
|
||||
eval "$(/home/renjianbo/miniconda3/bin/conda shell.bash hook)" && conda activate myenv && gunicorn -c gunicorn.conf.py run_dev:app
|
||||
|
||||
# 方法2: 后台启动
|
||||
cd /home/renjianbo/aitsc
|
||||
eval "$(/home/renjianbo/miniconda3/bin/conda shell.bash hook)" && conda activate myenv && nohup gunicorn -c gunicorn.conf.py run_dev:app > logs/gunicorn.log 2>&1 &
|
||||
```
|
||||
|
||||
### 2. 停止服务
|
||||
|
||||
```bash
|
||||
# 方法1: 使用PID文件停止
|
||||
kill -TERM $(cat logs/gunicorn.pid)
|
||||
|
||||
# 方法2: 强制停止
|
||||
kill -9 $(cat logs/gunicorn.pid)
|
||||
|
||||
# 方法3: 查找进程并停止
|
||||
ps aux | grep gunicorn | grep -v grep | awk '{print $2}' | xargs kill -TERM
|
||||
```
|
||||
|
||||
### 3. 重启服务
|
||||
|
||||
```bash
|
||||
# 方法1: 优雅重启(推荐)
|
||||
kill -HUP $(cat logs/gunicorn.pid)
|
||||
|
||||
# 方法2: 停止后重新启动
|
||||
kill -TERM $(cat logs/gunicorn.pid) && sleep 3 && eval "$(/home/renjianbo/miniconda3/bin/conda shell.bash hook)" && conda activate myenv && gunicorn -c gunicorn.conf.py run_dev:app
|
||||
```
|
||||
|
||||
### 4. 查看服务状态
|
||||
|
||||
```bash
|
||||
# 查看进程状态
|
||||
ps aux | grep gunicorn | grep -v grep
|
||||
|
||||
# 查看端口监听状态
|
||||
ss -tlnp | grep :5002
|
||||
|
||||
# 查看PID文件
|
||||
cat logs/gunicorn.pid
|
||||
```
|
||||
|
||||
### 5. 查看日志
|
||||
|
||||
```bash
|
||||
# 查看访问日志
|
||||
tail -f logs/gunicorn_access.log
|
||||
|
||||
# 查看错误日志
|
||||
tail -f logs/gunicorn_error.log
|
||||
|
||||
# 查看应用日志
|
||||
tail -f logs/app.log
|
||||
|
||||
# 查看实时日志
|
||||
tail -f logs/gunicorn.log
|
||||
```
|
||||
|
||||
## 服务配置
|
||||
|
||||
### Gunicorn配置文件
|
||||
|
||||
配置文件位置: `gunicorn.conf.py`
|
||||
|
||||
主要配置项:
|
||||
- **端口**: 5002
|
||||
- **工作进程数**: CPU核心数 × 2 + 1
|
||||
- **每个进程连接数**: 1000
|
||||
- **超时时间**: 30秒
|
||||
- **日志级别**: info
|
||||
|
||||
### 环境变量
|
||||
|
||||
```bash
|
||||
# 设置环境变量
|
||||
export FLASK_ENV=production
|
||||
export SECRET_KEY="your-production-secret-key"
|
||||
export LLM_API_KEY="your-api-key"
|
||||
```
|
||||
|
||||
## 故障排除
|
||||
|
||||
### 1. 服务无法启动
|
||||
|
||||
```bash
|
||||
# 检查端口是否被占用
|
||||
ss -tlnp | grep :5002
|
||||
|
||||
# 检查日志文件
|
||||
cat logs/gunicorn_error.log
|
||||
|
||||
# 检查Python环境
|
||||
python --version
|
||||
pip list | grep gunicorn
|
||||
```
|
||||
|
||||
### 2. 服务响应慢
|
||||
|
||||
```bash
|
||||
# 检查系统资源
|
||||
top
|
||||
free -h
|
||||
df -h
|
||||
|
||||
# 检查进程状态
|
||||
ps aux | grep gunicorn
|
||||
```
|
||||
|
||||
### 3. 端口访问失败
|
||||
|
||||
```bash
|
||||
# 检查防火墙
|
||||
sudo firewall-cmd --list-ports
|
||||
|
||||
# 开放端口
|
||||
sudo firewall-cmd --add-port=5002/tcp --permanent
|
||||
sudo firewall-cmd --reload
|
||||
```
|
||||
|
||||
## 性能监控
|
||||
|
||||
### 1. 系统资源监控
|
||||
|
||||
```bash
|
||||
# CPU和内存使用情况
|
||||
top -p $(cat logs/gunicorn.pid)
|
||||
|
||||
# 网络连接数
|
||||
ss -s | grep tcp
|
||||
```
|
||||
|
||||
### 2. 应用性能监控
|
||||
|
||||
```bash
|
||||
# 查看请求统计
|
||||
tail -f logs/gunicorn_access.log | grep -E "(GET|POST)"
|
||||
|
||||
# 查看响应时间
|
||||
tail -f logs/gunicorn_access.log | awk '{print $NF}'
|
||||
```
|
||||
|
||||
## 自动化脚本
|
||||
|
||||
### 启动脚本 (start_gunicorn.sh)
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
cd /home/renjianbo/aitsc
|
||||
eval "$(/home/renjianbo/miniconda3/bin/conda shell.bash hook)"
|
||||
conda activate myenv
|
||||
export FLASK_ENV=production
|
||||
gunicorn -c gunicorn.conf.py run_dev:app
|
||||
```
|
||||
|
||||
### 停止脚本 (stop_gunicorn.sh)
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
cd /home/renjianbo/aitsc
|
||||
kill -TERM $(cat logs/gunicorn.pid)
|
||||
echo "Gunicorn服务已停止"
|
||||
```
|
||||
|
||||
### 重启脚本 (restart_gunicorn.sh)
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
cd /home/renjianbo/aitsc
|
||||
kill -TERM $(cat logs/gunicorn.pid)
|
||||
sleep 3
|
||||
eval "$(/home/renjianbo/miniconda3/bin/conda shell.bash hook)"
|
||||
conda activate myenv
|
||||
export FLASK_ENV=production
|
||||
gunicorn -c gunicorn.conf.py run_dev:app
|
||||
```
|
||||
|
||||
## 常用命令速查
|
||||
|
||||
| 操作 | 命令 |
|
||||
|------|------|
|
||||
| 启动服务 | `eval "$(/home/renjianbo/miniconda3/bin/conda shell.bash hook)" && conda activate myenv && gunicorn -c gunicorn.conf.py run_dev:app` |
|
||||
| 停止服务 | `kill -TERM $(cat logs/gunicorn.pid)` |
|
||||
| 重启服务 | `kill -HUP $(cat logs/gunicorn.pid)` |
|
||||
| 查看状态 | `ps aux | grep gunicorn \| grep -v grep` |
|
||||
| 查看端口 | `ss -tlnp \| grep :5002` |
|
||||
| 查看日志 | `tail -f logs/gunicorn_access.log` |
|
||||
| 测试访问 | `curl http://localhost:5002/` |
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **环境激活**: 每次操作前必须激活conda环境
|
||||
2. **端口冲突**: 确保5002端口未被其他服务占用
|
||||
3. **权限问题**: 确保有logs目录的写入权限
|
||||
4. **防火墙**: 确保5002端口在防火墙中开放
|
||||
5. **日志轮转**: 定期清理日志文件避免磁盘空间不足
|
||||
|
||||
## 联系信息
|
||||
|
||||
如有问题,请查看日志文件或联系系统管理员。
|
||||
Reference in New Issue
Block a user