Learn how to run a FastAPI application on Rocky Linux 10 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 Rocky Linux 10, 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 Ubuntu 24.04 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 Ubuntu 24.04 using Uvicorn and Nginx.
Prepare the Rocky Linux 10 Server
We begin by updating the system packages.
sudo dnf update -y
Install required system packages.
sudo dnf install python3 python3-pip python3-virtualenv nginx git curl -y
Confirm Python installation.
python3 --version
Rocky Linux 10 typically includes Python 3.12, which is fully compatible with FastAPI.
Create a Dedicated Application User
Running applications under a separate user improves isolation and security.
sudo adduser fastapi
Switch to the new user.
su - fastapi
Create the Application Directory
Create a working directory for the project.
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 includes performance optimizations such as uvloop and httptools.
Create a Sample FastAPI Application
Create the main application file.
nano main.py
Example FastAPI application:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "FastAPI application is running"}
Save and exit the editor.
Test the Application with Uvicorn
Add port 8000 in firewall
sudo firewall-cmd --add-port=8000/tcp --permanent
sudo firewall-cmd --reload
Before configuring services, confirm the application works.
uvicorn main:app --host 0.0.0.0 --port 8000
Open a browser and access:
http://server-ip:8000
FastAPI automatically provides API documentation:
http://server-ip:8000/docs
Stop the server once testing is complete.
CTRL + C
Remove port 8000 from firewall
sudo firewall-cmd --permanent --remove-port=8000/tcp
sudo firewall-cmd --reload
Configure SELinux (If enabled)
Execute following two command:
sudo chcon -R -t bin_t /home/fastapi/fastapi-app/venv/bin
sudo setsebool -P httpd_can_network_connect 1
Note: If your fastapi path is different that replace /home/fastapi/fastapi-app with your path
Run FastAPI as a systemd Service
Production applications should run as system services.
Switch back to the root user.
exit
Create the service configuration.
sudo nano /etc/systemd/system/fastapi.service
Add the following configuration.
[Unit]
Description=FastAPI Application
After=network.target
[Service]
User=fastapi
Group=nginx
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
Check service status.
sudo systemctl status fastapi
Install and Configure Nginx
Enable and start the Nginx service.
sudo systemctl enable nginx
sudo systemctl start nginx
Create a new Nginx configuration.
sudo nano /etc/nginx/conf.d/fastapi.conf
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;
}
}
Test the configuration.
sudo nginx -t
Restart Nginx.
sudo systemctl restart nginx
Nginx will now forward incoming traffic to the FastAPI application.
Configure Firewall
Rocky Linux 10 typically uses firewalld.
Allow HTTP and HTTPS traffic.
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Verify firewall configuration.
sudo firewall-cmd --list-all
Enable HTTPS with Let's Encrypt
Install Certbot for SSL certificates.
sudo dnf install certbot python3-certbot-nginx -y
Request an SSL certificate.
sudo certbot --nginx -d example.com
Test certificate renewal.
sudo certbot renew --dry-run
Certificates renew automatically every 90 days.
Improve Performance with Multiple Workers
For production workloads, multiple workers improve concurrency.
Edit 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 common rule for worker configuration is:
workers = (2 × CPU cores) + 1
Monitoring and Logs
Logs help identify errors and performance issues.
View application logs.
sudo journalctl -u fastapi -f
Check Nginx logs.
/var/log/nginx/access.log
/var/log/nginx/error.log
Monitoring logs regularly helps maintain stable production systems.
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 Rocky Linux 10 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.

