# Deploying to GoDaddy / cPanel Shared Hosting

## Environment

- Python 3.9+ (system `/usr/bin/python3`)
- MySQL (cPanel)
- Packages installed via `pip3 install --user`
- Django 4.2 LTS

## 1. Create MySQL Database (cPanel)

cPanel → **MySQL Databases**:

1. Create a database (cPanel will prefix it with your account name, e.g. `cpaneluser_appdb`).
2. Create a database user with a strong password (e.g. `cpaneluser_appuser`).
3. Add the user to the database with **ALL PRIVILEGES**.

Note the final prefixed names — you'll need them in `.env`.

## 2. Upload the code

**Option A — Git (recommended):**

```bash
ssh user@yourhost
cd ~
git clone https://github.com/YOUR_USER/YOUR_REPO.git app
```

**Option B — File Manager:**

Upload a zip and extract to `/home/<cpaneluser>/app/` so `manage.py` lives at
`/home/<cpaneluser>/app/manage.py`.

## 3. Configure environment

```bash
cd ~/app
cp .env.cpanel .env
nano .env
```

Fill in:

```
SECRET_KEY=<python3 -c "import secrets; print(secrets.token_urlsafe(64))">
DEBUG=False
ALLOWED_HOSTS=example.com,www.example.com

DB_NAME=cpaneluser_appdb
DB_USER=cpaneluser_appuser
DB_PASSWORD=<your password>
DB_HOST=localhost
DB_PORT=3306

CORS_ALLOWED_ORIGINS=https://example.com,https://www.example.com
```

Lock down the .env file:

```bash
chmod 600 .env
```

## 4. Update paths

Edit `.htaccess` and set `PassengerAppRoot` to your actual app path
(e.g. `/home/cpaneluser/app`).

## 5. Install + migrate

```bash
cd ~/app
bash deploy_cpanel.sh
```

## 6. Point the domain to the app

The app needs to live at the document root for your domain.

In cPanel → **Domains**, check the document root for your domain.
Usually `/home/<cpaneluser>/public_html`.

**Option A — Symlink (cleanest):**
```bash
rm -rf ~/public_html    # back up first if needed
ln -s ~/app ~/public_html
```

**Option B — Change document root:**

cPanel → **Domains** → set document root to `/home/<cpaneluser>/app`.

## 7. Verify .htaccess is active

The `.htaccess` in the project root tells Apache to route requests to
`passenger_wsgi.py`. If Passenger isn't available, the RewriteRule fallback
routes through `dispatch.cgi`.

If you get a 500 error, check `~/logs/error.log` or cPanel → **Errors**.

## 8. Create superuser

```bash
cd ~/app
python3 manage.py createsuperuser
```

## 9. Test

- `https://example.com/api/v1/health/` → `{"status": "ok"}`
- `https://example.com/admin/` → Django admin login

## Troubleshooting

- **500 Internal Server Error**: check `~/logs/error.log`. Usually a missing
  package or wrong path in `.htaccess`.
- **Module not found**: re-run `pip3 install --user -r requirements.txt`.
- **Static files 404**: `python3 manage.py collectstatic --noinput`.
- **Changes not visible**: `touch ~/app/tmp/restart.txt`.
- **Passenger not recognized**: GoDaddy may not have mod_passenger. The
  `.htaccess` fallback uses `dispatch.cgi` — make sure `dispatch.cgi` is
  executable (`chmod +x dispatch.cgi`) and its shebang matches the server's
  Python path.

## Future deploys

```bash
cd ~/app
git pull origin main
bash deploy_cpanel.sh
```
