First, make sure Python and Pip are installed on your server. On Ubuntu, you can use the following commands:
sudo apt update
sudo apt install python3 python3-pip
Create a virtual environment and activate it:
python3 -m venv myenv
source myenv/bin/activate
Install FastAPI and Gunicorn:
pip install fastapi uvicorn gunicorn
On Ubuntu, you can install Nginx using the following command:
sudo apt install nginx
Create a new Python file named main.py
with the following code:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
You can start the FastAPI application with Gunicorn using the following command:
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app -b 0.0.0.0:8000
-w 4
: This specifies the number of worker processes. In this case, we are using 4 worker processes.-k uvicorn.workers.UvicornWorker
: This tells Gunicorn to use the Uvicorn worker class, which is optimized for FastAPI applications.main:app
: This specifies the Python file (main.py
) and the FastAPI application instance (app
).-b 0.0.0.0:8000
: This binds the application to all available network interfaces on port 8000.Create a new Nginx configuration file for your FastAPI application:
sudo nano /etc/nginx/sites-available/fastapi
Add the following configuration to the file:
server {
listen 80;
server_name your_domain_or_ip;
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;
}
}
listen 80
: This tells Nginx to listen on port 80.server_name your_domain_or_ip
: Replace your_domain_or_ip
with your actual domain name or server IP address.proxy_pass http://127.0.0.1:8000
: This forwards all incoming requests to the Gunicorn server running on 127.0.0.1:8000
.Create a symbolic link to enable the configuration:
sudo ln -s /etc/nginx/sites-available/fastapi /etc/nginx/sites-enabled/
Test the Nginx configuration:
sudo nginx -t
If the test is successful, restart Nginx:
sudo systemctl restart nginx
[Unit]
Description=Gunicorn instance to serve FastAPI app
After=network.target
[Service]
User=your_user
Group=www-data
WorkingDirectory=/path/to/your/app
Environment="PATH=/path/to/your/venv/bin"
ExecStart=/path/to/your/venv/bin/gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app -b 0.0.0.0:8000
[Install]
WantedBy=multi-user.target
Save the file as /etc/systemd/system/fastapi.service
and then run the following commands:
sudo systemctl daemon-reload
sudo systemctl start fastapi
sudo systemctl enable fastapi
worker_connections
, keepalive_timeout
, and client_max_body_size
based on your application’s requirements.Deploying FastAPI with Nginx and Gunicorn is a great way to ensure high performance, reliability, and security for your FastAPI applications in a production environment. By following the steps and best practices outlined in this blog, you can easily deploy and manage your FastAPI application. Remember to continuously monitor and optimize your setup to provide the best user experience.