# Namecheap (cPanel) deployment

## One command (build + zip)

Run **from the `laravel-react-app` folder** on your dev machine (PHP + Composer + Node required):

```powershell
powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\deploy-namecheap-zip.ps1
```

This will:

1. Run `npm run build` in `frontend/` (outputs to `public/app/`).
2. Run `composer install --no-dev --optimize-autoloader`.
3. Create **`bitwebai-laravel-namecheap.zip`** in the project root (excludes `.git`, `node_modules`, `.env`, `tests`, etc.).

Upload **`bitwebai-laravel-namecheap.zip`** via cPanel **File Manager** (or SFTP), then continue below.

---

## cPanel: MySQL database

1. Open **cPanel** → **MySQL® Databases** (or **MySQL Database Wizard**).
2. Create a **new database** (e.g. `username_bitwebai`). Note the **full name** (often prefixed with your cPanel user).
3. Create a **new MySQL user** with a **strong password**. Note **username** and **password**.
4. Add the user to the database with **ALL PRIVILEGES**.
5. Note **host**: on many Namecheap shared plans it is **`localhost`** (use what cPanel shows next to the database user).

You will use these values in `.env` as:

- `DB_HOST=localhost` (or the value cPanel shows)
- `DB_PORT=3306`
- `DB_DATABASE=your_full_database_name`
- `DB_USERNAME=your_full_mysql_username`
- `DB_PASSWORD=your_password`

---

## Upload and folder layout

Typical shared hosting: your site’s web root is `public_html` (or a subdomain folder).

**Option A — Document root points at Laravel `public` (recommended)**  
Upload and extract the zip **one level above** `public_html`, so the tree looks like:

```text
/home/youruser/
  laravel-react-app/        ← app root (contains app/, bootstrap/, vendor/, .env, …)
    public/                 ← Laravel public (index.php, app/)
```

In cPanel → **Domains** (or **Subdomains**), set the domain’s **document root** to:

```text
laravel-react-app/public
```

**Option B — Everything under `public_html`**  
Extract into `public_html` so `public_html/public` exists, then set document root to `public_html/public` the same way.

Do **not** leave `public_html` pointing only at `public/` contents without the parent Laravel folders; the app needs `../vendor`, `../storage`, etc. relative to `public/index.php`.

---

## Environment file on the server

1. In the **application root** (same folder as `artisan`), copy the example env:

   ```bash
   cp .env.example .env
   ```

2. Edit `.env` in File Manager or SSH:

   - `APP_ENV=production`
   - `APP_DEBUG=false`
   - `APP_URL=https://yourdomain.com` (your real HTTPS URL)
   - `DB_CONNECTION=mysql`
   - Fill **`DB_HOST`**, **`DB_DATABASE`**, **`DB_USERNAME`**, **`DB_PASSWORD`** from the database section above.

3. Generate key (SSH or cPanel **Terminal**):

   ```bash
   php artisan key:generate
   ```

---

## PHP version

In cPanel → **Select PHP Version** (or **MultiPHP Manager**), choose **PHP 8.2** or newer (match `composer.json`).

Enable extensions Laravel needs, commonly: `openssl`, `pdo`, `mbstring`, `tokenizer`, `xml`, `ctype`, `json`, `bcmath`, `fileinfo` (and `pdo_mysql` for MySQL).

---

## Permissions

From SSH in the application root:

```bash
chmod -R 775 storage bootstrap/cache
```

Ensure the web server user can write to `storage/` and `bootstrap/cache/`.

---

## Migrate and optimize

In the **application root**:

```bash
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

If queues use the database driver, ensure the `jobs` table exists (migrations) and configure **Cron** if you use the scheduler (optional for a minimal API + SPA).

---

## HTTPS

Use **SSL/TLS** in cPanel (Let’s Encrypt). Set `APP_URL` to `https://...` and consider `SESSION_SECURE_COOKIE=true` when everything is HTTPS.

---

## Troubleshooting

| Issue | What to check |
|--------|----------------|
| 503 “Frontend build not found” | Run `npm run build` in `frontend/` locally, re-zip, redeploy; confirm `public/app/index.html` exists on the server. |
| 500 / white screen | `storage/logs/laravel.log`, `APP_DEBUG=true` briefly (then false), permissions on `storage` / `bootstrap/cache`. |
| Database connection | `DB_*` in `.env`, user attached to DB with privileges, host `localhost` vs remote. |
| API 404 | `php artisan route:cache` after deploy; document root must be Laravel `public/`. |

---

## Security

- Never commit or upload `.env` from your machine into a public repo; create `.env` only on the server.
- Keep `APP_DEBUG=false` in production.
