# 🚀 Trade Plus Education - Deployment Guide

This guide will walk you through deploying the Trade Plus Education website to a production server.

## 📋 Prerequisites

-   **Server**: VPS or Shared Hosting with SSH access
-   **OS**: Ubuntu 20.04+ or similar Linux distribution
-   **PHP**: 8.2 or higher
-   **MySQL**: 5.7 or higher
-   **Web Server**: Apache or Nginx
-   **Composer**: Latest version
-   **Node.js**: v18+ and NPM

## 🌐 Server Requirements

### Minimum Server Specs

-   **RAM**: 2GB minimum (4GB recommended)
-   **Storage**: 20GB minimum
-   **CPU**: 2 cores minimum

### Required PHP Extensions

```bash
sudo apt install php8.2-cli php8.2-fpm php8.2-mysql php8.2-xml php8.2-mbstring php8.2-curl php8.2-zip php8.2-bcmath php8.2-intl php8.2-gd
```

## 📦 Step 1: Upload Files

### Option A: Git (Recommended)

```bash
cd /var/www/
git clone https://github.com/yourusername/tradeplusedu.git
cd tradeplusedu
```

### Option B: FTP/SFTP

Upload all files to `/var/www/tradeplusedu/` or your web root directory.

## 🔧 Step 2: Install Dependencies

```bash
# Navigate to project directory
cd /var/www/tradeplusedu

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

# Install Node dependencies and build assets
npm install
npm run build
```

## 🔐 Step 3: Environment Configuration

```bash
# Copy environment file
cp .env.production.example .env

# Generate application key
php artisan key:generate
```

Edit `.env` file:

```bash
nano .env
```

**Important Settings:**

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

DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=trade_plus_edu
DB_USERNAME=your_db_user
DB_PASSWORD=your_secure_password

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=info@tradeplusedu.com
MAIL_PASSWORD=your_app_password
```

## 🗄️ Step 4: Database Setup

### Create Database

```bash
mysql -u root -p
```

```sql
CREATE DATABASE trade_plus_edu CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'tradeplusedu'@'localhost' IDENTIFIED BY 'secure_password_here';
GRANT ALL PRIVILEGES ON trade_plus_edu.* TO 'tradeplusedu'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```

### Run Migrations

```bash
php artisan migrate --force
php artisan db:seed --force
```

## 🔗 Step 5: Storage Configuration

```bash
# Create symbolic link
php artisan storage:link

# Set proper permissions
chmod -R 775 storage bootstrap/cache
chown -R www-data:www-data storage bootstrap/cache
```

## ⚡ Step 6: Optimize for Production

```bash
# Cache configuration
php artisan config:cache

# Cache routes
php artisan route:cache

# Cache views
php artisan view:cache

# Optimize autoloader
composer dump-autoload --optimize
```

## 🌐 Step 7: Web Server Configuration

### Nginx Configuration

Create file: `/etc/nginx/sites-available/tradeplusedu.com`

```nginx
server {
    listen 80;
    server_name tradeplusedu.com www.tradeplusedu.com;
    root /var/www/tradeplusedu/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

Enable site:

```bash
sudo ln -s /etc/nginx/sites-available/tradeplusedu.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
```

### Apache Configuration

Create file: `/etc/apache2/sites-available/tradeplusedu.com.conf`

```apache
<VirtualHost *:80>
    ServerName tradeplusedu.com
    ServerAlias www.tradeplusedu.com
    DocumentRoot /var/www/tradeplusedu/public

    <Directory /var/www/tradeplusedu/public>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/tradeplusedu-error.log
    CustomLog ${APACHE_LOG_DIR}/tradeplusedu-access.log combined
</VirtualHost>
```

Enable site:

```bash
sudo a2ensite tradeplusedu.com.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
```

## 🔒 Step 8: SSL Certificate (HTTPS)

### Using Let's Encrypt (Free)

```bash
# Install Certbot
sudo apt install certbot python3-certbot-nginx

# For Nginx
sudo certbot --nginx -d tradeplusedu.com -d www.tradeplusedu.com

# For Apache
sudo certbot --apache -d tradeplusedu.com -d www.tradeplusedu.com
```

Auto-renewal test:

```bash
sudo certbot renew --dry-run
```

## 👤 Step 9: Change Admin Credentials

**IMPORTANT**: Change default admin password!

```bash
php artisan tinker
```

```php
$admin = User::where('email', 'admin@tradeplusedu.com')->first();
$admin->password = bcrypt('your_new_secure_password');
$admin->save();
exit
```

## 📧 Step 10: Email Configuration

### Using Gmail SMTP

1. Go to [Google Account Security](https://myaccount.google.com/security)
2. Enable 2-Step Verification
3. Generate App Password
4. Use App Password in `.env`:

```env
MAIL_USERNAME=info@tradeplusedu.com
MAIL_PASSWORD=your_16_character_app_password
```

### Test Email

```bash
php artisan tinker
Mail::raw('Test email', function($msg) {
    $msg->to('your-email@example.com')->subject('Test');
});
```

## 🔐 Step 11: Security Hardening

### 1. Firewall Setup

```bash
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
```

### 2. Hide Sensitive Files

Ensure `.env` is not web-accessible (should be above web root or protected by web server).

### 3. Regular Updates

```bash
# Set up automatic security updates
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
```

### 4. Database Backups

Create backup script: `/root/backup-db.sh`

```bash
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/root/backups"
mkdir -p $BACKUP_DIR

mysqldump -u tradeplusedu -p'password' trade_plus_edu > $BACKUP_DIR/db_backup_$DATE.sql
find $BACKUP_DIR -type f -mtime +7 -delete

echo "Backup completed: $DATE"
```

```bash
chmod +x /root/backup-db.sh

# Add to crontab (daily at 2 AM)
sudo crontab -e
0 2 * * * /root/backup-db.sh >> /var/log/db-backup.log 2>&1
```

## 📊 Step 12: Monitoring & Logs

### View Logs

```bash
# Application logs
tail -f storage/logs/laravel.log

# Nginx logs
tail -f /var/log/nginx/error.log

# Apache logs
tail -f /var/log/apache2/error.log
```

### Set up Log Rotation

Create: `/etc/logrotate.d/tradeplusedu`

```
/var/www/tradeplusedu/storage/logs/*.log {
    daily
    missingok
    rotate 14
    compress
    notifempty
    create 0644 www-data www-data
}
```

## ✅ Step 13: Post-Deployment Checklist

-   [ ] Database migrated successfully
-   [ ] Admin user created and password changed
-   [ ] All pages loading correctly
-   [ ] Forms submitting properly (test Apply Now and Contact)
-   [ ] Email notifications working
-   [ ] SSL certificate installed
-   [ ] WhatsApp button linking correctly
-   [ ] Images uploading and displaying
-   [ ] Admin panel accessible at /admin
-   [ ] Google Analytics/Facebook Pixel configured (optional)
-   [ ] Backup system in place
-   [ ] Error pages customized (404, 500)

## 🐛 Troubleshooting

### Issue: 500 Internal Server Error

```bash
# Check logs
tail -f storage/logs/laravel.log

# Clear caches
php artisan cache:clear
php artisan config:clear
php artisan view:clear

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

### Issue: Database Connection Failed

```bash
# Verify credentials in .env
# Test connection
php artisan tinker
DB::connection()->getPdo();
```

### Issue: Assets Not Loading

```bash
# Rebuild assets
npm run build

# Create storage link
php artisan storage:link

# Check file permissions
```

### Issue: Email Not Sending

```bash
# Test in tinker
php artisan tinker
Mail::raw('Test', fn($msg) => $msg->to('test@example.com')->subject('Test'));

# Check SMTP credentials
# Verify firewall allows port 587
```

## 🔄 Updating the Application

```bash
# Pull latest changes
git pull origin main

# Update dependencies
composer install --no-dev --optimize-autoloader
npm install && npm run build

# Run migrations
php artisan migrate --force

# Clear and rebuild cache
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

## 📞 Support

If you encounter issues during deployment:

-   **Email**: info@tradeplusedu.com
-   **WhatsApp**: +82 1021533894
-   **Phone**: +880 1711-352344

---

**🎉 Congratulations! Your Trade Plus Education website is now live!**
