# Fix Your Current Deployment Structure

You've uploaded everything to the server. Now let's reorganize it properly.

## Step 1: Check Current Structure

Login to cPanel File Manager and see what you have in `public_html`. You probably have something like:

**Current (Wrong):**
```
public_html/
├── backend/
│   └── (all backend files)
└── frontend/
    └── dist/
        └── spa/
            └── index.html
```

OR

```
public_html/
├── dist/
│   └── spa/
│       └── index.html
└── backend/
```

We need to fix this!

---

## Step 2: Reorganize Files (Using cPanel File Manager)

### A. Fix Frontend First

1. **Navigate into the wrong location:**
   - If you have `public_html/frontend/dist/spa/`
   - Go inside the `spa` folder
   
2. **Select all files in spa folder:**
   - Click checkbox for: index.html, css, js, fonts, icons, etc.
   - Right-click → Move
   
3. **Move to root:**
   - Move them to `/public_html/`
   - Confirm move
   
4. **Delete empty folders:**
   - Delete the now-empty `dist` and `frontend` folders

**OR if you have `public_html/dist/spa/`:**
1. Go inside `spa` folder
2. Select all files
3. Move to `/public_html/`
4. Delete empty `dist` and `spa` folders

### B. Move Backend Outside public_html

1. **Go up one level** from `public_html` (click the up arrow)
   - You should see your home directory (like `/home/username/`)

2. **Create a new folder:**
   - Click "New Folder"
   - Name it: `laravel-backend` or `backend-api`

3. **Move backend files:**
   - Go back into `public_html`
   - Find the `backend` folder
   - Right-click → Move
   - Move to `/home/username/laravel-backend/`

4. **Move Laravel's public folder:**
   - Go to `/home/username/laravel-backend/public/`
   - Select ALL files inside (index.php, .htaccess, etc.)
   - Move them to `/public_html/api/` (create this folder first)

---

## Step 3: Fix Laravel's index.php

1. **Open file**: `/public_html/api/index.php`

2. **Find these lines** (around line 16-17):
   ```php
   require __DIR__.'/../vendor/autoload.php';
   $app = require_once __DIR__.'/../bootstrap/app.php';
   ```

3. **Change to:**
   ```php
   require __DIR__.'/../../laravel-backend/vendor/autoload.php';
   $app = require_once __DIR__.'/../../laravel-backend/bootstrap/app.php';
   ```

4. **Save the file**

---

## Step 4: Configure Backend .env

1. **Open**: `/home/username/laravel-backend/.env`

2. **Update these settings:**
   ```env
   APP_ENV=production
   APP_DEBUG=false
   APP_URL=https://yourdomain.com
   FRONTEND_URL=https://yourdomain.com
   
   DB_CONNECTION=mysql
   DB_HOST=localhost
   DB_DATABASE=your_database_name
   DB_USERNAME=your_database_user
   DB_PASSWORD=your_database_password
   
   SESSION_DOMAIN=yourdomain.com
   SANCTUM_STATEFUL_DOMAINS=yourdomain.com
   ```

3. **Get database credentials** from cPanel → MySQL Databases
   - Database name
   - Database user
   - Database password

---

## Step 5: Run Backend Setup (SSH or Terminal in cPanel)

If you have SSH access:

```bash
# Navigate to backend
cd /home/username/laravel-backend

# Install dependencies (if not already done)
composer install --optimize-autoloader --no-dev

# Generate key (if APP_KEY is empty)
php artisan key:generate

# Run migrations
php artisan migrate --force

# Clear and cache config
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Fix permissions
chmod -R 755 storage bootstrap/cache
```

**No SSH?** Use cPanel Terminal or ask your host to run these commands.

---

## Step 6: Create .htaccess in public_html

1. **Create new file**: `/public_html/.htaccess`

2. **Add this content:**
   ```apache
   <IfModule mod_rewrite.c>
       RewriteEngine On
       
       # Redirect API requests to Laravel backend
       RewriteCond %{REQUEST_URI} ^/api/
       RewriteRule ^api/(.*)$ /api/index.php [QSA,L]
       
       # Handle frontend SPA routing
       RewriteRule ^index\.html$ - [L]
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule . /index.html [L]
   </IfModule>
   ```

---

## Step 7: Update Frontend API URL

Your frontend needs to know where the backend is.

**Find your API configuration:**
- Usually in `frontend/src/boot/axios.js`
- Or `frontend/src/config/api.js`
- Or environment variable

**Update to:**
```javascript
axios.defaults.baseURL = 'https://yourdomain.com/api'
```

**Then rebuild and re-upload:**
```bash
cd frontend
npm run build
```

Upload contents of `dist/spa/` to `public_html` again.

---

## Final Structure (Target)

```
/home/username/
│
├── public_html/              ← Your website root
│   ├── index.html           ← Frontend (direct)
│   ├── css/                 ← Frontend assets
│   ├── js/                  ← Frontend assets  
│   ├── fonts/
│   ├── icons/
│   ├── .htaccess            ← Routing rules
│   │
│   └── api/                 ← Laravel public folder
│       ├── index.php        ← Fixed paths
│       └── .htaccess
│
└── laravel-backend/         ← Backend (hidden)
    ├── app/
    ├── bootstrap/
    ├── config/
    ├── database/
    ├── .env                 ← Production config
    ├── vendor/
    └── storage/
```

---

## Quick Verification

### ✅ Check Frontend:
1. Visit: `https://yourdomain.com`
2. Should load your Vue/Quasar app
3. Check browser console (F12) - no major errors

### ✅ Check Backend:
1. Visit: `https://yourdomain.com/api/`
2. Should see Laravel response (might be 404 but from Laravel)
3. Try: `https://yourdomain.com/api/users` or another route

### ✅ Check Files:
- `public_html/index.html` exists (✓)
- `public_html/api/index.php` exists (✓)
- `laravel-backend/.env` configured (✓)
- No `frontend/` or `dist/` folders in public_html (✓)

---

## Common Issues After Fix

### 500 Error on Backend
```bash
# Fix permissions
cd /home/username/laravel-backend
chmod -R 755 storage bootstrap/cache
```

### Frontend Can't Connect to API
- Check if API URL is correct in frontend code
- Rebuild frontend after changing API URL
- Check browser console for exact error

### Database Connection Error
- Verify `.env` database credentials
- Create database in cPanel if it doesn't exist
- Run migrations: `php artisan migrate --force`

### Laravel Routes Not Working
```bash
cd /home/username/laravel-backend
php artisan route:clear
php artisan config:clear
php artisan cache:clear
```

---

## Need Help?

If you get stuck, tell me:
1. What error you see
2. What URL shows the error
3. Your current folder structure in public_html

I'll help you fix it!
