Deploy a Flask app on a VPS with Gunicorn

Localhost is a comfy lie

To deploy a Flask app on a VPS, install Python and Nginx, run the app under Gunicorn, keep it alive with a systemd service, and let Nginx face the internet. That’s the whole recipe.

So why four moving parts for something that runs fine with flask run? Well, that built-in server is a development convenience, and Flask’s own docs say plainly it’s meant for local work. Real traffic, a random crash at 3 a.m., a reboot after a system update: none of that is in its job description.

Each tool in the stack plugs one specific hole. Gunicorn spins up several worker processes. systemd drags the app back to life whenever it dies. Nginx takes the slow, messy, unpredictable internet on its chin so the app never has to. Three jobs, three tools, and honestly, less scary than it sounds.

Prepare the server first

Almost any reliable hosting VPS with root access and a fairly recent Ubuntu release will do the job, and a small plan is plenty for a first project. Nobody needs a monster machine just to learn the ropes.

Then comes a short sequence that most newcomers end up repeating on every new server:

  1. Refresh the package lists and upgrade what’s installed: sudo apt update && sudo apt upgrade
  2. Grab the essentials with sudo apt install python3-venv python3-pip nginx
  3. Inside the project folder, create a virtual environment (python3 -m venv venv), so project packages stay out of the system Python
  4. Switch it on with source venv/bin/activate, then run pip install flask gunicorn
  5. Try a first launch: gunicorn –bind 127.0.0.1:8000 app:app

Step five is where people stumble. Read app:app as “file name, then the Flask object inside it”: app.py on the left of the colon, the variable holding the Flask application on the right. Mix those up and Gunicorn complains immediately. Annoying, sure, but at least it’s loud.

One more thing before the site goes public. UFW should allow OpenSSH first, because switching the firewall on without that rule can lock the owner out of their own server (a painful lesson to learn the hard way). Once SSH is safe, the Nginx profile opens ports 80 and 443.

FastAPI plays by slightly different rules, since it’s an ASGI framework and needs an ASGI server such as Uvicorn. The systemd and Nginx parts below look almost the same, though.

Gunicorn: the engine room

Gunicorn follows a pre-fork model: one master process supervises a handful of workers, and the operating system itself balances connections between them, which keeps things pleasantly boring.

The big question is how many workers to start. The official guidance leans on (2 x number of cores) + 1, and the nproc command reveals the core count in a blink. In practice that looks like this:

  • 1 core: 3 workers
  • 2 cores: 5 workers
  • 4 cores: 9 workers

Bear in mind that every worker carries its own copy of the app, so RAM vanishes faster than expected, and small servers often run fewer workers than the formula suggests. That’s fine. Nobody’s grading.

Also worth knowing: Gunicorn’s design notes expect a buffering proxy sitting in front of a default setup. That’s exactly the seat Nginx fills in the next part.

systemd and Nginx: the safety net

Turning the app into a proper service takes a unit file with three sections. [Unit] carries a description and tells systemd to wait for the network. [Service] names the user, the working directory and an ExecStart line that points at the Gunicorn binary living inside the virtual environment. [Install] needs just one line, WantedBy=multi-user.target. After that, sudo systemctl enable –now myapp starts the service right away and registers it for boot as well.

The payoff? A crash no longer means downtime until someone notices. The app restarts itself, survives reboots, and leaves a trail of logs that journalctl can display.

Nginx is the last piece. Its server block listens on port 80 and hands requests to Gunicorn through proxy_pass, to 127.0.0.1:8000 or a Unix socket, whichever the setup prefers. Forwarding the Host, X-Real-IP, X-Forwarded-For and X-Forwarded-Proto headers lets Flask see the real visitor instead of just Nginx. Before reloading, a quick sudo nginx -t catches the typos that would otherwise take the site down.

If a socket throws “permission denied”, the usual suspect is a user mismatch between Nginx and Gunicorn, and aligning the two normally fixes it. HTTPS comes cheap too, since Certbot can pull free Let’s Encrypt certificates in a couple of commands.

Final thoughts

Deployment feels intimidating mostly because of the vocabulary. Strip the jargon off and the pattern is surprisingly tidy: an isolated environment, a worker manager, a service supervisor, a reverse proxy up front. Learn it once, and the next ten apps need only small edits.

A few habits pay off later. Keep dependencies listed in a requirements file. Read the journalctl output before guessing. Change one thing at a time, and test the Nginx config before every reload, no exceptions.

Developers who treat the first deployment as a reusable template usually move noticeably faster afterwards. Happy shipping, and may every restart be a boring one.

Pankaj Kumar
Pankaj Kumar

I have been working on Python programming for more than 12 years. At AskPython, I share my learning on Python with other fellow developers.

Articles: 264