Web App Deployment

Your First Deploy on a PaaS

35 分钟

This walk-through uses Render; Railway and Fly.io follow the same pattern (see their own tracks).

1. Push your code to GitHub

git init && git add . && git commit -m "Initial commit"
git remote add origin git@github.com:you/myapp.git
git push -u origin main

Make sure .gitignore excludes .env, .venv, *.db and uploads.

2. Create the database

In the Render dashboard: New → PostgreSQL. Choose a region near your users and copy the Internal Database URL (used by services in the same region; faster and free of egress).

3. Create the web service

New → Web Service, connect the repository, then:

Setting Value
Runtime Python
Build command ./render-build.sh or pip install -r requirements.txt && flask db upgrade
Start command gunicorn wsgi:app -c gunicorn.conf.py
Health check path /api/health

Build runs once per deploy (install packages, compile CSS, run migrations). Start runs your server and must keep running.

4. Environment variables

Add in the service's Environment tab:

SECRET_KEY=<64 random hex characters>
DATABASE_URL=<internal database URL>
FLASK_ENV=production
PYTHON_VERSION=3.11.9

Plus any API keys (Stripe, email). They are encrypted and injected at runtime; never put them in the repository.

5. Deploy and watch the logs

Render builds and starts the app. Watch the Logs tab. Common first-deploy failures:

Log message Cause
ModuleNotFoundError package missing from requirements.txt
KeyError: 'SECRET_KEY' environment variable not set
could not translate host name external vs internal database URL mix-up
health check failing app not binding to 0.0.0.0:$PORT, or /api/health erroring
relation "users" does not exist migrations did not run

6. Create the first admin

Use the platform's Shell to run one-off commands:

flask create-admin

Infrastructure as code

A render.yaml blueprint in the repository describes the service, database and environment variables, so the whole setup can be recreated in one click and reviewed in Git.

Exercise

Deploy a small Flask app with a PostgreSQL database and a /api/health route. Break it on purpose (remove an environment variable), read the logs, and fix it.

Check yourself

  • What is the difference between the build and start commands?
  • Why use the internal database URL?
  • Where do API keys go?

你的进度只保存在此浏览器中。

提交审核(10 积分)