# Numerix Pro — cPanel Deployment Guide

## Prerequisites

- cPanel hosting with PHP 8.3+
- MySQL 8.0+
- Composer access (via SSH or cPanel Terminal)
- Node.js 18+ on your dev machine (to build React)

---

## Step 1: Build the React Frontend

On your **local machine**:

```bash
# From the monorepo root
cd /path/to/numerix-pro-monorepo

# Set your production API URL
echo "VITE_API_BASE_URL=https://api.yourdomain.com" > artifacts/numerix-pro/.env.production

# Build
pnpm --filter @workspace/numerix-pro run build

# The built files are in: artifacts/numerix-pro/dist/
```

Upload the **contents of `artifacts/numerix-pro/dist/`** to your cPanel public_html (or a subdomain folder).

---

## Step 2: Deploy the Laravel Backend

### Option A: Subdomain API (Recommended)

1. Create a subdomain `api.yourdomain.com` pointing to `/home/user/api.yourdomain.com/`
2. Upload the `laravel-backend/` directory to `/home/user/api.yourdomain.com/`

### Option B: Subfolder `/api`

Upload to a subfolder and configure the document root to point to `laravel-backend/public/`.

---

## Step 3: Configure the Environment

```bash
# SSH into your server
ssh user@yourdomain.com

cd ~/api.yourdomain.com

# Copy and edit .env
cp .env.example .env
nano .env
```

Set these values in `.env`:

```env
APP_ENV=production
APP_DEBUG=false
APP_URL=https://api.yourdomain.com

DB_HOST=localhost
DB_DATABASE=cpanel_db_name
DB_USERNAME=cpanel_db_user
DB_PASSWORD=your_db_password

FRONTEND_URL=https://yourdomain.com

SUPER_ADMIN_EMAIL=admin@yourdomain.com
SUPER_ADMIN_PASSWORD=strong_password_here
```

---

## Step 4: Install Dependencies & Set Up

```bash
# Install Composer dependencies
composer install --optimize-autoloader --no-dev

# Generate app key
php artisan key:generate

# Run migrations
php artisan migrate --force

# Seed the database (creates super admin and roles)
php artisan db:seed --force

# Cache config for performance
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Set storage permissions
chmod -R 775 storage bootstrap/cache
```

---

## Step 5: Configure cPanel Document Root

In cPanel → Domains → yourdomain.com → Document Root:
- Set to: `/home/user/api.yourdomain.com/public`

Or in `public/.htaccess` (already included in Laravel):
```apache
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
```

---

## Step 6: Set Up the Queue Worker (for file processing)

### Option A: cPanel Cron Job (simple, for low volume)

Add in cPanel → Cron Jobs, run every minute:
```bash
cd /home/user/api.yourdomain.com && php artisan queue:work --stop-when-empty --timeout=3600 >> /dev/null 2>&1
```

### Option B: Background daemon with Supervisor (recommended for production)

```ini
[program:numerix-worker]
command=php /home/user/api.yourdomain.com/artisan queue:work database --queue=processing --tries=3 --timeout=3600
autostart=true
autorestart=true
user=your_unix_user
redirect_stderr=true
stdout_logfile=/home/user/api.yourdomain.com/storage/logs/worker.log
```

---

## Step 7: CORS Configuration

In `.env`, ensure:
```env
FRONTEND_URL=https://yourdomain.com
```

This is used in `config/cors.php` to whitelist your React app's origin.

---

## Step 8: First Login

After deployment:

1. Visit `https://yourdomain.com/login`
2. Log in with super admin credentials set in `.env`
3. Change your password in Settings immediately

---

## Troubleshooting

| Problem | Solution |
|---------|----------|
| 500 error | Check `storage/logs/laravel.log` |
| CORS error | Verify `FRONTEND_URL` in `.env`, run `php artisan config:cache` |
| Queue not processing | Check cron job is running, check `failed_jobs` table |
| File upload fails | Check `storage/app/data/` is writable: `chmod 775 storage/app/data` |
| Slow processing | Upgrade to Supervisor for queue workers instead of cron |

---

## File Structure After Deployment

```
api.yourdomain.com/      ← Laravel backend
├── app/
├── bootstrap/
├── config/
├── database/
├── public/              ← Document root
│   └── index.php
├── routes/
│   └── api.php
├── storage/
│   └── app/data/        ← Uploaded files (auto-cleaned)
└── .env

yourdomain.com/          ← React frontend (built files)
├── index.html
├── assets/
└── .htaccess            ← React Router SPA redirect
```

### React SPA `.htaccess` (upload to public_html)

```apache
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
```
