Deploying a FastAPI project on Ubuntu 24
Update System Packages
sudo apt update && sudo apt upgrade -y
Install Python and Pip
sudo apt install python3 python3-pip -y
Install Git (if needed)
sudo apt install git -y
Install Virtual Environment
sudo apt install python3-venv -y
Clone Your FastAPI Project
git clone https://github.com/your-repo/your-fastapi-project.git cd your-fastapi-project
Set Up a Virtual Environment
python3 -m venv venv source venv/bin/activate pip install --upgrade pip pip install -r requirements.txt
Test Your Application Locally. Run the app to ensure it works.
Replace main:app with your application’s entry point.
uvicorn main:app --reload ## Result INFO: Will watch for changes in these directories: ['/folder/folder'] INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [26266] using WatchFiles INFO: Started server process [26268] INFO: Waiting for application startup. INFO: Application startup complete.
Install Gunicorn
pip install gunicorn
Test Gunicorn with Your FastAPI App.
Replace main:app with your application’s entry point.
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app ## Result [2024-12-24 20:03:17 +0000] [26555] [INFO] Starting gunicorn 23.0.0 [2024-12-24 20:03:17 +0000] [26555] [INFO] Listening at: http://127.0.0.1:8000 (26555) [2024-12-24 20:03:17 +0000] [26555] [INFO] Using worker: uvicorn.workers.UvicornWorker [2024-12-24 20:03:17 +0000] [26556] [INFO] Booting worker with pid: 26556 [2024-12-24 20:03:17 +0000] [26557] [INFO] Booting worker with pid: 26557 [2024-12-24 20:03:17 +0000] [26558] [INFO] Booting worker with pid: 26558 [2024-12-24 20:03:17 +0000] [26559] [INFO] Booting worker with pid: 26559 [2024-12-24 20:03:21 +0000] [26556] [INFO] Started server process [26556] [2024-12-24 20:03:21 +0000] [26556] [INFO] Waiting for application startup. [2024-12-24 20:03:21 +0000] [26556] [INFO] Application startup complete. [2024-12-24 20:03:21 +0000] [26558] [INFO] Started server process [26558] [2024-12-24 20:03:21 +0000] [26558] [INFO] Waiting for application startup. [2024-12-24 20:03:21 +0000] [26558] [INFO] Application startup complete. [2024-12-24 20:03:21 +0000] [26557] [INFO] Started server process [26557] [2024-12-24 20:03:21 +0000] [26557] [INFO] Waiting for application startup. [2024-12-24 20:03:21 +0000] [26557] [INFO] Application startup complete. [2024-12-24 20:03:22 +0000] [26559] [INFO] Started server process [26559] [2024-12-24 20:03:22 +0000] [26559] [INFO] Waiting for application startup. [2024-12-24 20:03:22 +0000] [26559] [INFO] Application startup complete.
Install Nginx
sudo apt install nginx -y
Configure Nginx for Your FastAPI App.
Create a new configuration file:
sudo nano /etc/nginx/sites-available/fastapi
Add the following configuration
server { listen 80; server_name your_domain_or_IP; location / { proxy_pass http://127.0.0.1:8000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
Save and close the file.
Enable the Configuration
sudo ln -s /etc/nginx/sites-available/fastapi /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl restart nginx ##Result nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Configure Gunicorn as a Systemd Service
Create a Gunicorn Service File
sudo nano /etc/systemd/system/fastapi.service
Add the following configuration
[Unit] Description=Gunicorn instance to serve FastAPI After=network.target [Service] User=your_username Group=www-data WorkingDirectory=/path/to/your/project ExecStart=/path/to/your/project/venv/bin/gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app [Install] WantedBy=multi-user.target
Replace /path/to/your/project with your project’s directory.
Start and Enable the Service
sudo systemctl start fastapi sudo systemctl enable fastapi ##Result Created symlink /etc/systemd/system/multi-user.target.wants/fastapi.service → /etc/systemd/system/fastapi.service.
Visit your server's domain or IP in the browser
Secure Your Application with SSL
Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
Obtain an SSL Certificate
sudo certbot --nginx -d your_domain
Verify Auto-Renewal
sudo certbot renew --dry-run