Back to Blog
fastapi
deployment
python
railway
dokku
aws
vps
web-dev

How to Deploy a FastAPI App

Step-by-step guide to deploy your FastAPI app using Railway, Dokku on a VPS, or AWS EC2 — with real examples and pro tips.

Niklas L.
7 min read

How to Deploy a FastAPI App (Railway, Dokku, AWS EC2)

Once you’ve finished building your FastAPI app and tested it locally, the next big step is getting it online so others can use it. Deployment can seem a little overwhelming at first, especially if you're deciding between different hosting options, but it doesn’t have to be.

In this guide, I’ll walk you through how to deploy a FastAPI application using three different platforms. Each option suits a slightly different use case, whether you're experimenting with a personal project or deploying something more production-ready.

We’ll cover:

Let’s start from the beginning by setting up a basic FastAPI app, and then we’ll move through the deployment options one by one.


Step 1: Create Your FastAPI App

If you haven’t created your FastAPI app yet, here’s how to start from scratch with a simple setup.

Create a new project folder and set up a virtual environment:

mkdir fastapi-app && cd fastapi-app
python -m venv venv
source venv/bin/activate
pip install fastapi "uvicorn[standard]"
pip freeze > requirements.txt

Next, create a file called main.py with a basic route:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello from FastAPI"}

To test it locally, run:

uvicorn main:app --reload

You should see output in your terminal showing the app is running. You can visit http://127.0.0.1:8000 in your browser to confirm it's working.

If you're looking to skip the setup entirely and want a starter project that includes things like user authentication, testing, Docker, and structure for a larger app, take a look at FastLaunchAPI.dev. It's a premium FastAPI starter template built for production use.


Step 2: Push the Project to GitHub

Before you can deploy the app, you’ll need to push it to a GitHub repository. Most platforms pull your code directly from there during deployment.

Here’s how to do it:

git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/yourusername/your-repo.git
git push -u origin main

Once the code is on GitHub, you're ready to pick a deployment option.


Option 1: Deploy FastAPI Using Railway

Railway is one of the easiest platforms for deploying apps, especially if you're just getting started or want to move quickly. You don’t have to deal with servers, SSH, or Docker unless you want to. Railway handles most of the setup automatically.

Deploy Using GitHub

  1. Head over to Railway and sign in or create an account.
  2. Click “New Project,” then choose “Deploy from GitHub.”
  3. Select the repository where your FastAPI app lives.
  4. Railway will detect your Python project and handle the build and deploy process.

After deployment, you can go to the project settings and generate a public domain so the app is accessible online.

Deploy Using the Railway CLI (Optional)

If you prefer working from the command line, Railway also offers a CLI tool:

npm install -g railway
railway login
railway init
railway up

This will link your project to Railway and deploy it in just a few commands.


Option 2: Deploy FastAPI on a VPS Using Dokku

If you want more control over your hosting environment but don’t want the complexity of configuring everything manually, Dokku is a solid middle-ground. It’s often called the “poor man's Heroku” because it offers similar features but runs on your own server.

Set Up Dokku on Your VPS

Start with a clean Ubuntu server. You can get one from providers like DigitalOcean or Hetzner. Then, install Dokku using the following commands:

wget https://raw.githubusercontent.com/dokku/dokku/v0.30.6/bootstrap.sh
sudo DOKKU_TAG=v0.30.6 bash bootstrap.sh

During setup, you’ll be asked to provide an SSH key and optionally configure a domain.

Prepare Your FastAPI App

Dokku expects a few files in your project to know how to run it:

  1. Make sure your requirements.txt includes FastAPI and Uvicorn.
  2. Create a Procfile with this line:
web: uvicorn main:app --host=0.0.0.0 --port=${PORT}

Then, push your app to the Dokku server like this:

dokku apps:create fastapi-app
dokku buildpacks:add fastapi-app https://github.com/heroku/heroku-buildpack-python
git remote add dokku dokku@your-vps-ip:fastapi-app
git push dokku main

After the push, Dokku will build and start your app. If you’ve configured a domain, it’ll be live there. Otherwise, you can access it via the server’s public IP.


Option 3: Deploy FastAPI on AWS EC2

This option is for those who want full control over their server, perhaps to install custom dependencies, integrate with other AWS services, or fine-tune everything from networking to logging. It's more setup than Railway or Dokku, but it's also more flexible.

Launch and Connect to an EC2 Instance

  1. Go to the AWS EC2 dashboard and launch a new instance, choosing Ubuntu as your operating system.
  2. Make sure to open port 8000 in your security group settings.
  3. Connect to the server via SSH:
ssh -i your-key.pem ubuntu@your-ec2-ip

Install Dependencies and Run Your App

Once you're on the server, set up Python and your app:

sudo apt update && sudo apt install python3-pip python3-venv -y
git clone https://github.com/yourusername/your-repo.git
cd your-repo
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
uvicorn main:app --host 0.0.0.0 --port 8000

Visit http://your-ec2-ip:8000 in your browser, and you should see your app running.

For production use, you'll likely want to set up a process manager like gunicorn or supervisord, and use a reverse proxy such as Nginx to handle SSL and serve requests on port 80 or 443.


Frequently Asked Questions

What’s the fastest way to deploy a FastAPI app? Railway is the quickest and easiest way. It requires almost no setup and gets your app online in minutes.

Can I deploy FastAPI on a VPS for cheap? Yes. Using Dokku on a basic VPS (like a $5/month DigitalOcean droplet) gives you full control without much overhead.

Do I need Docker to deploy FastAPI? Not always. Railway and Dokku can both deploy Python apps without Docker. However, Docker is useful if you're building complex setups or need environment consistency across development and production.

Is EC2 better for large projects? EC2 is great when you need maximum flexibility. It’s a good fit for production workloads that require fine-grained control over networking, deployment pipelines, or security configurations.

Any starter templates available? Yes. FastLaunchAPI.dev offers a complete FastAPI starter with authentication, structure, testing, Docker support, and more. It’s built to help you skip repetitive setup and focus on your actual product.


Final Thoughts

There’s no one-size-fits-all way to deploy a FastAPI app. It really depends on what you're building and how much control you want.

If you're looking for something quick and hands-off, Railway is an excellent starting point. If you want your own server but prefer not to set everything up manually, Dokku on a VPS gives you a nice balance. And if you're building a serious production system, AWS EC2 offers the most flexibility.

No matter which route you take, once your FastAPI app is online, you're ready to start building real features and serving real users.

If you want to save time on setup and structure, don’t forget to check out FastLaunchAPI.dev. It’s a great option if you're starting something big and want to do it right from day one.


Related Articles