# 🌐 cPanel Hosting Setup for ELMIS Backup System

## 📋 Prerequisites

- cPanel hosting account with PHP 8.1+ support
- SSH access (recommended) or File Manager access
- MySQL database access
- Email functionality enabled

## 🚀 Step 1: Upload Laravel Application

### Option A: Using File Manager
1. **Compress your project** locally (exclude `node_modules`, `.git`)
2. **Upload** to `public_html/` or subdomain folder
3. **Extract** the files
4. **Move** contents of `public` folder to `public_html/`
5. **Update** `index.php` to point to correct Laravel paths

### Option B: Using SSH (Recommended)
```bash
# Upload via Git or FTP
cd public_html
git clone your-repository.git .

# Install dependencies
composer install --optimize-autoloader --no-dev

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

## 🔧 Step 2: Configure Environment

### Update .env for cPanel
```env
# Application
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com

# Database (from cPanel MySQL)
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=yourusername_elmisdb
DB_USERNAME=yourusername_elmisuser
DB_PASSWORD=your_database_password

# Backup Configuration
BACKUP_NAME="ELMIS-Production"
BACKUP_STORAGE=local
BACKUP_DISK=backup_local
BACKUP_MAIL_TO=mkh.bm.edu@gmail.com
BACKUP_FILENAME_PREFIX="elmis-prod-"

# Mail Configuration (cPanel SMTP)
MAIL_MAILER=smtp
MAIL_HOST=mail.yourdomain.com
MAIL_PORT=587
MAIL_USERNAME=noreply@yourdomain.com
MAIL_PASSWORD=your_email_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=noreply@yourdomain.com
MAIL_FROM_NAME="ELMIS Backup System"

# File Paths (adjust for cPanel)
BACKUP_PATH=/home/yourusername/backups
```

## ⏰ Step 3: cPanel Cron Job Configuration

### Access Cron Jobs in cPanel
1. **Login** to cPanel
2. **Find** "Cron Jobs" in Advanced section
3. **Click** "Cron Jobs"

### Add Backup Cron Job

**Cron Expression for 12:00 AM (Midnight):**
```
0 0 * * *
```

**Command for cPanel:**
```bash
/usr/local/bin/php /home/yourusername/public_html/artisan backup:database --notify --cleanup >> /home/yourusername/logs/backup.log 2>&1
```

### Complete Cron Job Setup

| Field | Value |
|-------|-------|
| **Minute** | `0` |
| **Hour** | `0` |
| **Day** | `*` |
| **Month** | `*` |
| **Weekday** | `*` |
| **Command** | `/usr/local/bin/php /home/yourusername/public_html/artisan backup:database --notify --cleanup >> /home/yourusername/logs/backup.log 2>&1` |

## 📁 Step 4: Create Backup Directory

### Using File Manager
1. **Navigate** to home directory (`/home/yourusername/`)
2. **Create** folder: `backups`
3. **Set permissions** to `755`

### Using SSH
```bash
mkdir -p /home/yourusername/backups
mkdir -p /home/yourusername/logs
chmod 755 /home/yourusername/backups
chmod 755 /home/yourusername/logs
```

## 🔧 Step 5: Update Laravel Configuration for cPanel

### Update config/backup-disks.php
```php
'backup_local' => [
    'driver' => 'local',
    'root' => env('BACKUP_PATH', '/home/' . get_current_user() . '/backups'),
    'throw' => false,
],
```

### Test PHP Path
Create test file to find correct PHP path:
```php
<?php
// Create: /home/yourusername/public_html/test-php.php
echo "PHP Path: " . PHP_BINARY . "\n";
echo "Current User: " . get_current_user() . "\n";
echo "Home Directory: " . $_SERVER['HOME'] ?? 'Not set' . "\n";
?>
```

Visit: `https://yourdomain.com/test-php.php`

## 🧪 Step 6: Test the Setup

### Manual Test via SSH
```bash
# Test backup command
cd /home/yourusername/public_html
/usr/local/bin/php artisan backup:database --notify

# Test cron command
/usr/local/bin/php /home/yourusername/public_html/artisan backup:test
```

### Check Logs
```bash
# View backup logs
tail -f /home/yourusername/logs/backup.log

# View Laravel logs
tail -f /home/yourusername/public_html/storage/logs/laravel.log
```

## 📧 Step 7: Email Configuration

### Option A: cPanel Email
```env
MAIL_MAILER=smtp
MAIL_HOST=mail.yourdomain.com
MAIL_PORT=587
MAIL_USERNAME=backup@yourdomain.com
MAIL_PASSWORD=your_email_password
MAIL_ENCRYPTION=tls
```

### Option B: Gmail SMTP
```env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-gmail@gmail.com
MAIL_PASSWORD=your-app-password
MAIL_ENCRYPTION=tls
```

## 🔄 Step 8: Multiple Cron Jobs (Recommended)

### Daily Database Backup (12:00 AM)
```
0 0 * * * /usr/local/bin/php /home/yourusername/public_html/artisan backup:database --notify --cleanup >> /home/yourusername/logs/backup.log 2>&1
```

### Weekly Full Backup (Sunday 2:00 AM)
```
0 2 * * 0 /usr/local/bin/php /home/yourusername/public_html/artisan backup:run >> /home/yourusername/logs/full-backup.log 2>&1
```

### Daily Health Check (3:00 AM)
```
0 3 * * * /usr/local/bin/php /home/yourusername/public_html/artisan backup:monitor >> /home/yourusername/logs/health.log 2>&1
```

### Weekly Cleanup (Sunday 4:00 AM)
```
0 4 * * 0 /usr/local/bin/php /home/yourusername/public_html/artisan backup:clean >> /home/yourusername/logs/cleanup.log 2>&1
```

## 🛡️ Step 9: Security Considerations

### File Permissions
```bash
# Laravel directories
chmod -R 755 storage/
chmod -R 755 bootstrap/cache/
chmod 644 .env

# Backup directory
chmod 755 /home/yourusername/backups/
chmod 644 /home/yourusername/backups/*.sql.gz
```

### Hide Sensitive Files
Create `.htaccess` in root directory:
```apache
# Deny access to sensitive files
<Files ".env">
    Order allow,deny
    Deny from all
</Files>

<Files "*.log">
    Order allow,deny
    Deny from all
</Files>
```

## 📊 Step 10: Monitoring & Troubleshooting

### Check Cron Execution
```bash
# View cron logs (may vary by host)
tail -f /var/log/cron
grep CRON /var/log/syslog
```

### Common Issues & Solutions

**Issue**: Command not found
**Solution**: Use full PHP path: `/usr/local/bin/php` or `/usr/bin/php`

**Issue**: Permission denied
**Solution**: Check file permissions and ownership

**Issue**: Artisan not found
**Solution**: Use absolute path to artisan file

**Issue**: Database connection failed
**Solution**: Verify cPanel database credentials

**Issue**: Email not sending
**Solution**: Check cPanel email configuration and limits

### Verification Script
Create monitoring script:
```php
<?php
// /home/yourusername/public_html/backup-status.php
require_once 'bootstrap/app.php';

$app = require_once 'bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

$status = $kernel->call('backup:status');
echo "Backup Status: " . $status . "\n";
?>
```

## 🎯 Production Checklist

- [ ] Laravel application uploaded and configured
- [ ] Database credentials updated in .env
- [ ] Backup directories created with proper permissions
- [ ] PHP path verified for cron jobs
- [ ] Email configuration tested
- [ ] Cron jobs added in cPanel
- [ ] Manual backup test successful
- [ ] Log files accessible and writable
- [ ] Security settings applied
- [ ] Monitoring system in place

## 📞 Support Commands

```bash
# Test backup system
php artisan backup:test

# Manual backup
php artisan backup:database --notify

# Check backup status
php artisan backup:status

# View recent logs
tail -20 /home/yourusername/logs/backup.log
```
