大约 1 分钟
创建
python3 -m venv .venv
提示下方
apt install python3.12-venv
激活
linux\mac
source .venv/bin/activate
windows
powershell
.venv\Scripts\Activate.ps1
CMD
.venv\Scripts\activate.bat
Ubuntu 安装uv
1. 官方脚本:
```bash
# 安装uv
curl -LsSf https://astral.sh/uv/install.sh | sh
# 重新加载shell
source ~/.bashrc
# 验证安装
uv --version
```
2. 手动下载安装
```bash
# 创建安装目录
mkdir -p ~/.local/bin
# 下载最新版本的uv
wget https://github.com/astral-sh/uv/releases/latest/download/uv-x86_64-unknown-linux-gnu.tar.gz
# 解压
tar -xzf uv-x86_64-unknown-linux-gnu.tar.gz
# 移动到bin目录
mv uv ~/.local/bin/
# 添加到PATH
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
# 验证安装
uv --version
```
国内镜像配置:
```bash
# 使用清华大学镜像加速
uv config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
# 或者使用阿里云镜像
uv config set global.index-url https://mirrors.aliyun.com/pypi/simple/
```gunicorn部署
# 创建配置文件
cat > gunicorn.conf.py << 'EOF'
# Gunicorn配置文件
bind = "127.0.0.1:5000"
workers = 4
worker_class = "sync"
worker_connections = 1000
timeout = 30
keepalive = 2
max_requests = 1000
max_requests_jitter = 100
preload_app = True
accesslog = "/opt/python-pro/easy-doc-ai/data/logs/access.log"
errorlog = "/opt/python-pro/easy-doc-ai/data/logs/error.log"
loglevel = "info"
daemon = True
pidfile = "/opt/python-pro/easy-doc-ai/gunicorn.pid"
EOF#!/bin/bash
# 服务管理脚本
PROJECT_DIR="/opt/python-pro/easy-doc-ai"
CODE_DIR="$PROJECT_DIR/code"
PID_FILE="$PROJECT_DIR/gunicorn.pid"
VENV_DIR="$CODE_DIR/.venv"
start() {
if [ -f "$PID_FILE" ]; then
echo "Service is already running (PID: $(cat $PID_FILE))"
return 1
fi
echo "Starting Gunicorn service..."
cd "$CODE_DIR"
source "$VENV_DIR/bin/activate"
"$VENV_DIR/bin/gunicorn" --config gunicorn.conf.py app:app
echo "Service started"
}
stop() {
if [ ! -f "$PID_FILE" ]; then
echo "Service is not running"
return 1
fi
PID=$(cat "$PID_FILE")
echo "Stopping Gunicorn service (PID: $PID)..."
kill -TERM $PID
sleep 2
if kill -0 $PID 2>/dev/null; then
echo "Force killing process..."
kill -9 $PID
fi
rm -f "$PID_FILE"
echo "Service stopped"
}
status() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if kill -0 $PID 2>/dev/null; then
echo "Service is running (PID: $PID)"
else
echo "PID file exists but process is not running"
rm -f "$PID_FILE"
fi
else
echo "Service is not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 1
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac用法:
# 给脚本执行权限
chmod +x service.sh
# 启动服务
./service.sh start
# 停止服务
./service.sh stop
# 重启服务
./service.sh restart
# 查看状态
./service.sh status