fastapi-deploy
Skill1 starsChanged 5 months agoMITView on GitHub
Deploy FastAPI applications with uvicorn to a VPS, managed by PM2. Use this skill when: the user wants to deploy, redeploy, or troubleshoot a FastAPI backend on a remote VPS. Store in .env in the project root. Load with python-dotenv: Never commit .env to git. Add to .gitignore. After changing .env — just pm2 restart <name>, no rebuild needed. ⚠️ Do NOT add CORS headers in Nginx if already set in FastAPI — duplicate headers cause browser errors. Pick one…
- Reads credentials
- Installs packages
In this repository4 other files it gives its agents.
- nextjs-deploySkill
- nginxSkill
- pm2Skill
- ssl-certbotSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- could be automatic: a setting could enforce it
- Very specific: line 73
Never commit
.envto git: Add to .gitignore. - Very specific: line 75
After changing .env — just pm2 restart <name>, no rebuild needed.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
FastAPI Deploy Skill
Deploy FastAPI applications with uvicorn to a VPS, managed by PM2.
Use this skill when: the user wants to deploy, redeploy, or troubleshoot a FastAPI backend on a remote VPS.
Stack assumptions
- Python 3.10+
- Virtual environment at
.venv/inside the project directory - PM2 managing the uvicorn process
- App entry point:
app/main.pywithappas the FastAPI instance - Environment variables loaded via
python-dotenv(load_dotenv()inmain.py)
Standard deploy flow
1. Upload files to server
Upload project directory via SFTP excluding .venv/, __pycache__/, *.pyc.
2. Set up virtualenv on server (first time only)
cd /root/<project>/backend
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt3. Update dependencies (subsequent deploys)
cd /root/<project>/backend
.venv/bin/pip install -r requirements.txt4. Restart PM2 process
pm2 restart <process-name>
# or start for the first time:
pm2 start ".venv/bin/python -m uvicorn app.main:app --host 0.0.0.0 --port <PORT>" \
--name <process-name>
pm2 save5. Verify
pm2 logs <process-name> --lines 20
curl http://localhost:<PORT>/docsEnvironment variables
Store in .env in the project root. Load with python-dotenv:
# app/main.py
from dotenv import load_dotenv
load_dotenv()# /root/<project>/backend/.env
DATABASE_URL=sqlite:///./data.db
SECRET_KEY=your-secret-key
TELEGRAM_BOT_TOKEN=your-token
ADMIN_USERNAME=admin
ADMIN_PASSWORD=secure-passwordNever commit .env to git. Add to .gitignore.
After changing .env — just pm2 restart <name>, no rebuild needed.
PM2 start command
# Standard uvicorn via PM2
pm2 start ".venv/bin/python -m uvicorn app.main:app --host 0.0.0.0 --port 8000" \
--name myapp-backend
# With auto-reload (development only — never in production)
pm2 start ".venv/bin/python -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000" \
--name myapp-backend-devCORS configuration
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=[
"https://yourdomain.com",
"https://app.yourdomain.com",
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)⚠️ Do NOT add CORS headers in Nginx if already set in FastAPI — duplicate headers cause browser errors. Pick one place.
Common issues & fixes
422 Unprocessable Entity
The request body doesn't match the Pydantic model. Check:
# Add default values to optional fields
class AnalyzeRequest(BaseModel):
symbol: str
model: str = "claude-sonnet-4-6" # ← add defaultsModule not found
# Make sure you're using the venv pip, not system pip
.venv/bin/pip install <package>
# NOT: pip install <package>Port conflict with another service
pm2 list
lsof -i :<PORT>
# change port in PM2 start command and Nginx upstreamLogs show old code after deploy
pm2 restart <name>
# if still old: pm2 delete <name> → pm2 start ... → pm2 saveSQLite database locked
# Use check_same_thread=False for SQLite with FastAPI
engine = create_engine(
DATABASE_URL,
connect_args={"check_same_thread": False}
)🇷🇺 На русском
Используй этот скилл когда нужно задеплоить, передеплоить или починить FastAPI бэкенд на VPS.
Стандартный флоу:
- Загрузи файлы через SFTP (без
.venv/,__pycache__/) - На сервере (первый раз):
python3 -m venv .venv && .venv/bin/pip install -r requirements.txt - Обновить зависимости:
.venv/bin/pip install -r requirements.txt - Перезапустить:
pm2 restart <имя>
Важно:
.envвсегда на сервере, никогда в git- После изменения
.env→ толькоpm2 restart, rebuild не нужен - CORS настраивай либо в FastAPI, либо в Nginx — не в обоих сразу
- При 422 ошибках — добавь дефолтные значения в Pydantic модели
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

