Deploy the FastAPI app on Debian with Nginx, Uvicorn, and Systemd.

Ashfaque Ali
3 min readMay 15

--

Last week, I was exploring the options to deploy the FastAPI project on the VPS, and I found that it’s super easy to deploy the FastAPI app.

I will share a quick step-by-step guide that will cover the following :

  • A domain configured with Nginx reverse proxy
  • FastAPI app inside a virtual environment
  • And also a background service that will ensure the app is always running even after a reboot.

Let’s start with Nginx. We will create a site-available config file for our app domain.

sudo nano /etc/nginx/sites-available/myappdomain.com
server {
listen 80;
server_name www.myappdomain.com myappdomain.com;

location / {
proxy_pass http://127.0.0.1:8000; # should match with the fastapi port
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

To enable the domain/site, we need to create a symbolic link to the site-available config file in the site-enabled directory of Nginx.

sudo ln -s /etc/nginx/sites-available/myappdomain.com /etc/nginx/sites-enabled/

Finally, restart the Nginx server.

sudo systemctl restart nginx

We are done with the Nginx config.

Let’s configure the FastAPI app in the virtual environment.

Make sure our VPS is up to date and the required dependencies are installed (you can ignore if Python is already installed).

sudo apt update
sudo apt install python3-pip python3-dev
sudo apt install python3-venv

Let’s create a virtual environment for our app at the chosen path where your app files will be located:

/var/www/python/myappdomain.com

cd /var/www/python/myappdomain/
python3 -m venv venv

Our virtual environment is ready. Now, let’s activate it. Please ensure that you are on the same app path.

source venv/bin/activate

--

--

Ashfaque Ali

Software Engineering Manager