# fastapi_template **Repository Path**: gunhe/fastapi_template ## Basic Information - **Project Name**: fastapi_template - **Description**: fastapi的骨架 - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-06-06 - **Last Updated**: 2026-01-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # skeleton_fastapi ## 介绍 fastapi的骨架 ## 安装教程 ```bash # 下载代码 git clone https://gitee.com/gunhe/skeleton_fastapi.git # 进入项目目录 cd skeleton_fastapi/ # 新建虚拟环境 python -m venv .venv source .venv/bin/activate python -m pip install --upgrade pip # 安装依赖 pip install -r ./requirements.txt # 迁移数据库 alembic revision --autogenerate -m "commit_xxx" # 手动更新数据库 alembic upgrade head # alembic 资料 # https://www.cnblogs.com/Amd794/p/18868186 # 启动项目 uvicorn main:app --host 0.0.0.0 --port 8080 --reload ``` ## 部署 systemd下运行fastapi 1. 创建 systemd 服务文件 新建服务配置文件 /etc/systemd/system/fastapi.service(需 root 权限): ```conf [Unit] Description=FastAPI Application Service # 项目名称 After=network.target [Service] User=root WorkingDirectory=/home/fhj/code/skeleton_fastapi # 项目路径 ExecStart=/home/fhj/.local/bin/uvicorn app.main:app --host 0.0.0.0 --port 8080 --reload # 项目启动命令 Restart=always # 崩溃后自动重启 RestartSec=5 # 重启间隔(秒) [Install] WantedBy=multi-user.target # 多用户模式下启动 ``` 2. 启动并启用服务 ```bash # 重新加载 systemd 配置 systemctl daemon-reload # 启动服务 systemctl start fastapi # 启用开机自启 systemctl enable fastapi # 查看服务状态(确认运行中) systemctl status fastapi # 查看实时日志(排查问题) journalctl -u fastapi -f ``` 3. 重启 ```bash # 1. 重新加载 systemd 配置(确保修改生效) sudo systemctl daemon-reload # 2. 重启服务 sudo systemctl restart fastapi # 3. 验证服务状态(确认是否运行) sudo systemctl status fastapi ```