01kg
Posted on September 11, 2024
Recently, I created a FastAPI app as the backend for my multiple frontend services.
If any fronend visits https://some-api.somecompany.com/my-api
, then backend would respond.
TL;DR:
Installations
- Create a VM in any Cloud platform. (I use Ubuntu 24.04 LTS)
- Create a DNS record points to VM's IP. (let's say
some-api.somecompany.com
) -
Install Nginx using Ubuntu's
apt
package manager. -
Create and edit a configuration file
sudo vim /etc/nginx/conf.d/some-api.conf
with the following content:
server { listen 80; server_name some-api.somecompany.com location /my-api/ { proxy_pass http://0.0.0.0:8000 } }
-
Install CertBot using
snap
.snap
is included in Ubuntu 24.04 LTS
# Install Certbot sudo snap install --classic certbot # Prepare the Certbot command sudo ln -s /snap/bin/certbot /usr/bin/certbot
-
Run
sudo certbot --nginx
to start fetching a cert to enable HTTPS.
After providing an email "for urgent renewal and security notices", agreeing Terms of Service, agreeing sharing your email address, Certbot scan all files under/etc/nginx/conf.d
(only one in this case) and list domain names:
Which names would you like to activate HTTPS for? We recommend selecting either all domains, or all domains in a VirtualHost/server block. --------------------- 1: some-api.somecompany.com ---------------------
Select 1, hit
Enter
.
Testing
-
Spin up the FastAPI app
cd /THE/ABSOLUTE/PATH/TO/YOUR/APP && source .venv/bin/activate && uvicorn main:app --host 0.0.0.0 --port 8080
-
cd /THE/ABSOLUTE/PATH/TO/YOUR/APP
: Changes the current directory to the absolute path specified. This is where your application code is located. -
&&
: This is a logical operator in the shell that allows you to run multiple commands in sequence. The command following&&
will only run if the command before it was successful. -
source .venv/bin/activate
: Sources (executes) theactivate
script located in the.venv/bin
directory. This script is typically used to activate a virtual environment in Python. When you activate a virtual environment, it sets up the environment variables and paths specific to that virtual environment. -
uvicorn main:app --host 0.0.0.0 --port 8080
: This command starts the Uvicorn ASGI server to run your application.main:app
refers to the Python filemain.py
and theapp
object within it that Uvicorn should run. The--host 0.0.0.0
flag specifies that the server should listen on all network interfaces, and--port 8080
specifies the port number (8080) on which the server should listen for incoming connections.
-
Visit
https://some-api.somecompany.com/my-api
see if it works
Deploying
Make sure your VM's IP is a "static" one, which means it will never change after VM stop (deallocated) and start.
-
Let Ubuntu run FastAPI app after any unexpected restart:
echo "@reboot root bash -c 'cd /THE/ABSOLUTE/PATH/TO/YOUR/APP && source .venv/bin/activate && uvicorn main:app --host 0.0.0.0 --port 8080 > /tmp/YOU_NAME_IT.log 2>&1'" | sudo tee -a /etc/crontab
this command sets up a cron job that will run the specified command at system reboot. The command changes to the app directory, activates the virtual environment, starts the Uvicorn server, and logs the output to a specified file.
***Why
bash -c
?
In the result of my practice, withoutbash -c
the FastAPI app won't run successfully after reboot. Restart your VM to see if it still works.
Posted on September 11, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.