Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

Deploy FastAPI on a server using Disco

profile picture
Author
Greg Sadetsky
Published
2025-04-30
Time to read
7 minutes reading time

About the author- Greg finds joy in the absurd.

Introduction

In this tutorial, we'll deploy a FastAPI application on a server using Disco, an open-source web deployment platform. FastAPI is a modern, high-performance web framework for building APIs with Python, and Disco simplifies the deployment process by handling infrastructure setup, SSL certificates, and continuous deployment.

By the end of this tutorial, you'll have a working FastAPI application deployed on your own server with automatic HTTPS and continuous deployment from GitHub. This setup gives you both the flexibility of self-hosting and the convenience of platform-as-a-service solutions.

Prerequisites

  • A server with Ubuntu 24.04 and at least 4GB RAM
  • A domain name you can configure
  • Basic knowledge of Python and command-line operations
  • Git on your local machine and a GitHub account

Example terminology

  • Domain: example.com
  • Subdomain for server: server.example.com
  • Subdomain for application: fastapi.example.com
  • IP address: <10.0.0.1>

Step 1 - Create a server

First, we need to create a server to host our application (e.g. with Hetzner Cloud).

  • Choose a data center region close to your target audience
  • Choose "Ubuntu 24.04" as the operating system
  • Select a server plan with at least 4GB RAM (with Hetzner Cloud CPX21 or higher recommended)
  • Add your SSH key for secure access (or create a new one)
  • Give your server a name (e.g., fastapi-server)

Once your server is created, note down its IP address. You'll need it for the next steps.

Make sure that you're able to SSH into your server by running:

ssh root@<10.0.0.1>
# replace the IP above with your server's IP

If this is your first time connecting, you may be prompted to accept the server's fingerprint. Type yes and press Enter. Once you've verified that you can SSH into your server, exit the SSH session:

exit

Step 2 - Configure DNS Settings

Before proceeding, we need to set up two domain names:

  1. A domain for your Disco server (e.g., server.example.com)
  2. A domain for your FastAPI application (e.g., fastapi.example.com)

Go to your domain registrar's DNS management panel and add these records:

For the server domain:

Type: A
Name: server
Value: <10.0.0.1> (replace with your server's IP address)
TTL: 300 (or default)

For the application domain:

Type: CNAME
Name: fastapi
Value: server.example.com (replace with your own domain)
TTL: 300 (or default)

DNS changes can take up to 24 hours to propagate, but often take just a few minutes. You can verify if the DNS is properly set up using:

ping server.example.com
dig server.example.com

If this returns responses from your server's IP address, the DNS is configured correctly.

Step 3 - Install the Disco CLI

The Disco CLI is a command-line tool that helps you manage your deployments. Install it on your local machine with:

curl https://cli-assets.letsdisco.dev/install.sh | sh

This script will download and install the Disco CLI. After installation, verify it's working:

disco --version

You should see the version number of the installed Disco CLI.

Step 4 - Initialize Your Server with Disco

Now let's set up Disco on your server by running this command on your local machine:

disco init root@server.example.com  --identity-file=/home/<your_user>/.ssh/id_ed25519

This command will:

  • Connect to your server using SSH
  • Install Docker
  • Set up the Disco server
  • Configure the initial SSL certificate

You'll see a progress bar during the initialization process. Once it's complete, you'll see the word "Done". Your server is now ready to host applications.

Step 5 - Create a FastAPI Application

Let's create a simple FastAPI application to deploy. First, create a new GitHub repository:

  1. Go to GitHub and create a new repository (e.g., fastapi-demo)

  2. Clone the repository to your local machine:

    git clone https://github.com/yourusername/fastapi-demo.git
    cd fastapi-demo
  3. Create the following files in the repository:

    main.py - This is our FastAPI application:

    from fastapi import FastAPI
    
    app = FastAPI()
    
    @app.get("/")
    def read_root():
        return {"message": "Hello from the server deployed with Disco!"}
    
    @app.get("/items/{item_id}")
    def read_item(item_id: int):
        return {"item_id": item_id, "name": f"Test Item {item_id}"}

    requirements.txt - Dependencies for our application:

    fastapi>=0.95.0
    uvicorn>=0.21.1

    Dockerfile - Instructions to build our application container:

    FROM python:3.11-slim
    
    WORKDIR /app
    
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt
    
    COPY . .
    
    CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

    disco.json - Configuration for Disco:

    {
        "version": "1.0",
        "services": {
            "web": {
                "port": 8000
            }
        }
    }
  4. Commit and push your changes:

    git add .
    git commit -m "Initial commit"
    git push

Step 6 - Connect Disco to GitHub

To allow Disco to deploy your application from GitHub, you need to connect your GitHub account:

disco github:apps:add

This command will open a browser window where you can authorize Disco with GitHub. You'll need to:

  1. Name the GitHub application (any name will do)
  2. Select the repository you just created
  3. Click "Install"

Step 7 - Deploy Your FastAPI Application

Now we can deploy our FastAPI application using the Disco CLI on our local machine:

disco projects:add \
  --name fastapi-demo \
  --github yourusername/fastapi-demo \
  --domain fastapi.example.com

Replace:

  • yourusername/fastapi-demo with your GitHub username and repository name
  • fastapi.example.com with your application domain

Disco will automatically:

  • Pull your code from GitHub
  • Build the Docker container
  • Deploy it to your server
  • Set up HTTPS with Let's Encrypt
  • Configure domain routing

The deployment process should take less than a minute to complete. You'll see output showing the progress.

Step 8 - Test Your Deployed Application

Once the deployment is complete, you can test your FastAPI application:

  1. Open a web browser and navigate to:

    https://fastapi.example.com

    You should see the JSON response:

    {"message": "Hello from the server deployed with Disco!"}

  1. You can also test the items endpoint:

    https://fastapi.example.com/items/42

    Which should return:

    {"item_id": 42, "name": "Test Item 42"}

Step 9 - Making Changes and Automatic Deployment

One of the benefits of using Disco is automatic deployment when you push changes to GitHub. Let's test this:

  1. Modify read_root in main.py:

    @app.get("/")
    def read_root():
        return {"message": "This is a completely different message!"}
  2. Commit and push your changes:

    git add main.py
    git commit -m "Update greeting message"
    git push
  3. Within seconds, Disco will detect the changes, rebuild your application, and deploy it automatically.

  4. Refresh your browser at https://fastapi.example.com to see the updated message.

Conclusion

Congratulations! You've successfully deployed a FastAPI application on a server using Disco. Your setup now includes:

  • A fully managed deployment pipeline
  • Automatic HTTPS setup with Let's Encrypt
  • Fast deployments triggered by Git pushes
  • Complete control over your server and application

This deployment method gives you the best of both worlds: the control and cost-effectiveness of self-hosting with the convenience of a platform-as-a-service solution. It's perfect for personal projects, small businesses, or any application where you want to maintain ownership of your infrastructure.

For more advanced configurations and features, check out the Disco documentation and the FastAPI documentation.

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue
Try Hetzner Cloud

Get €20/$20 free credit!

Valid until: 31 December 2025 Valid for: 3 months and only for new customers
Get started
Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more