Deploying a FastAPI application on Ubuntu 22.04 for a real project involves several steps, including setting up the server, installing necessary software, configuring the application, and setting up a reverse proxy. Here's a step-by-step tutorial
First, update your system packages:
sudo apt updateInstall Python and Pip if they are not already installed:
sudo apt install python3 python3-pipCheck it:
python3 --version
pip --version
Create a virtual environment for your FastAPI project:
sudo apt install python3.10-venv
python3 -m venv fastapi-env
source fastapi-env/bin/activateInstall FastAPI and Uvicorn within your virtual environment:
pip install fastapi
pip install 'uvicorn[standard]'Create project folder and main.py file:
mkdir my_fastapi_project
cd my_fastapi_project
touch main.pyCreate a sample FastAPI application. For example, edit a file named main.py:
vim main.pyfrom fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}Run the application locally to ensure it works:
uvicorn main:app --reloadYou should be able to access the application at http://127.0.0.1:8000.
For test in any browser run the application with real IP on 80 port:
uvicorn main:app --host 85.159.231.14 --port 80 --workers 4Step 7: Set Up Gunicorn3 to Serve the FastAPI Application
Install Gunicorn:
pip install gunicornCreate file gunicorn_conf.py for settings Gunicorn:
vim gunicorn_conf.pybind = "0.0.0.0:8000"
workers = 4Run Gunicorn with Uvicorn worker:
gunicorn -k uvicorn.workers.UvicornWorker -c gunicorn_conf.py main:appStep 8: Install and Configure Nginx4
Install Nginx to act as a reverse proxy:
sudo apt install nginxCreate a new Nginx configuration file for your FastAPI application:
sudo nano /etc/nginx/sites-available/fastapiAdd the following content to the file, replacing your_domain with your actual domain or IP address:
server {
listen 80;
server_name your_domain;
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Enable the configuration by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/fastapi /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginxCreate a Gunicorn systemd service file:
sudo vim /etc/systemd/system/fastapi.serviceAdd the following content:
[Unit]
Description=FastAPI service
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/root/my_fastapi_project
ExecStart=/root/fastapi-env/bin/gunicorn -k uvicorn.workers.UvicornWorker -c /root/my_fastapi_project/gunicorn_conf.py main:app
[Install]
WantedBy=multi-user.targetReplace /path/to/your/project with the actual path to your FastAPI project.
Reload systemd to apply the new service, start it, and enable it to run on boot:
sudo systemctl daemon-reload
sudo systemctl start fastapi
sudo systemctl enable fastapiIf you have a firewall enabled, you need to allow traffic on port 80 (HTTP):
sudo ufw allow 'Nginx Full'
sudo ufw enableYour FastAPI application should now be accessible at your domain or IP address. Open a web browser and go to http://your_domain.
journalctl -u fastapi.service
systemctl status fastapi
vim /var/log/nginx/error.logFor a secure connection, you can set up HTTPS using Let's Encrypt:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domainFollow the prompts to complete the installation and obtain your SSL certificate.
You've successfully deployed a FastAPI application on Ubuntu 22.04. This setup includes running FastAPI with Gunicorn, serving it behind Nginx as a reverse proxy, and optionally securing it with HTTPS using Let's Encrypt. Make sure to replace placeholder values with your actual domain, paths, and project specifics.
Amazing. Thank you very much!
Just a minor thing. In step 9, you mention
Replace /path/to/your/project with the actual path to your FastAPI project.But
/path/to/your/projectis not in the systemd service file above.Anyways. I got everything working.