DEV Community

Shaikh Al Amin
Shaikh Al Amin

Posted on

How to deploy fastAPI app with postgreSQL database in AWS EC2

SSH into your AWS cli

System update and install Postgres, Nginx, python3 pip, and venv

sudo apt update sudo apt upgrade sudo apt install curl ca-certificates sudo install -d /usr/share/postgresql-common/pgdg sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc sudo install -d /usr/share/postgresql-common/pgdg sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' sudo apt install postgresql-14 postgresql-client-14 sudo apt install python3-pip python3-venv sudo apt install nginx 
Enter fullscreen mode Exit fullscreen mode

Complete Postgres setup and create a database

sudo -i -u postgres psql -U postgres -h localhost postgres@shaikh:~$ psql postgres=# create database local_test; CREATE DATABASE postgres=# grant all privileges on database local_test to postgres; GRANT postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; ALTER ROLE postgres=# exit() 
Enter fullscreen mode Exit fullscreen mode

Generate ssh profile to pull the project from github

cd ~/.ssh/ ssh-keygen -t ed25519 -C "amin@test.com" 
Enter fullscreen mode Exit fullscreen mode

Copy the public key and put inside your github setting SSH keys with a valid name

cat id_ed25519.pub 
Enter fullscreen mode Exit fullscreen mode

Create a folder for git projects and clone the repo

cd ~/ mkdir projects cd projects/ git clone git@github.com:shaikhalamin/backend-project.git 
Enter fullscreen mode Exit fullscreen mode

Create a virtual environment and activate the venv

python3.10 -m venv venv source venv/bin/activate 
Enter fullscreen mode Exit fullscreen mode

Now CD into the project directory and install the requirements along with Gunicorn server

cd backend-project/ pip install -r requirements.txt pip install gunicorn 
Enter fullscreen mode Exit fullscreen mode

Setup your .env and modify accordingly

cp .env.example .env nano .env 
Enter fullscreen mode Exit fullscreen mode

Test the server run using unicorn

uvicorn main:app --host 0.0.0.0 --port 8000 cat .env nano configs/setting.py uvicorn main:app --host 0.0.0.0 --port 8000 
Enter fullscreen mode Exit fullscreen mode

Now Setup gunicorn service for running your application as a service

Get the Gunicorn installed path and project path by the below command

which gunicorn pwd 
Enter fullscreen mode Exit fullscreen mode

Create below service file and put the below following content in there

sudo nano /etc/systemd/system/devapi_gunicorn.service 
Enter fullscreen mode Exit fullscreen mode
 [Unit] Description=Gunicorn instance to serve fast API application After=network.target [Service] User=ubuntu Group=ubuntu WorkingDirectory=/home/ubuntu/backend-project/ ExecStart=/home/ubuntu/venv/bin/gunicorn -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8080 main:app [Install] WantedBy=multi-user.target 
Enter fullscreen mode Exit fullscreen mode

Now enable Gunicorn service

sudo systemctl enable devapi_gunicorn sudo systemctl start devapi_gunicorn sudo systemctl status devapi_gunicorn 
Enter fullscreen mode Exit fullscreen mode

Now create an nginx config file and change it according to your domain name

cd /etc/nginx/sites-available/ sudo nano orangedev.com 
Enter fullscreen mode Exit fullscreen mode
 server { server_name orangedev.com; location / { proxy_pass http://localhost:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } 
Enter fullscreen mode Exit fullscreen mode

Move the nginx config file to the sites-enabled

sudo ln -s /etc/nginx/sites-available/orangedev.com /etc/nginx/sites-enabled/ 
Enter fullscreen mode Exit fullscreen mode

Check the Nginx config and reload the Nginx

sudo nginx -t sudo systemctl reload nginx sudo systemctl restart nginx 
Enter fullscreen mode Exit fullscreen mode

Allow fire all for SSH, HTTP, and HTTPS only

sudo ufw status sudo ufw allow ssh sudo ufw allow http sudo ufw allow https # For posgres from another server sudo ufw allow 5432/tcp sudo ufw enable sudo ufw status 
Enter fullscreen mode Exit fullscreen mode

If everything goes well install certbot for SSL certificated

sudo snap install --classic certbot sudo ln -s /snap/bin/certbot /usr/bin/certbot [N:B] # Setup domain name and point to the correct IP and check from DNS checker then run the below command sudo certbot --nginx 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)