1. Initial Setup & Project Acquisition

Git Installation

Install Git version control system to clone the project repository.

sudo apt install git-all

Clone Repository

Download the billing application source code from GitHub.

git clone https://github.com/username/My_Billing.git

2. System Dependencies

Essential Build Tools

Install compiler tools and utilities needed for building packages.

sudo apt install -y build-essential curl git

Python & PostgreSQL Stack

Install Python runtime, pip, venv, and PostgreSQL database system.

sudo apt install -y python3 python3-venv python3-pip postgresql postgresql-contrib

3. Database Configuration

Access PostgreSQL

Switch to the postgres system user and open the interactive terminal.

sudo -i -u postgres
psql

Create User & Database

Create the dedicated billing user and database, then grant privileges.

CREATE USER billing_user WITH PASSWORD 'StrongPassword';
CREATE DATABASE billing OWNER billing_user;
ALTER DATABASE billing OWNER TO billing_user;

Configure Role Settings

Set encoding, isolation level, and timezone for the user.

ALTER ROLE billing_user SET client_encoding TO 'utf8';
ALTER ROLE billing_user SET default_transaction_isolation TO 'read committed';
ALTER ROLE billing_user SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE billing TO billing_user;

4. Django Project Setup

Setup Virtual Environment

Navigate to project, create venv, activate it, and install dependencies.

cd My_Billing
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

5. Django Settings

Configure settings.py

Update DEBUG, ALLOWED_HOSTS, and Database config in My_Billing/settings.py.

DEBUG = False
ALLOWED_HOSTS = ['localhost', '127.0.0.1']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'billing',
        'USER': 'billing_user',
        'PASSWORD': 'StrongPassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Static & Media Config

Configure static and media file paths in settings.py.

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

6. Migration & Admin

Apply Migrations

Migrate database, create superuser, and collect static files.

python manage.py migrate
python manage.py createsuperuser
python manage.py collectstatic

7. Gunicorn Setup

Install & Configure Gunicorn

Install Gunicorn and create the systemd service file: /etc/systemd/system/billing-gunicorn.service.


[Unit]
Description=Gunicorn service for Django (Billing)
After=network.target

[Service]
User=user
Group=www-data

WorkingDirectory=/home/user/My_Billing

Environment="PATH=/home/user/My_Billing/venv/bin"
Environment="DJANGO_SETTINGS_MODULE=Billing.settings"
Environment="PYTHONUNBUFFERED=1"

ExecStart=/home/user/My_Billing/venv/bin/gunicorn \
    --workers 3 \
    --timeout 120 \
    --bind unix:/run/billing-gunicorn/billing.sock \
    --access-logfile - \
    --error-logfile - \
    Billing.wsgi:application

Restart=always
RestartSec=5

RuntimeDirectory=billing-gunicorn
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

Start Gunicorn Service

Reload daemon, start, and enable the Gunicorn service.

sudo systemctl daemon-reload
sudo systemctl start billing-gunicorn
sudo systemctl enable billing-gunicorn
sudo systemctl status billing-gunicorn

Restart Gunicorn Service

Restart Command of the Gunicorn service.

                            sudo systemctl restart billing-gunicorn
sudo systemctl status billing-gunicorn

8. Nginx Configuration

Install & Configure Nginx

Install Nginx and create config: /etc/nginx/sites-available/billing.


upstream billing_app {
    server unix:/run/billing-gunicorn/billing.sock;
}

server {
    listen 127.0.0.1:8000;
    listen 192.168.1.169:8000;

    server_name localhost;

    access_log /var/log/nginx/billing_access.log;
    error_log  /var/log/nginx/billing_error.log;

    # ---- ALLOW ONLY LOCAL NETWORK ----
    allow 127.0.0.1;
    allow 192.168.1.0/24;
    deny all;

    location / {
        proxy_pass http://billing_app;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto http;
    }
}
                        

Enable Site & Restart

Link the config, test, and restart Nginx.

sudo ln -s /etc/nginx/sites-available/billing /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default

Test & Restart Nginx

Test and restart Nginx.

sudo nginx -t
sudo systemctl reload nginx

9. Static Files Permissions

Fix Permissions

Ensure Nginx (www-data) can read specific static directories.

sudo chmod 755 /home/user
sudo chmod 755 /home/user/My_Billing
sudo chmod -R 755 /home/user/My_Billing/staticfiles
sudo chown -R user:www-data /home/user/My_Billing/staticfiles
sudo systemctl reload nginx