Deploying FastAPI Application on a Linux Server with Gunicorn and Nginx
Introduction:
FastAPI, a modern and high-performance Python web framework, empowers developers to rapidly build robust APIs. When it comes to deploying FastAPI applications in a production environment, leveraging Gunicorn as a WSGI HTTP server and Nginx as a reverse proxy server can significantly enhance performance and scalability. In this comprehensive guide, we will walk you through the step-by-step process of deploying a FastAPI application on a Linux server using Gunicorn and Nginx, ensuring a streamlined and professional deployment.
Prerequisites: Before proceeding with the deployment, ensure you have the following prerequisites in place:
- A Linux server (We recommend Ubuntu 18.04 or a higher version).
- A fully developed FastAPI application ready for deployment.
- Superuser or sudo access privileges on the server.
Step 1: Setting up the Linux Server:
a). Establish an SSH connection with your Linux server.
b). Update the server’s package list to ensure you have the latest versions
sudo apt update
c). Install Python 3 and pip, the package installer for Python
sudo apt install python3 python3-pip
Step 2: Installing and Configuring Gunicorn
a). Install Gunicorn using pip, ensuring you have the latest stable version
sudo pip3 install gunicorn
b). Create a Gunicorn service file using a nano text editor or vim:
sudo nano /etc/systemd/system/myapp.service
c). Inside the file, include the following configuration:
[Unit]
Description=My FastAPI Application
After=network.target
[Service]
User=<your_username>
Group=www-data
WorkingDirectory=/path/to/your/app
ExecStart=/usr/local/bin/gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app
[Install]
WantedBy=multi-user.target
Remember to replace <your_username>
with your username and /path/to/your/app
with the actual path to your FastAPI application's main file.
d). Save and close the file.
e). Start the Gunicorn service to launch your FastAPI application:
sudo systemctl start myapp
f). Enable the service to automatically start on server boot:
sudo systemctl enable myapp
Step 3: Configuring Nginx as a Reverse Proxy
a). Install Nginx, a popular web server and reverse proxy server
sudo apt install nginx
b). Create an Nginx server block configuration file
sudo nano /etc/nginx/sites-available/myapp
c). Insert the following configuration into the file:
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://localhost: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;
}
}
Replace your_domain.com
with your domain or server IP address.
d). Create a symbolic link to enable the server block configuration
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
e). Test the Nginx configuration for syntax errors:
sudo nginx -t
f). If the test is successful, restart Nginx:
sudo systemctl restart nginx
Conclusion:
By following the steps outlined above, you should now have your FastAPI application successfully deployed on a Linux server using Gunicorn and Nginx.
Thank you for your interest in reading my story on Medium!