agentleFS
ElvitaStudioElvitaStudio/vps-deploy-skills/skills/fastapi-deploy/SKILL.md

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

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
  1. Very specific:

    Never commit .env to git: Add to .gitignore.

    line 73
  2. Very specific:

    After changing .env — just pm2 restart <name>, no rebuild needed.

    line 75

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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.py with app as the FastAPI instance
  • Environment variables loaded via python-dotenv (load_dotenv() in main.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.txt

3. Update dependencies (subsequent deploys)

cd /root/<project>/backend
.venv/bin/pip install -r requirements.txt

4. 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 save

5. Verify

pm2 logs <process-name> --lines 20
curl http://localhost:<PORT>/docs

Environment 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-password

Never 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-dev

CORS 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 defaults

Module 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 upstream

Logs show old code after deploy

pm2 restart <name>
# if still old: pm2 delete <name> → pm2 start ... → pm2 save

SQLite database locked

# Use check_same_thread=False for SQLite with FastAPI
engine = create_engine(
    DATABASE_URL,
    connect_args={"check_same_thread": False}
)

🇷🇺 На русском

Используй этот скилл когда нужно задеплоить, передеплоить или починить FastAPI бэкенд на VPS.

Стандартный флоу:

  1. Загрузи файлы через SFTP (без .venv/, __pycache__/)
  2. На сервере (первый раз): python3 -m venv .venv && .venv/bin/pip install -r requirements.txt
  3. Обновить зависимости: .venv/bin/pip install -r requirements.txt
  4. Перезапустить: pm2 restart <имя>

Важно:

  • .env всегда на сервере, никогда в git
  • После изменения .env → только pm2 restart, rebuild не нужен
  • CORS настраивай либо в FastAPI, либо в Nginx — не в обоих сразу
  • При 422 ошибках — добавь дефолтные значения в Pydantic модели

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.