Python API Development - Comprehensive Course for Beginners
Following this course - Python API Development
- Description
- Useful Commands
- Require (Arch)
- Pyenv Setup (Arch)
- Bash Shell Setup
- Fish Shell Setup
- Pyenv Final Setup
- Pip Install 'requirements.txt'
- Postgres & PGAdmin (Arch)
- Ubuntu VM Setup
- 1. Setup Ubuntu Server VM. (Used QEMU & Virt Manager)
- 2. Setup Directories and Repo
- 3. Install / Update Packages
- 4. Install Pip Package
- 5. Setup Postgresql
- 6. Setup Postgresql Continued...
- 7. Virtualenv Setup
- 8. Pip Install 'requirements.txt'
- 9. Setup and Configure Environment Variables within '/home/ubuntu/.env'
- 10. Setup Alembic
- 11. Setup systemctl for gunicorn within '/etc/systemd/system/'
- 12. Setup Nginx and setup config within '/etc/nginx/sites-available'
- 13. UFW Firewall Configuration
- Docker Setup (Arch)
source venv/bin/activate # bash
. venv/bin/activate.fish # fish
deactivate
uvicorn app.main:app --reload
pgadmin4
docker ps
docker exec -it {IMAGE NAME} bash
docker-compose -f docker-compose-dev.yml up -d
docker-compose -f docker-compose-dev.yml down
pytest -v
pytest --disable-warnings -vsudo pacman -S vscode postman postgresql pyenv docker docker-composesudo pacman -S --needed base-devel openssl zlib xz
curl https://pyenv.run | bashecho 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fiecho 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profileset -Ux PYENV_ROOT $HOME/.pyenv
fish_add_path $PYENV_ROOT/binset -Ux PYENV_ROOT $HOME/.pyenv
set -U fish_user_paths $PYENV_ROOT/bin $fish_user_pathspyenv init - | sourceexec "$SHELL"pyenv install 3.11.0
pyenv local 3.11.0
python -m venv venv
source venv/bin/activate # bash
. venv/bin/activate.fish # fish
python --version pip install -r requirements.txtsudo -u postgres -i
initdb --locale $LANG -E UTF8 -D '/var/lib/postgres/data/'
exit
sudo systemctl enable --now postgresql
psql -U postgres
postgres=# \password {password_here}
exit
sudo mkdir /var/lib/pgadmin
sudo mkdir /var/log/pgadmin
sudo chown $USER /var/lib/pgadmin
sudo chown $USER /var/log/pgadmin
pip install pgadmin4
pgadmin4Details in here - https://ravinderfzk.medium.com/install-postgresql-and-pgadmin4-in-arch-linux-eb013b45540f
admin@admin.com admin@123
mkdir -p /home/ubuntu/app/src
cd /home/ubuntu/app/src
git clone https://github.com/Uzy777/Python-API-Development.git .sudo apt update
sudo apt upgrade -y
sudo apt install python3 python3-pip postgresql postgresql-contrib nginx -ysudo pip3 install virtualenvpsql -U postgres
postgres=# \passwordAdd the following line in section "CONNECTIONS AND AUTHENTICATION" to allow all ip addresses to connect
listen_addresses '*'
Edit the connections section at the bottom to all every machine to connect
local all postgres scram-sha-256
local all all scram-sha-256
host all all 0.0.0.0/0 scram-sha-256
host all all ::/0 scram-sha-256
systemctl restart postgresql
psql -U postgresShould now be able to connect using pgadmin interface
cd /home/ubuntu/app
virtualenv venv
source venv/bin/activatecd /home/ubuntu/app/src
pip install -r requirements.txtDATABASE_HOSTNAME=localhost
DATABASE_PORT=5432
DATABASE_PASSWORD=
DATABASE_NAME=fastapi
DATABASE_USERNAME=postgres
SECRET_KEY=
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=300
set -o allexport; source /home/ubuntu/.env; set +o allexport
rebootcd /home/ubuntu/app
source venv/bin/activate
cd src
alembic upgrade headsudo cp /home/ubuntu/app/src/gunicorn.service /etc/systemd/system/api.service
systemctl start api.service
systemctl enable api.service
systemctl status api.servicesystemctl start nginxlocation / {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
systemctl restart nginxsudo ufw status
sudo ufw allow http
sudo ufw allow https
sudo ufw allow ssh
# sudo ufw allow 5432 (Use to connect in from outside)
sudo ufw enable
sudo ufw statussudo systemctl start docker.service
sudo systemctl enable docker.servicesudo usermod -aG docker $USER
newgrp dockersudo docker build -t fastapi .docker-compose up -d
# docker-compose down (Teardown docker build)