How to Host Flask Applications on a VPS

Have you built a Flask app and wondered how to share it with the world?

You’re not alone. When I first built something cool with Flask, it just ran on my laptop. That was fine for testing. But what if I wanted to show it to others? That’s when I learned about hosting Flask on a VPS.

In this post, I’ll guide you through the steps to host your Flask app on a VPS, or Virtual Private Server.


What’s a VPS?

Let’s start with the basics.

A VPS is a type of server you can rent. It’s like your own computer on the internet. You can run websites, apps, or even host files.

It’s “virtual” because the physical machine is shared with others, but you get your own slice.

Some popular VPS providers are:

  • DigitalOcean
  • Vultr
  • Linode

I usually use Ubuntu on my VPS because it’s stable and has lots of helpful guides. But if you use another Linux version like Debian or CentOS, the steps will be similar.


What’s Flask?

Flask is a Python web framework. It helps you build websites and APIs using Python.

It’s called a “micro” framework because it’s lightweight and doesn’t come with too many built-in tools. You can add only what you need.

If you’ve built a simple app with Flask, and now want to share it online, read on.


What You’ll Need

Before we begin, make sure you have:

  • A Flask app that works locally
  • A VPS with Ubuntu 20.04 or 22.04
  • Root or sudo access to your VPS
  • Basic knowledge of Python and Linux
  • A domain name (optional, but helpful)

Step 1: Connect to Your VPS

Use SSH to log into your VPS:

ssh username@your_vps_ip

Replace username with root or your user, and your_vps_ip with your server’s IP.


Step 2: Update and Install Packages

Always start by updating your system:

sudo apt update
sudo apt upgrade

Then install some needed tools:

sudo apt install python3-pip python3-venv nginx git

This installs:

  • pip: Python package manager
  • venv: lets you create virtual environments
  • nginx: a web server
  • git: version control (optional)

Step 3: Upload Your Flask App

You can send your Flask app to the server in a few ways:

  • Use scp (secure copy)
  • Use git to clone from GitHub
  • Upload with an FTP tool like FileZilla

Let’s say your app is in a folder called myflaskapp.

Move it to /var/www/:

sudo mv myflaskapp /var/www/
cd /var/www/myflaskapp

Step 4: Set Up a Python Virtual Environment

You don’t want to install Python packages globally. Use a virtual environment.

In your app directory:

python3 -m venv venv
source venv/bin/activate

Then install your app’s dependencies:

pip install -r requirements.txt

Make sure your requirements.txt file includes Flask and anything else your app needs.


Step 5: Test the App with Gunicorn

Gunicorn is a Python WSGI server. It helps run Flask apps in production.

Install it:

pip install gunicorn

Now run your app:

gunicorn -w 4 -b 127.0.0.1:8000 app:app

Let me break that down:

  • -w 4: uses 4 worker processes
  • -b 127.0.0.1:8000: binds to port 8000 on localhost
  • app:app: the file is app.py and the Flask object is also called app

If that runs without errors, you’re almost there.


Step 6: Set Up a Systemd Service

You want your Flask app to keep running, even after a reboot. That’s what systemd does.

Create a file:

sudo nano /etc/systemd/system/myflaskapp.service

Paste this:

[Unit]
Description=Gunicorn for Flask app
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/myflaskapp
ExecStart=/var/www/myflaskapp/venv/bin/gunicorn -w 4 -b 127.0.0.1:8000 app:app

[Install]
WantedBy=multi-user.target

Save and exit.

Now enable and start the service:

sudo systemctl start myflaskapp
sudo systemctl enable myflaskapp

Check it:

sudo systemctl status myflaskapp

Step 7: Configure Nginx as a Reverse Proxy

You don’t want users to visit port 8000. Nginx will forward traffic from port 80 to your app.

Create a new config file:

sudo nano /etc/nginx/sites-available/myflaskapp

Add this:

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Link it:

sudo ln -s /etc/nginx/sites-available/myflaskapp /etc/nginx/sites-enabled/

Test and restart:

sudo nginx -t
sudo systemctl restart nginx

Now if you visit your VPS IP (or domain), you should see your Flask app.


Optional: Add SSL with Certbot

For HTTPS, use Certbot:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com

This gets a free SSL certificate and updates your Nginx config.


Two Handy Lists

Common Problems and Fixes:

  • App crashes? Check journalctl -u myflaskapp
  • Page not loading? Make sure Gunicorn is running
  • 502 error? Nginx can’t reach the app, check ports
  • Certbot failed? Domain might not point to your server

Tips to Make Hosting Easier:

  • Always use a virtual environment
  • Use systemd to auto-start your app
  • Nginx is great as a reverse proxy
  • Keep your dependencies in requirements.txt

Final Thoughts

Hosting a Flask app on a VPS might seem scary the first time. I was nervous, too. But once I got it running, it felt great.

The nice part? You’re in control. You can update your app, restart it, or even host more apps on the same server.

How did your setup go? Got stuck at any step?

Leave a Reply