Deploying FastAPI with Screen in background: Easier way!

At first, I thought, I would show a simple deployment e.g. with Heroku, Railway or other platforms as a service. However, they abstract out a lot of complexity. This is good for beginners but I don't want you to be a beginner all the years. So, let's do it the hard way.
This also brings the most learning opportunity, This could be tiring, you might face lots of bugs in the path but it will be totally worth it. 🐛🦟🐞

Let's visit Digitalocean and create a droplet/VM to access it over the internet.

Let's start with a basic naive deployment first.
Start by creating a Droplet/VM.

Let's ssh into our VM and install the basics requirements:

sudo apt-get update
sudo apt-get upgrade 
sudo apt-get install python3-pip
apt install python3.x-venv
sudo apt-get install nginx

Okay, let's pull up our codebase and install the requirements.

git clone https://github.com/sourabhsinha396/fastapi-blog.git

To install the requirements, I would prefer a virtual environment. Also, make sure to use psycopg2-binary instead of psycopg2 in the requirements.txt file. Also, I would suggest to use SQLite for a faster setup process. So, uncomment the SQLite lines in db/session.py

pip install -r requirements.txt
uvicorn main:app

Note that we did not use the --reload flag, as we don't want to reload on every code change.
In a different terminal, lets visit the sites-available directory and create a nginx conf for our fastapi app.

cd /etc/nginx/sites-available/
nano fastapi

paste the below configuration in this file and then save it with ctrl+x

server {
    listen 80;
    server_name your-domain.com 143.244.156.129;

    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;
    }
}

test the configuration once with nginx -t and check if it says successful.
Link the file to sites-enabled and restart nginx service.

sudo ln -s /etc/nginx/sites-available/fastapi_deployment /etc/nginx/sites-enabled
sudo service nginx restart

Now, you can safely visit http://public_ip or http://your-domain.com. Make sure to avoid visiting https version. It was good right, but there is a problem, what if we exit our server? We need to terminate the uvicorn process and then the website will be down :( Why not run this uvicorn process in a terminal in the background? That will enable us to leave the server without killing the process. To do this, we are going to use screen.

screen -ls  # lists all the screen
screen -S fastapi  #creates new screen
#do your work here e.g. starting uvicorn
ctrl+A and quickly press ctrl+D to detach the screen and come back to main terminal.
screen -r fastapi # to re-enter the fastapi screen 
ctrl + D  # to terminate the screen

By using this multi-screen strategy, we can  let the uvicorn process run in the background, and nginx connect with uvicorn and serve our webapp.
It is definitely hacky approach but always remember "version 1 is better than version None". So, its good. We will definitely improve our deployment strategy in the next episode of FastAPITutorial.

FastAPITutorial

Brige the gap between Tutorial hell and Industry. We want to bring in the culture of Clean Code, Test Driven Development.

We know, we might make it hard for you but definitely worth the efforts.

Contacts

Refunds:

Refund Policy
Social

Follow us on our social media channels to stay updated.

© Copyright 2022-23 Team FastAPITutorial