# Deploy PEZAJOB on cPanel (test domain)

This guide assumes a **subdomain or addon domain** on cPanel (e.g. `test.pezajob.com`) with **Setup Python App** (Phusion Passenger).

## 1. Build upload package (on your Mac)

From the project root:

```bash
chmod +x scripts/build_cpanel_upload.sh
./scripts/build_cpanel_upload.sh
```

This creates `PEZAJOB_upgraded-cpanel-upload.zip` in the parent folder, excluding virtualenvs, caches, secrets, and local SQLite.

Optional flags:

```bash
./scripts/build_cpanel_upload.sh --with-db      # include local SQLite (demo data)
./scripts/build_cpanel_upload.sh --with-media # include media/ uploads (~400MB+)
```

Default archive is **code + static assets only** — run `setup_pezajob` or `migrate` on the server to populate data.

## 2. Upload and extract on cPanel

1. **File Manager** → create a folder, e.g. `pezajob` under your home directory (not necessarily `public_html` if you map the domain to this folder in the Python app).
2. Upload `PEZAJOB_upgraded-cpanel-upload.zip` and **Extract**.
3. Confirm `manage.py` and `passenger_wsgi.py` sit in the same folder.

**Permissions (typical):**

- Folders: `755`
- Files: `644`
- `media/` must be **writable** by the app user (`775` or `755` depending on host).

## 3. Create the Python application

**cPanel → Setup Python App → Create application**

| Field | Value |
|-------|--------|
| Python version | 3.10+ (3.11 or 3.12 preferred) |
| Application root | Path to extracted folder (e.g. `pezajob`) |
| Application URL | Your test domain or subdomain |
| Application startup file | `passenger_wsgi.py` |
| Application entry point | `application` |

Click **Create**. Note the virtualenv path.

## 4. Install dependencies

In **Setup Python App**, open **Enter to the virtual environment** (or use SSH):

```bash
cd ~/pezajob   # your application root
source /home/USER/virtualenv/pezajob/3.11/bin/activate   # path from cPanel UI

pip install --upgrade pip
pip install -r requirements-cpanel.txt
```

If install succeeds and the server has enough memory, you may add OCR packages:

```bash
pip install pymupdf rapidocr-onnxruntime
```

Or use the full `requirements.txt` on a VPS-style plan.

## 5. Environment file

On the server:

```bash
cp .env.cpanel.example .env
nano .env   # or edit in File Manager
```

Set at minimum:

- `DJANGO_SECRET_KEY` — long random string
- `DJANGO_DEBUG=False`
- `DJANGO_ALLOWED_HOSTS` — your test hostname
- `DJANGO_SITE_URL` — `https://your-test-domain`
- `DJANGO_CSRF_TRUSTED_ORIGINS` — `https://your-test-domain`
- `DJANGO_WHITENOISE_WSGI_STATIC=True`
- Database variables (PostgreSQL recommended)

**Restart** the Python app from cPanel after changing `.env`.

## 6. Database

### PostgreSQL (recommended)

1. cPanel → **PostgreSQL Databases** → create database and user, grant **ALL PRIVILEGES**.
2. Put credentials in `.env` (`DJANGO_DB_*` as in `.env.cpanel.example`).
3. Run migrations (SSH or **Run script** / terminal):

```bash
source .../bin/activate
cd ~/pezajob
python manage.py migrate
python manage.py setup_pezajob    # optional: demo data + accounts
python manage.py createsuperuser  # production admin
```

### SQLite (quick test only)

In `.env`:

```
PEZAJOB_DB_NAME=/home/USER/pezajob/db.sqlite3
```

Ensure the path is writable. Then `migrate` as above.

## 7. Static files

```bash
python manage.py collectstatic --noinput
```

If collectstatic fails on missing manifest references, set in `.env`:

```
DJANGO_STATICFILES_STORAGE=django.contrib.staticfiles.storage.StaticFilesStorage
```

Run `collectstatic` again and restart the app.

Uploaded files live under `media/`; with `DJANGO_WHITENOISE_WSGI_STATIC=True`, Django serves both `/static/` and `/media/` through Passenger.

## 8. SSL and domain

- **SSL/TLS** → issue AutoSSL for the test domain.
- Ensure `DJANGO_SITE_URL` and `DJANGO_CSRF_TRUSTED_ORIGINS` use `https://`.
- `DJANGO_SECURE_SSL=True` when `DEBUG=False` (default in `.env.cpanel.example`).

## 9. Verify

| Check | URL |
|-------|-----|
| Site home | `https://your-test-domain/` |
| Health | `https://your-test-domain/health/` → `{"status":"ok"}` |
| Django admin | `https://your-test-domain/django-admin/` |
| Platform dashboard | `https://your-test-domain/platform/dashboard/` (admin role) |

Errors append to `django_errors.log` in the project root.

## 10. Cron (optional)

cPanel → **Cron Jobs** — e.g. close expired jobs weekly:

```bash
cd /home/USER/pezajob && /home/USER/virtualenv/pezajob/3.11/bin/python manage.py <command>
```

## Troubleshooting

| Problem | What to try |
|---------|-------------|
| 500 on every page | Read `django_errors.log`; confirm `.env` loaded; restart Python app |
| Static/CSS missing | `collectstatic`; `DJANGO_WHITENOISE_WSGI_STATIC=True`; restart |
| CSRF / login fails | Fix `DJANGO_CSRF_TRUSTED_ORIGINS` and `DJANGO_SITE_URL` |
| Uploads fail | Permissions on `media/`; check host upload size limits |
| `pip install` OOM | Use `requirements-cpanel.txt`; skip OCR packages |
| Wrong Python | Recreate app in Setup Python App with 3.10+ |

## Files added for cPanel

- `passenger_wsgi.py` — Passenger entry
- `.env.cpanel.example` — server environment template
- `requirements-cpanel.txt` — lean dependencies
- `scripts/build_cpanel_upload.sh` — zip builder
