# Deploying the backend on cPanel (Passenger + MySQL)

Shared cPanel runs Python through **Phusion Passenger** ("Setup Python App"),
uses **MySQL**, and has **no Redis/Celery daemon** — so payout tasks run inline
(`CELERY_TASK_ALWAYS_EAGER=true`). These steps assume SSH/Terminal access.

## A. One-time setup in the cPanel UI

1. **MySQL Databases** → create a database (e.g. `cpuser_hustle`), create a user
   with a strong password, and **add the user to the database with ALL
   PRIVILEGES**. Note the real names (cPanel prefixes them with `cpuser_`).

2. **Setup Python App** (Application Manager) →
   - Python version: **3.10–3.12**
   - Application root: e.g. `hustlehub-backend`
   - Application URL: a subdomain (`api.yourdomain.com`) or a path
   - Create. cPanel makes a virtualenv + a `passenger_wsgi.py` + `.htaccess`,
     and shows the **"Enter to the virtual environment"** command — copy it.

## B. Terminal steps

```bash
# 1. Go to the application root cPanel created
cd ~/hustlehub-backend

# 2. Put the code here (git, or upload the backend/ folder's contents)
git clone <your-repo> .        # or upload so manage.py is in this directory

# 3. Activate the app's virtualenv (paste the command cPanel showed you)
source ~/virtualenv/hustlehub-backend/3.12/bin/activate
cd ~/hustlehub-backend

# 4. Install dependencies (cPanel-tailored: MySQL + no gunicorn)
pip install --upgrade pip
pip install -r requirements-cpanel.txt

# 5. Create the environment file
cp .env.cpanel.example .env
nano .env        # set SECRET_KEY, DJANGO_ALLOWED_HOSTS, DATABASE_URL, etc.
```

Minimum `.env` values (see `.env.cpanel.example`):
```
DJANGO_SETTINGS_MODULE=config.settings.prod
DJANGO_SECRET_KEY=<long-random-string>
DJANGO_DEBUG=false
DJANGO_ALLOWED_HOSTS=api.yourdomain.com,yourdomain.com
CSRF_TRUSTED_ORIGINS=https://api.yourdomain.com
DATABASE_URL=mysql://cpuser_hustle:PASSWORD@localhost/cpuser_hustle
CELERY_TASK_ALWAYS_EAGER=true
```

```bash
# 6. Migrate, seed system ledger accounts, collect static, create an admin
python manage.py migrate
python manage.py seed_system_accounts
python manage.py collectstatic --noinput
python manage.py createsuperuser        # or: python manage.py seed_demo

# 7. Restart the app so Passenger reloads
mkdir -p tmp && touch tmp/restart.txt
#   (or click "Restart" on the Python App in cPanel)
```

## C. Verify

```bash
curl https://api.yourdomain.com/api/health
# -> {"status": "ok", "service": "500-hustle-hub-api"}
```

- Admin: `https://api.yourdomain.com/admin/`
- Point the apps at it: web `VITE`/base-URL, Flutter
  `--dart-define=API_BASE_URL=https://api.yourdomain.com/api`.

## Notes & gotchas

- **Passenger entrypoint**: `passenger_wsgi.py` (included) loads
  `config.settings.prod`. If cPanel generated its own, replace it with ours.
- **HTTPS**: `config.settings.prod` enables SSL redirect/HSTS. Make sure AutoSSL
  is active on the domain, or you'll get redirect loops on plain HTTP.
- **MySQL driver**: we ship **PyMySQL** (pure-Python, no compiler). A shim in
  `config/__init__.py` registers it as MySQLdb automatically.
- **Background jobs**: with `CELERY_TASK_ALWAYS_EAGER=true`, payouts settle
  in-request. If you later wire an async mobile-money gateway, add a cron job
  hitting a management command to reconcile pending payouts instead.
- **Redeploy** after code changes: `git pull && pip install -r
  requirements-cpanel.txt && python manage.py migrate && python manage.py
  collectstatic --noinput && touch tmp/restart.txt`.
