# cPanel Deployment Guide

## Pre-Deployment Checklist

### Local Testing (Critical - Do This First!)

Before uploading to cPanel, test everything on localhost:

```
1. Admin Panel Tests:
   ✓ Navigate to http://localhost/emporio-sport/admin_login.php
   ✓ Login with admin credentials
   ✓ Test Products:
     - Add new product → Verify it saves
     - Edit product → Verify changes save
     - Delete product → Verify it's removed
   ✓ Test Carousel:
     - Add carousel item with FILE UPLOAD → Verify image uploads
     - Edit carousel item with URL → Verify URL saves
   ✓ Test Site Config:
     - Change site title → Verify it saves
     - Add social media link → Verify it saves
   ✓ Test Categories:
     - Add category → Verify it saves
   ✓ Refresh page → ALL DATA SHOULD PERSIST
   ✓ Check browser console → No JavaScript errors
   ✓ Check PHP error log → No PHP errors

2. Run Verification Script:
   - Navigate to http://localhost/emporio-sport/test_deployment.php
   - Verify all tests pass (should show ✓ for all items)
   - If any tests fail, DO NOT proceed to cPanel
```

---

## Step-by-Step cPanel Deployment

### Step 1: Prepare Files for Upload

1. Remove test files (optional):
   ```
   - test_deployment.php (if you don't want it on production)
   - Any .backup.json files (if space is critical)
   ```

2. Keep these critical files:
   ```
   ✓ helpers/database_helper.php (REQUIRED - core functionality)
   ✓ admin_*.php files (all updated with DatabaseHelper)
   ✓ data/ directory (or let cPanel create it)
   ```

### Step 2: Connect to cPanel

**Using FTP/SFTP (Recommended):**

1. Get your cPanel FTP credentials:
   - Host: `ftp.yourdomain.com` or `your-cpanel-ip`
   - Username: Usually `username@yourdomain.com` or cPanel username
   - Password: Your cPanel password
   - Port: 21 (FTP) or 22 (SFTP)

2. Use an FTP client (FileZilla, WinSCP, etc.):
   - Connect to your cPanel server
   - Navigate to `/home/username/public_html/` (your website root)
   - Or wherever your domain is set up

**Using cPanel File Manager:**

1. Login to cPanel
2. Go to File Manager
3. Navigate to your domain's public_html folder
4. Right-click → Upload Files

### Step 3: Upload Project Files

1. Upload all files from `c:\xampp\htdocs\emporio-sport\` to your cPanel directory

2. Make sure to upload:
   ```
   ✓ helpers/ folder (with database_helper.php)
   ✓ All admin_*.php files
   ✓ All public pages (shop.php, blog.php, etc.)
   ✓ All configuration files
   ✓ data/ folder (if it exists locally - OR create it in cPanel)
   ```

3. Do NOT upload:
   ```
   ✗ xampp/ or system directories
   ✗ .git folders
   ✗ Development notes files
   ```

### Step 4: Set Up Directories and Permissions

Once files are uploaded to cPanel:

1. **Create data directory (if not exists):**
   ```bash
   # Via SSH (if available)
   mkdir -p ~/public_html/data
   mkdir -p ~/public_html/uploads
   
   # Or use cPanel File Manager:
   # Right-click in public_html → Create New Folder → "data"
   # Right-click in public_html → Create New Folder → "uploads"
   ```

2. **Set Directory Permissions:**
   ```bash
   # Via SSH:
   chmod 755 ~/public_html/data
   chmod 755 ~/public_html/uploads
   chmod 644 ~/public_html/data/*.json
   chmod 644 ~/public_html/uploads/*
   
   # Or via cPanel File Manager:
   # Right-click folder → Change Permissions → Set to 755
   ```

### Step 5: Verify Installation

1. **Run deployment verification:**
   ```
   Open: yourdomain.com/test_deployment.php
   All tests should show ✓ PASS
   ```

2. **Test admin panel:**
   ```
   Navigate to: yourdomain.com/admin_login.php
   Login with admin credentials
   Test adding/editing a product
   Verify it saves and persists
   ```

3. **Test website:**
   ```
   Navigate to: yourdomain.com/shop.php
   Verify products display
   Navigate to: yourdomain.com/
   Verify all pages load correctly
   ```

### Step 6: Configure if Needed

If you have cPanel-specific settings:

1. **Check database connection** (if using database instead of JSON):
   ```
   Update connection strings in:
   - db_config.php (if using)
   - Any database-related files
   ```

2. **Update email settings:**
   ```
   In: api_contact_submit.php, api_feedback_submit.php
   Update email addresses if needed
   ```

3. **SSL/HTTPS:**
   ```
   Access cPanel → SSL/TLS
   Install SSL certificate
   Update shop.php and cart.php to use https://
   ```

---

## Troubleshooting

### Problem: "Error saving data" message in admin

**Solution:**
1. Check `data/` directory exists and is writable (755 permissions)
2. Check PHP error log in cPanel:
   - cPanel → Metrics → Error log
   - Look for DatabaseHelper errors
3. Ensure `.json` files have correct permissions (644)

### Problem: Products not saving

**Solution:**
1. Verify `/data` directory exists
2. Check directory is writable: `chmod 755 data/`
3. Check file permissions: `chmod 644 data/*.json`
4. Try running test_deployment.php to identify exact issue

### Problem: File uploads not working

**Solution:**
1. Create `/uploads` directory if doesn't exist
2. Set permissions: `chmod 755 uploads/`
3. Check PHP upload_max_filesize setting:
   ```
   cPanel → Select PHP Version → Options
   Look for upload_max_filesize (should be ≥ 5MB)
   ```

### Problem: "Permission denied" errors

**Solution:**
1. Ensure you're using correct permissions:
   ```bash
   # Directories: 755 (rwxr-xr-x)
   chmod 755 data
   chmod 755 uploads
   
   # Files: 644 (rw-r--r--)
   chmod 644 data/*.json
   chmod 644 *.php
   ```

2. Check if running as correct user:
   ```bash
   # Files should be owned by your cPanel user
   # Not root or another user
   ```

### Problem: "Data directory not found"

**Solution:**
1. DatabaseHelper will try to create it automatically
2. If that fails, create manually in cPanel File Manager
3. Ensure directory is in the right location (same as .php files)

---

## Post-Deployment Verification

After deployment, verify everything works:

### Admin Panel Tests:
```
✓ Add a new product
✓ Edit an existing product
✓ Delete a product
✓ Upload carousel image via file
✓ Add social media link
✓ Change site title
✓ Refresh page → data persists
```

### Frontend Tests:
```
✓ Shop page loads products
✓ Product details page loads correctly
✓ Add to cart works
✓ Checkout works
✓ Contact form works
✓ Blog pages load
```

### File System Checks:
```
✓ /data directory exists with 755 permissions
✓ /uploads directory exists with 755 permissions
✓ .json files have 644 permissions
✓ .php files have 644 permissions
```

---

## Backup Before Next Changes

Once everything is working on cPanel:

1. **Create a backup:**
   ```bash
   # Via SSH (if available):
   tar -czf ~/backup_working.tar.gz ~/public_html/
   
   # Or use cPanel Backup Tool
   ```

2. **Save database files locally:**
   ```
   Download /data/*.json files
   Download /uploads/* files
   Store in safe location
   ```

3. **Document any customizations:**
   ```
   Note any cPanel-specific paths or settings
   Keep record of database changes
   Document any custom configurations
   ```

---

## Environment Differences: Localhost vs cPanel

The DatabaseHelper automatically handles path differences:

| Item | Localhost | cPanel |
|------|-----------|--------|
| Base Path | `C:\xampp\htdocs\emporio-sport` | `/home/username/public_html` |
| Data Dir | `C:\xampp\htdocs\emporio-sport\data` | `/home/username/public_html/data` |
| Uploads Dir | `C:\xampp\htdocs\emporio-sport\uploads` | `/home/username/public_html/uploads` |
| DatabaseHelper | ✓ Works Identically | ✓ Works Identically |

**No code changes needed!** The DatabaseHelper uses `__DIR__` which automatically adapts to both environments.

---

## Key Features on cPanel

### Automatic Backup System:
```
Every time you save:
- Original file backed up as .backup.json
- New data saved safely
- If something goes wrong, you can restore
```

### Error Logging:
```
All operations logged to:
cPanel → Metrics → Error log
- Track all saves
- Identify issues
- Useful for debugging
```

### Atomic Writes:
```
Data saved safely in steps:
1. Write to temporary file
2. Validate JSON
3. Rename to final file
Prevents: data corruption, partial saves, file locks
```

---

## Quick Reference Commands

If using SSH access to cPanel:

```bash
# List files
ls -la ~/public_html/data/

# Check directory permissions
stat ~/public_html/data

# Set permissions
chmod 755 ~/public_html/data
chmod 644 ~/public_html/data/*.json

# Check PHP version
php --version

# Test PHP file
php ~/public_html/test_deployment.php

# Check error log
tail -f ~/public_html/error_log

# Create backup
tar -czf ~/backup.tar.gz ~/public_html/
```

---

## Support Resources

If you encounter issues:

1. **Check cPanel Error Log:**
   - cPanel → Metrics → Error log
   - Search for "DatabaseHelper" or "emporio-sport"

2. **Check PHP Configuration:**
   - cPanel → Select PHP Version → Options
   - Verify upload_max_filesize ≥ 5MB
   - Verify max_execution_time ≥ 30

3. **Test Script:**
   - Run: yourdomain.com/test_deployment.php
   - This identifies what's working and what's not

4. **Common Issues:**
   - Missing /data directory → create it
   - Wrong permissions → use chmod 755
   - File uploads fail → check uploads directory
   - Data not saving → check error log

---

## Success Indicators

You've successfully deployed when:

✅ Admin login works
✅ Can add/edit/delete products
✅ Can upload carousel images
✅ Can change site settings
✅ All data persists after refresh
✅ test_deployment.php shows all tests passing
✅ Frontend displays products correctly
✅ No errors in PHP error log

---

## Next Steps

After successful deployment:

1. Update DNS if needed
2. Set up SSL/TLS certificate
3. Configure email for contact forms
4. Update social media links
5. Add real product images
6. Create promotional content
7. Set up analytics
8. Test thoroughly from customer perspective

---

**Status:** Ready for production deployment ✓

All files have been updated with DatabaseHelper for cross-platform compatibility.
The system is tested and ready for cPanel.
