Skip to content

Deploy a Flask App

How to Deploy a Flask App on a VPS, Cloud Server, or Dedicated Server

In this guide, I will show how to deploy a Flask application on a VPS, cloud, or dedicated server using Docker and Caddy. Whether you’re running your application on DigitalOcean, AWS, Hetzner, Scaleway or own hardware, the deployment steps will be the same.

I have prepared a simple Flask application along with a Dockerfile that you can use as a reference. The project is available on GitHub.

Setting Up the Server

Start a new server on your cloud provider with a public IP address, SSH access from your development machine, and a fresh Ubuntu 22.04 installation. Ensure that ports 22, 80, and 443 are open. (Some cloud providers, like AWS, close ports by default; you’ll need to open them before deployment.)

If you’re looking for a new cloud provider, consider DigitalOcean, Hetzner, or OVH. These providers typically have ports open by default. OVH also offers affordable domain registration and free DNS management via API.

Deploy with One Command

If you don’t want to deploy manually or don’t have time to set up a deployment pipeline, you can deploy your Flask application to any cloud provider or private server with a single command from your local repository using TurboCloud deployment toolkit. (No need for GitHub, Bitbucket, or even Git!) You can also deploy directly from GitHub or Bitbucket repositories.

Prepare Your Flask App for Deployment

If you deploy with TurboCloud, it will check for the presence of a Dockerfile in the project's root directory. If no Dockerfile is found, TurboCloud will attempt to generate one automatically using Nixpacks. (See here for details on how Nixpacks generates Dockerfiles for Flask apps.) However, we recommend creating a Dockerfile manually and placing it in the project's root folder for better control:

  • Create a file named Dockerfile in your project’s root directory.
  • Add the following content (replace CMD ["src/main.py"] with a path to your main file):
FROM python:3.11-alpine
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["src/main.py"]

Deploy from the Local Repository

  • On your development machine, navigate to the folder containing the project's code and run the deployment command (replace server_ip with the actual public IP of your server and service_port with the port number your app uses):
cd my_project
curl https://turbocloud.dev/deploy | bash -s -- -i server_ip -p service_port
  • Wait until the deployment script displays the URL of the deployed Flask application.
  • Full documentation for all available parameters and detailed instructions can be found at turbocloud.dev/docs.

Deploy from GitHub or Bitbucket

  • SSH into your server (replace server_ip with the actual public IP of your server):
ssh root@server_ip
  • Run the setup command:
curl https://turbocloud.dev/setup | bash -s
  • Once installation is complete, start the TurboCloud TUI (Terminal User Interface), which functions similarly to standard applications on macOS, Linux, and other operating systems but can operate on servers without displays. Use the TUI to set up deployments with interactive guides:
turbocloud

Step-by-Step guide

Prerequisites

Before proceeding, ensure you have the following:

  1. A VPS or cloud server: You can use any cloud provider however we recommend to use Hetzner, DigitalOcean, OVH or Vultr.
  2. Domain Name: If you havn't a domain name yet you can purchase it at OVH, for example.
  3. Installed Tools on your development machine:
  4. Docker and Docker CLI
  5. scp command for uploading files

Step 1: Write a Simple Flask Application

  • Create a file main.py in your project directory with the following content
# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask on a VPS!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
  • Create a file requirements.txt in your project directory with the following content
flask==2.3.2

Step 2: Create a Dockerfile

The Dockerfile is used to containerize your application. In the same directory as app.py, create a file named Dockerfile:

FROM python:3.11-alpine
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["main.py"]

This Dockerfile specifies the base Python image, installs Flask, and runs the app.

Step 3: Build and Test the Docker Image Locally

Build the Docker image locally to ensure it works before deploying to the VPS (Docker should be installed on your local machine):

docker build -t flask-app .

Run the container:

docker run -d -p 5000:5000 flask-app

Open http://localhost:5000 in your browser to verify that the application is running.


Step 4: Upload Files to the VPS

Use scp to upload your project files to the VPS:

scp app.py Dockerfile user@your-server-ip:/home/user/flask-app

Replace user with your server username (in most cases it will be root) and your-server-ip with your server’s IP address. Log in to your VPS to confirm that the files are uploaded:

ssh user@your-server-ip
ls /home/user/flask-app

Step 5: Install Docker on the server

If Docker is not already installed, use the following commands to install it:

sudo apt update
sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker

Verify Docker is running:

docker --version

Step 6: Build and Run the Docker Container on the server

Navigate to the project directory and build the Docker image:

cd /home/user/flask-app
sudo docker build -t flask-app .

Run the container:

sudo docker run -d -p 5000:5000 flask-app

At this point, your Flask app should be accessible at http://your-server-ip:5000.

Step 7: Install and Configure Caddy Server

Caddy Server is a fast web server that provides automatic HTTPS. Install Caddy on your server:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install -y caddy

Once installed, verify the Caddy version:

caddy version

Configure Caddy for Your Domain

Edit the Caddy configuration file to serve your Flask app. Create a file /etc/caddy/Caddyfile:

example.com {
    reverse_proxy localhost:5000
}

Replace example.com with your domain. Reload Caddy to apply the changes:

sudo systemctl reload caddy

Step 8: Point Your Domain to the server

Log in to your domain registrar and update the DNS settings to point your domain to the VPS’s IP address. Add an A Record:

  • Host: @
  • Value: Your server's IP address
  • TTL: Default

Wait for DNS propagation, which may take a few minutes to hours.


Step 9: Test Your Deployment

Open a browser and navigate to your domain (e.g., https://example.com). You should see your Flask application served securely with HTTPS.

Step 10: Remove Unnecessary Files

Clean up any files you uploaded during deployment:

rm -rf /home/user/flask-app

Conclusion

In this guide, we’ve covered two options for deploying Flask applications:

  1. Using the TurboCloud deployment toolkit with a single command. This option also includes CI/CD, multiple environments, a Web Application Firewall (WAF), a Rate Limiter, and VPN functionality across servers and local machines.
  2. Deploying manually with Docker, Docker Compose, and Caddy. To prepare your server for production, we recommend adding a Web Application Firewall (WAF) and a Rate Limiter.