# Deploy DramaBox API on cPanel

These steps assume your host provides **Setup Python App** (CloudLinux “Python Selector”) or **Application Manager** with **Passenger**. Shared hosts differ; if Python apps are not offered, use a VPS or a platform that supports long‑running Python processes.

## Finding and setting up Python (if you are new to cPanel)

Not every cPanel looks the same; hosts rename menus or hide features by plan.

### Step 1 — Log in

Open the URL your host gave you, for example `https://yourdomain.com:2083` or `https://cpanel.yourhost.com`, and sign in.

### Step 2 — Search for Python

At the **top of cPanel** there is usually a **search box**. Type:

- `python`  

Common icons / names you might see:

| What you might see | What it usually means |
|--------------------|------------------------|
| **Setup Python App** | Main tool on CloudLinux hosts — use this first |
| **Python App** | Same idea, shorter name |
| **Python Selector** | Often opens the same Python app installer |
| **Application Manager** | May list Python (and other) apps |

If **nothing** appears when you search “python”, your plan may not include Python web apps.

### Step 3 — Open “Setup Python App” (or equivalent)

1. Click **Create Application** / **Create** / **+** (wording varies).
2. Choose **Python version** **3.10 or newer** (3.11 or 3.12 is fine).
3. **Application root**: the folder where you will upload DramaBox (must contain `passenger_wsgi.py`). Many people use something like `api` or `dramabox` under their home directory — you can create an empty folder first in **File Manager**, then paste that path here.
4. **Application URL**: your subdomain, e.g. `api.yourdomain.com`, or a path like `yourdomain.com/api` if the tool only allows that.
5. **Application startup file** / **entry point**: enter **`passenger_wsgi.py`**.
6. Finish the wizard. cPanel will create a **virtual environment (venv)** and may show you the path and a **“Run Pip Install”** or **Terminal** option.

Write down or copy the **full path** to the app folder and to **venv** if shown — you need them for `pip install`.

### Step 4 — If you truly cannot find Python anywhere

1. Open a **ticket or live chat** with your hosting provider and ask exactly:  
   *“Do you support Python web applications on cPanel (Setup Python App / Passenger / CloudLinux Python Selector)? If yes, where do I enable it?”*
2. If they say **no**, this shared plan cannot run this API the “cPanel Python” way. Options then:
   - Upgrade to a plan that includes **Python** / **Passenger**, or  
   - Use a **VPS**, **Docker**, or a service like **Railway / Render / Fly.io** and point your subdomain there.

### Step 5 — After Python is “set up”

You still must **upload your project files**, run **`pip install -r requirements.txt`** inside that app’s venv, and **restart** the application (same Python screen usually has a **Restart** button). Those steps are in the sections below.

---

## 1. Create the subdomain

1. In cPanel: **Domains** → **Create** (or **Subdomains**).
2. Add e.g. `api.yourdomain.com`, document root e.g. `public_html/api` **or** a dedicated folder under `/home/youruser/` (follow what **Setup Python App** expects—often an empty folder you choose as **application root**).

3. Enable **SSL**: **SSL/TLS Status** → run **AutoSSL** for `api.yourdomain.com`.

## 2. Upload the project

Upload the whole project (including `app/`, `dramas.json`, `requirements.txt`, `passenger_wsgi.py`) into your **application root** folder (FTP / File Manager / Git).

Ensure:

- `dramas.json` is present (large file uploads may need FTP or zip + extract).
- The app can create **`data/`** and **`data/catalog.db`** (first run). Set folder permissions so the Python user can write (often **755** for `data/`).

## 3. Setup Python App (typical CloudLinux flow)

1. Open **Setup Python App** (or **Python App**).
2. **Python version**: 3.10+ (3.11/3.12 recommended).
3. **Application root**: path to the folder that contains `passenger_wsgi.py`.
4. **Application URL**: `api.yourdomain.com` (or subpath if you prefer).
5. **Application startup file**: `passenger_wsgi.py` (Passenger looks for `application` inside it—already defined).
6. Create / note the **virtual environment** path the wizard gives you.

## 4. Install dependencies

Use the **Run Pip Install** field if available, or SSH:

```bash
cd /home/USER/path/to/app
source ./venv/bin/activate   # use the path cPanel shows for this app
pip install -r requirements.txt
```

Then **Restart** the Python application in cPanel.

## 5. Environment variables (optional)

In **Setup Python App**, add variables if needed:

| Variable | Example |
|----------|---------|
| `DRAMAS_JSON_PATH` | `/home/user/api/dramas.json` |
| `DATABASE_PATH` | `/home/user/api/data/catalog.db` |
| `CORS_ORIGINS` | `https://yourmovieapp.com` or `*` for testing |
| `FORCE_RELOAD_DB` | `1` once after replacing `dramas.json`, then remove |

## 6. Verify

Open:

- `https://api.yourdomain.com/health` → `{"status":"ok"}`
- `https://api.yourdomain.com/docs` → Swagger UI
- `https://api.yourdomain.com/api/v1/stats` → counts

## Troubleshooting

- **500 / Passenger error**: Check **error log** in cPanel (Errors / Metrics, or `~/logs/`). Often missing `pip install`, wrong **application root**, or `dramas.json` path.
- **SQLite not writable**: Create `data/`, chmod **755** or **775**, owned by the account user.
- **Timeout on first request**: First boot imports JSON into SQLite and can take **one to several minutes**; later requests are fast.
- **No Python app in cPanel**: Hosting may be PHP-only; use a VPS, Docker, or a provider that supports Python/Passenger.

## Reverse proxy note

If your host does **not** use Passenger but allows **custom Node/Python** with a **port** and **nginx/Apache proxy**, run:

```bash
python -m uvicorn app.main:app --host 127.0.0.1 --port 8000
```

and configure the proxy to that port (only if your plan allows long‑running processes and port binding).
