How to Deploy a Python App Without Docker on a VPS
Some links on this page are affiliate links. If you sign up through them we may earn a commission at no extra cost to you. We only link to services we have actually used, and commission never decides what we recommend.
You already got it running once. You SSH'd in, activated a virtualenv, started gunicorn, and it answered on port 8000 — then you closed the terminal and it was gone. The wall you are about to hit is not installation. It is making the process survive your logout, survive a reboot, and survive the next deploy without breaking what the last one fixed.
Quick verdict
For one Python app on one VPS, run it under systemd with a virtualenv and gunicorn (WSGI) or uvicorn (ASGI), and put nginx in front. That is the boring answer, it costs you one small VPS, and it has no lock-in at all — the same unit file works on any Debian or Ubuntu box you rent anywhere. The tradeoff is real: you now own OS patching, TLS renewal, log rotation, and the 3am page. If you would rather not own that, a PaaS with Python auto-detection is the correct choice and I will not talk you out of it.
What you are actually choosing between
| Option | Cost shape | What actually decides it |
|---|---|---|
| systemd + venv + gunicorn/uvicorn + nginx on a VPS | Cheapest tier: one small VPS, flat monthly | Highest ops burden, zero lock-in, performance ceiling is whatever the box has. Pick this for one app, one server, a team of one. |
| PaaS with Python auto-detect (Render, Railway, Heroku-style buildpacks) | Mid, scales with instance size | Low ops burden, moderate lock-in, ceiling capped per plan. Pick this if owning patching is worse than paying for it. |
| Serverless (Lambda + Mangum, Zappa, Cloud Functions) | Per-request; cheapest at idle, ugly at sustained load | Cold starts, packaging pain, high lock-in. Pick this for spiky or near-zero traffic, not for a steady web app. |
| Managed app runner that builds from source (Cloud Run, App Engine) | Mid | Low ops, but you are one config file from writing a Dockerfile anyway. Pick this if you already live in that cloud. |
The performance ceiling on a single VPS is higher than most people assume for a small app, and lower than they assume once they add a background worker, Postgres, and Redis to the same 2GB box.
Step by step
1. Stop installing into the system Python. On Debian 12 and Ubuntu 23.04 and later, sudo pip install fails with error: externally-managed-environment (PEP 668). That is not a bug to work around with --break-system-packages. It is the OS telling you that you will break apt if you keep going. Create a service user and a venv owned by it:
sudo adduser --system --group --home /srv/myapp myapp
sudo -u myapp python3 -m venv /srv/myapp/venv
sudo -u myapp /srv/myapp/venv/bin/pip install -r requirements.txt
Pin your dependencies. An unpinned requirements.txt means the deploy that works today fails next month because a transitive dependency bumped a major version.
2. Install build headers only if a wheel is missing. Most packages ship manylinux wheels. When one does not — older psycopg2, some lxml or Pillow versions — you need sudo apt install python3-dev build-essential libpq-dev and friends. Build on the same architecture you deploy to. Building on an arm64 Mac and copying the venv to an x86 VPS gives you an Exec format error that looks like a systemd problem and is not.
3. Pick the server and bind it to loopback. gunicorn for Django and Flask, uvicorn for FastAPI and anything ASGI. Bind to 127.0.0.1:8000, not 0.0.0.0:8000, so the app is unreachable except through nginx. A unix socket is marginally faster and slightly harder to get permissions right on; start with loopback TCP and move later if you care.
4. Write the systemd unit — this is where the 502 comes from. The failure people hit next is a unit that looks right and dies on boot, because ExecStart used a relative path or the venv that systemd points at is not the venv they installed into. Use absolute paths everywhere.
[Unit]
Description=myapp
After=network.target
[Service]
User=myapp
Group=myapp
WorkingDirectory=/srv/myapp/current/app
Environment="PATH=/srv/myapp/current/venv/bin"
EnvironmentFile=/srv/myapp/.env
ExecStart=/srv/myapp/current/venv/bin/gunicorn \
--workers 3 \
--bind 127.0.0.1:8000 \
--access-logfile - \
--error-logfile - \
config.wsgi:application
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
Three things bite here. WorkingDirectory is what relative file paths and most .env loaders resolve against — get it wrong and uploads land in /. EnvironmentFile does not do shell expansion, so no $OTHER_VAR and no export; keep it to plain KEY=value lines. And Restart=always will happily restart a permanently broken app in a loop, so a green systemctl status for two seconds means nothing.
5. Put nginx in front and set the body size. The default client_max_body_size is 1m, which means every upload over a megabyte fails with a 413 that your app never sees and your logs may not show.
server {
listen 80;
server_name app.example.com;
client_max_body_size 25m;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
If your framework builds absolute URLs, it needs X-Forwarded-Proto or it will emit http:// links on an https:// site. Then run certbot with the nginx plugin and let it manage renewal.
6. Enable it, then reboot. sudo systemctl enable --now myapp and then actually reboot the box. A service that starts by hand and not on boot is almost always a missing enable, and you will not find out until a kernel update reboots you at an inconvenient hour.
7. Deploy into a new directory, not into the live venv. The next wall after the reboot is the redeploy: running pip install into the venv that gunicorn is currently using, mid-request, and getting a half-written package or an import error that only clears after a restart. Use releases and a symlink:
/srv/myapp/
releases/20240115T101500/{venv,app}
releases/20240116T090000/{venv,app}
current -> releases/20240116T090000
Build the new venv, rsync the code in, flip the symlink, systemctl restart myapp. If it fails, flip back. This costs you one script and removes an entire class of "it broke on deploy and I don't know why".
8. Watch the logs from the right place. journalctl -u myapp -f gets you stdout from gunicorn because of the --access-logfile - flags above. nginx errors are in /var/log/nginx/error.log. A 502 in the browser means nginx could not reach the upstream — check systemctl status myapp first, not your application code.
Where this falls short
A single VPS is a single point of failure. A restart drops in-flight requests, so your deploys are not zero-downtime unless you run two units behind nginx and drain them, which is more machinery than most small apps justify. Accept the two-second blip or move to a platform that does rolling deploys for you.
You also inherit the whole sysadmin surface: unattended security upgrades, TLS renewal, log rotation, disk space, and monitoring that actually pages you. None of that is Python work, and all of it is now yours. Add a background worker (Celery, RQ, arq) and it needs its own unit file — forget that and your jobs silently never run while the web app looks perfectly healthy.
Do something else entirely if any of these are true: more than two or three people deploy this app, you need autoscaling or multiple regions, you have several services that need to talk to each other, or you find yourself debugging the server more than the code. At that point a PaaS is cheaper than your time, and if you genuinely need reproducible system libraries, a Dockerfile is the honest answer rather than a defeat.
FAQ
Why does my Python app return 502 Bad Gateway after a reboot?
nginx started but the upstream did not. Check systemctl status myapp — the usual causes are a unit that was never enabled, an ExecStart path that does not exist, or a venv that is missing the server package because you installed into a different one. journalctl -u myapp -n 50 will name it.
How do I keep a Python app running after I log out of SSH?
Do not use nohup or screen. Write a systemd unit and run systemctl enable --now. That gives you start-on-boot, automatic restart on crash, and logs in the journal, none of which nohup provides.
Do I need nginx if gunicorn is already serving the app?
Not strictly, but you want it. gunicorn is not built to terminate TLS, serve static files efficiently, or handle slow clients, and running it on port 80 as root is a bad idea. nginx handles those and proxies to gunicorn on loopback.
How do I handle secrets without Docker?
Put them in an EnvironmentFile outside the repo, owned by the service user, mode 600, and reference it from the unit. Do not commit a .env and do not bake secrets into the unit file itself, since unit files are world-readable.
Related reading: How to Set Up a Reverse Proxy with Caddy on a Linux VPS, The Cheapest Way to Run a Node.js API in Production
Bottom line
Run it under systemd with a per-release virtualenv and nginx in front, on the cheapest VPS that fits your memory footprint. It is unglamorous, it has no vendor in the middle, and once the unit file and the release symlink exist, deploys become a script you stop thinking about.
The first concrete step today: create the service user and the venv, and get gunicorn answering on 127.0.0.1:8000 under systemctl start. Do not touch nginx until that works, and do not consider it done until you have rebooted the box and watched it come back on its own.