
FastAPI is a modern Python web framework for building high-performance APIs and web applications. It supports asynchronous programming using async and await, enabling efficient handling of many simultaneous client connections. Built on the ASGI (Asynchronous Server Gateway Interface) standard, FastAPI is ideal for real-time applications and remains compatible with WSGI for traditional deployments. It also offers automatic, interactive API documentation through Swagger UI, making development and testing easier and faster.
This article explains how to deploy FastAPI applications using Gunicorn as the application server and Nginx as a reverse proxy on an Ubuntu 20.04 server.
fastapi.example.com.In this section, you will create a FastAPI application inside a virtual environment. You will set up the project directory, install necessary dependencies, and test the application using the uvicorn ASGI server. Follow the steps below to setup the FastAPI application.
Navigate to your home directory.
Create a new directory for your FastAPI project.
Navigate into the project directory.
Update the APT package index.
Install the python3-venv package.
Create a Python virtual environment named env.
Activate the virtual environment.
Install the fastapi package with all optional dependencies.
Add the following FastAPI application code into the file.
Save and close the file by pressing Ctrl + X, then Enter.
Create a temporary web server using uvicorn.
Open a new terminal session and run the following command.
Your output should be similar to the one below:
Gunicorn is a Python WSGI HTTP server for UNIX systems. It simplifies the management of FastAPI applications and supports ASGI via Uvicorn worker classes. Follow the steps below to deploy your FastAPI application using Gunicorn.
Install the gunicorn Python package.
Start a temporary Gunicorn server with the Uvicorn worker.
Open a new session and test the Gunicorn deployment using curl .
Your output should be similar to the one below:
Create a Gunicorn configuration file named gunicorn_conf.py.
Add the following code into the file.
Save and close the file by pressing Ctrl + X, then Enter. Replace the paths of the variables if they are different in your case.
Create and open the systemd unit file for the FastAPI Gunicorn service.
Add the following systemd service configuration to the file.
Save and close the file by pressing Ctrl + X, then Enter.
Reload the systemd daemon.
Start and enable the FastAPI Gunicorn service.
Your output should be similar to the one below:
View the status of newly created service.
Test the response from the socket file using curl.
Your output should be similar to the one below:
In this section, you will configure Nginx as a reverse proxy for your FastAPI application. Nginx is a lightweight, high-performance web server commonly used to forward client requests to application servers like Gunicorn. It allows you to serve your application on standard HTTP and HTTPS ports (80 and 443), improves request handling and performance, and supports SSL termination for secure connections.
Install the Nginx Web Server.
Create a virtual host configuration.
Add the following configuration. Replace fastapi.example.com with your actual domain name.
Save and close the file with Ctrl + X, then Enter.
Create a symbolic link to the sites-enabled/ directory to activate the vhost.
Check the Nginx configuration syntax for any errors.
Your output should be similar to the one below:
Reload the Nginx service to apply the new configuration.
Allow HTTP and HTTPS traffic through the firewall.
Verify the firewall status to confirm the rules are applied.
To encrypt traffic and protect user data, you can secure your FastAPI application with HTTPS using a free SSL certificate from Let’s Encrypt. This section shows you how to configure SSL for your Nginx reverse proxy using Certbot.
Install Certbot and the Nginx plugin
Request and install an SSL certificate.
Certbot will automatically update your Nginx configuration. When prompted, enter your email address and agree to the terms of service.
Visit the following link in a browser to confirm SSL is working.
Test automatic certificate renewal.
If no errors appear, your SSL certificate will renew automatically every 90 days.
You have deployed a FastAPI application with Gunicorn and Nginx on a production ready Ubuntu server. This setup uses a Unix socket for efficient communication between Gunicorn and Nginx, and leverages systemd to manage the application as a background service. By securing Nginx with a free SSL certificate from Let’s Encrypt, you’ve ensured that all client traffic is encrypted over HTTPS. This configuration provides a robust, scalable, and secure environment for running FastAPI applications in production. For more information related to FastAPI, visit the official FastAPI website.
0 Comments
Be the first to comment and share your perspective with the community.