https://code.iweb.city/iw/servercode/src/branch/main/postgres-pgadmin-python3-django-gunicorn-nginx
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04
https://tonyteaches.tech/django-nginx-uwsgi-tutorial/
https://www.datadoghq.com/blog/nginx-502-bad-gateway-errors-gunicorn/#nginx-cant-access-the-socket

sudo apt install python3-pip python3-dev python3-venv libpq-dev postgresql postgresql-contrib nginx curl
# OPTIONAL FOR OLDER PYTHON:  sudo apt install virtualenv python3-virtualenv

su - postgres
psql
# ONLY DROP IF NEEDED TO FIX: DROP DATABASE MYDATABASE;
CREATE USER MYDBUSERNAME WITH PASSWORD 'MYDBPASSWORD' CREATEDB;
CREATE DATABASE MYDATABASE WITH OWNER MYDBUSERNAME;
ALTER ROLE MYDBUSERNAME SET client_encoding TO 'utf8';
ALTER ROLE MYDBUSERNAME SET default_transaction_isolation TO 'read committed';
ALTER ROLE MYDBUSERNAME SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE MYDATABASE TO MYDBUSERNAME;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO MYDBUSERNAME;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO MYDBUSERNAME;
grant postgres to MYDBUSERNAME
\q

sudo adduser MYUSERNAME
sudo usermod -aG sudo MYUSERNAME
su - MYUSERNAME

# sudo ufw allow 5432
# sudo vi /etc/postgresql/15/main/postgresql.conf
listen_addresses = 'localhost, 192.168.1.3, 192.168.1.6, 192.168.1.7, 192.168.1.66, 192.168.1.123, 192.168.1.126, 192.168.1.127, 192.168.1.222'

# sudo vi /etc/postgresql/15/main/pg_hba.conf
local   MYDATABASE    MYDBUSERNAME    scram-sha-256
host    MYDATABASE    MYDBUSERNAME    192.168.1.0/24    scram-sha-256

# TEST DATABASE CONNECTION
psql "postgres://MYDBUSERNAME@192.168.1.6/MYDATABASE"

mkdir ~/env
python3 -m venv /home/MYUSERNAME/env/MYPROJECTDIR
ls ~/env/MYPROJECTDIR/bin
source ~/env/MYPROJECTDIR/bin/activate
which python
pip install Django gunicorn psycopg2-binary
django-admin startproject iwebcity
cd iwebcity
vi ~/iwebcity/iwebcity/settings.py
ALLOWED_HOSTS = ['.iweb.city', '.localhost', '127.0.0.1', '[::1]', '192.168.1.6']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'MYDATABASE',
        'USER': 'MYDBUSERNAME',
        'PASSWORD': 'MYDBPASSWORD',
        'HOST': 'localhost',
        'PORT': '',
    }
}
STATIC_URL = '/static/'
import os
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

~/iwebcity/manage.py makemigrations
~/iwebcity/manage.py migrate
~/iwebcity/manage.py createsuperuser
~/iwebcity/manage.py collectstatic

# sudo ufw allow 8001
# TEST DJANGO PYTHON INCLUDED WEBSERVER ONLY  - REBOOT TO APPLY ALL FIREWALL AND POSTGRES CHANGES
~/iwebcity/manage.py runserver 0.0.0.0:8001
gunicorn --bind 0.0.0.0:8001 iwebcity.wsgi
deactivate
_______________________________________________________________

vi /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target

vi /etc/systemd/system/gunicorn.service
Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=serv
Group=nginx
WorkingDirectory=/home/serv/iwebcity
ExecStart=/home/serv/env/MYPROJECTDIR/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          iwebcity.wsgi:application

[Install]
WantedBy=multi-user.target
_______________________________________________________________
sudo systemctl daemon-reload
sudo systemctl restart gunicorn
systemctl start gunicorn.socket
systemctl enable gunicorn.socket
systemctl enable gunicorn.service
systemctl status gunicorn.socket
curl --unix-socket /run/gunicorn.sock localhost
sudo journalctl -u gunicorn
sudo tail -F /var/log/nginx/error.log
file /run/gunicorn.sock
namei -l /run/gunicorn.sock
The following logs may be helpful:
    Check the Nginx process logs: sudo journalctl -u nginx
    Check the Nginx access logs: sudo less /var/log/nginx/access.log
    Check the Nginx error logs: sudo less /var/log/nginx/error.log
    Check the Gunicorn application logs: sudo journalctl -u gunicorn
    Check the Gunicorn socket logs: sudo journalctl -u gunicorn.socket
_______________________________________________________________
#NGINX

vi dj.iweb.city.conf
upstream django {
    server unix:///home/iw/iwebcity/iwebcity.sock;
}
# configuration of the server
server {
    listen      8001;
    server_name django.iweb.city www.django.iweb.city dj.iweb.city;
    charset     utf-8;
    # max upload size
    client_max_body_size 75M;
    # Django media and static files
    location /media  {
        alias /home/iw/iwebcity/media;
    }
    location /static {
        alias /home/iw/iwebcity/static;
    }
    # Send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/iw/iwebcity/uwsgi_params;
    }
}
