Learn how to run a FastAPI application on Debian 13 using Uvicorn and Nginx.
Introduction
FastAPI has quickly become one of the most popular Python frameworks for building modern APIs. It is fast, lightweight, and designed for high performance. When combined with Uvicorn and Nginx, FastAPI applications can run reliably in production environments with excellent scalability.
In this guide, we will walk through the complete process of deploying a FastAPI application on Debian 13, using Uvicorn as the ASGI server and Nginx as a reverse proxy. The steps follow a production-ready approach that developers commonly use when deploying APIs on VPS or dedicated servers.
This tutorial covers everything from installing dependencies to configuring system services, enabling HTTPS, and improving security.
Prerequisites
Before we begin, let’s ensure we have the following in place:
- A Debian 13 dedicated server or KVM VPS.
- A basic programming knowledge.
- A domain name pointing A record to server IP.
How to run a FastAPI application on Debian 13 using Uvicorn and Nginx.
Prepare the Debian 13 Server
We begin by updating the server and installing essential packages.
sudo apt update && sudo apt upgrade -y
Install Python tools and required utilities.
sudo apt install python3 python3-pip python3-venv nginx git curl -y
Confirm Python installation.
python3 --version
Debian 13 typically includes Python 3.12, which works well with FastAPI.
Create a Dedicated Application User
Running applications under a separate user improves security and isolates services.
sudo adduser fastapi
Grant sudo access if administrative privileges are required.
sudo usermod -aG sudo fastapi
Switch to the new user.
su - fastapi
Create the Application Directory
Organizing project files properly prevents confusion as applications grow.
mkdir fastapi-app
cd fastapi-app
Create a Python virtual environment.
python3 -m venv venv
Activate the environment.
source venv/bin/activate
Upgrade pip.
pip install --upgrade pip
Install FastAPI and Uvicorn
Install the required Python packages.
pip install fastapi uvicorn[standard]
The uvicorn[standard] package installs performance improvements such as uvloop and httptools.
Create a Sample FastAPI Application
Create the application file.
nano main.py
Example application:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "FastAPI is running successfully"}
Save and exit.
Test the Application with Uvicorn
Before configuring production services, we verify that the application works correctly.
Open port UFW firewall
sudo ufw allow 8000/tcp
Now execute following command to start server
uvicorn main:app --host 0.0.0.0 --port 8000
Open the browser and visit:
http://server-ip:8000
FastAPI automatically provides interactive documentation:
http://server-ip:8000/docs
Once the application responds correctly, stop the server.
CTRL + C
Remove 8000 port from firewall
sudo ufw delete allow 8000/tcp
Run FastAPI as a System Service
Running Uvicorn manually is not suitable for production. Instead, we create a systemd service to manage the application.
Switch back to the root user.
exit
Create the service file.
sudo nano /etc/systemd/system/fastapi.service
Add the following configuration.
[Unit]
Description=FastAPI Application
After=network.target
[Service]
User=fastapi
Group=www-data
WorkingDirectory=/home/fastapi/fastapi-app
Environment="PATH=/home/fastapi/fastapi-app/venv/bin"
ExecStart=/home/fastapi/fastapi-app/venv/bin/uvicorn main:app --host 127.0.0.1 --port 8000
Restart=always
[Install]
WantedBy=multi-user.target
Reload system services.
sudo systemctl daemon-reload
Start the application.
sudo systemctl start fastapi
Enable automatic startup.
sudo systemctl enable fastapi
Verify the service.
sudo systemctl status fastapi
If everything is configured correctly, the application will run in the background.
Configure Nginx as Reverse Proxy
Nginx improves performance, handles client connections efficiently, and protects the backend service.
Create a new configuration file.
sudo nano /etc/nginx/sites-available/fastapi
Example configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Enable the configuration.
sudo ln -s /etc/nginx/sites-available/fastapi /etc/nginx/sites-enabled/
Test the configuration.
sudo nginx -t
Restart Nginx.
sudo systemctl restart nginx
Visit the domain in a browser. Nginx now forwards traffic to the FastAPI application.
Configure Firewall
Debian servers typically use UFW.
Allow HTTP and HTTPS traffic.
sudo ufw allow 'Nginx Full'
sudo ufw enable
Check firewall status.
sudo ufw status
Enable HTTPS with Let's Encrypt
Secure connections are essential for production environments.
Install Certbot.
sudo apt install certbot python3-certbot-nginx -y
Generate the SSL certificate.
sudo certbot --nginx -d example.com
Certbot automatically updates the Nginx configuration and installs the SSL certificate.
Test renewal.
sudo certbot renew --dry-run
Certificates renew automatically every 90 days.
Improve Performance with Multiple Workers
For production environments, running multiple workers improves concurrency.
Modify the service file.
sudo nano /etc/systemd/system/fastapi.service
Update the startup command.
ExecStart=/home/fastapi/fastapi-app/venv/bin/uvicorn main:app \
--host 127.0.0.1 \
--port 8000 \
--workers 4
Reload services.
sudo systemctl daemon-reload
sudo systemctl restart fastapi
A good rule is:
workers = (2 x CPU cores) + 1
Logging and Monitoring
Application logs help identify issues quickly.
Check service logs.
sudo journalctl -u fastapi -f
Check Nginx logs.
/var/log/nginx/access.log
/var/log/nginx/error.log
Monitoring logs regularly improves reliability and stability.
Best Practices for Production Deployment
Production environments benefit from several additional improvements.
Use separate environment files for secrets such as API keys and database credentials.
Use Gunicorn with Uvicorn workers for high-traffic APIs.
Implement rate limiting and security headers in Nginx.
Regularly update system packages.
Schedule automatic backups of application code and databases.
Final Thoughts
Deploying FastAPI with Uvicorn and Nginx on Debian 13 provides a stable and scalable environment for modern APIs. Uvicorn handles asynchronous Python requests efficiently, while Nginx manages client connections and improves overall performance.
With proper service management, SSL encryption, and monitoring in place, this architecture is suitable for both small applications and large production systems.
This setup is widely used across modern cloud environments and provides a reliable foundation for building and deploying high-performance Python APIs.

