Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Deploying a FastAPI App with Nginx, Supervisor, and Gunicorn

Sign upSign InSign upSign InMember-only storyYash PrakashFollowLevel Up Coding--ListenShareTake your Python API deployment skills to the next level in under 6 minutesFastAPI is one of the most important web frameworks in Python and for good reason. It’s wonderfully easy to setup and is faster than even Node.js. What’s not to like?I recently performed a full scale deployment of a side project that I recently launched: Quirkability, and it required a thorough deployment routine for the Fastapi server that I had to setup for the website to be live.There are a few quirks that you need to be aware of in these processes, and of course, some tools that you need to learn.No need to fear though as in this article, we’ll learn about it all.Let’s dive in 👇Before we start, make sure you have the following set up:In this tutorial, we will deploy a FastAPI application using the following technology stack:Enable Automatic Updates for more security. It’s optional though, so: your choice!Install the `unattended-upgrades` package on your Ubuntu system:Next, enable automatic updates by editing the configuration file:Ensure that the following lines are uncommented:Save and close the file.This will make sure to:Create a new user with sudo privileges so that your server isn’t always being accessed via the root user:Log in as the new user:You may also want to secure your server by changing the default SSH port, disabling root login, and most importantly, setting up a firewall.These steps are beyond the scope of this tutorial, but you can find many resources online to help you complete these tasks.One great way to enable firewall for everything except Nginx and SSH processes is by the following commands:Install the necessary libraries and tools to get your deployment down the road:I prefer to use Pyenv to manage the Python versions on my system(s):You’ll need sudo as it needs to export with system Path variables.Follow the instructions on the screen to add some snippets of code to your .bash_profile and .profile. Then, test it with:This will give you a list of Python versions that can be installed. Select something around 3.10 or higher if you’re like myself, using all the benefits of the newer versions.For specific requirements, select as per your needs.Create a new directory for your FastAPI application and navigate to it:Now clone your GitHub repo:Then create a virtual environment and activate it:Install your requirements.txt (you should have one already):Test your FastAPI app locally by running:Your app should now be running on localhost.Install Gunicorn in your virtual environment:Now, in your project directory, go ahead and create a config file for Gunicorn called gunicorn_run:Make this file executable by running:Finally, make the new BIND parameter path directory we specified above for defining our socket:This will help run Gunicorn with 2 worker processes and the Uvicorn worker class.Now, it’s time to setup to Supervisor to actually run Gunicorn with the above defined configurations!Create a new Supervisor configuration file for your FastAPI app:Add the following configuration snippet:Close and save the file. Next, quickly make a new directory called “logs” in your project directory:This will store our gunicorn errors.Make sure to replace `yourusername` with your actual username everywhere above.Now, tell Supervisor to read the new configuration and check the status of the FastAPI server:Your FastAPI app should now show up as running as a managed process.Test it by running a curl command from the terminal:It should return your desired response.Create a new Nginx configuration file for your FastAPI server:Add the following configuration snippet inside:Replace `yourdomain.com` with your actual domain name.Save and close the file. Then create a symbolic link to the `sites-enabled` directory:Test your Nginx configuration and restart Nginx:Your FastAPI app should now be accessible at `http://yourdomain.com`.Install snapd first:To obtain an SSL certificate for your domain, first install Certbot:Run Certbot to interactively obtain and install the SSL certificate on specific domains you’ve defined inside the Nginx config file:After following through the process, Certbot will automatically update your Nginx configuration to enable HTTPS.Certbot will also automatically handle the renewal of your certificate when it’s time to actually do it. To test that it works, type and enter:If you get a permission error telling you that NGINX cannot access the unix socket, just add the www-data user (which typically is the user running the NGINX processes) to the yourusername group like so:Restart Supervisor process when your code changes (eg, on a Git Pull):Hurray! 🎊You’ve successfully deployed a FastAPI Python application using Nginx, Gunicorn, and Supervisor. Your app is now running as a managed process, and you’ve also secured it with an SSL certificate. Well done!This setup is now suitable for production environments and can be easily scaled to handle more traffic by adjusting the number of Gunicorn worker processes.Hope you learned something new from this tutorial!If you want a regular supply of these informative articles, get my newsletter: https://codecast.substack.comThanks for being a part of our community! Before you go:🔔 Follow us: Twitter | LinkedIn | Newsletter🚀👉 Join the Level Up talent collective and find an amazing job----Level Up CodingCreating software and writing about it | Helping people make their first $ online: https://quirkability.xyzYash PrakashinTowards Data Science--3Julie Perilla GarciainLevel Up Coding--25Arslan MirzainLevel Up Coding--19Yash PrakashinTowards Data Science--Dino CajicinGeek Culture--Shomarri Romell DiazinFAUN — Developer Community 🐾--Daniel EastermaninLevel Up Coding--inVeritainDev Genius--The Useful TechinMac O’Clock--23Arturo Cuicas--HelpStatusWritersBlogCareersPrivacyTermsAboutText to speechTeams



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Deploying a FastAPI App with Nginx, Supervisor, and Gunicorn

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×