# Docker to Direct Hosting Migration

## Overview

This document explains the migration from a full Docker setup to a hybrid approach for **production only**, while keeping the **local development** environment unchanged with full Docker setup.

## Why This Change?

### Benefits of Direct Hosting (Production)
1. **Better Performance**: Direct PHP execution without Docker overhead
2. **Easier Debugging**: Direct access to logs and files
3. **Resource Efficiency**: Lower memory and CPU usage
4. **Faster File Operations**: Direct file system access
5. **Simpler Deployment**: No need to build Docker images for application

### Benefits of Keeping Database Services in Docker (Production)
1. **Isolation**: Database services are isolated and easily manageable
2. **Portability**: Easy to move database services between environments
3. **Version Control**: Specific database versions can be maintained
4. **Backup/Restore**: Docker volumes make backup/restore operations simple

### Local Development Benefits (Unchanged)
1. **Consistency**: Same environment across all developers
2. **Easy Setup**: One command to start everything
3. **Isolation**: No conflicts with local system services
4. **Familiar Workflow**: Existing Docker commands remain the same

## Changes Made

### 1. Updated Docker Compose Files

#### `docker-compose.yml` (Local Development - UNCHANGED)
- ✅ **Kept** `app` service (Laravel application)
- ✅ **Kept** `nginx` service
- ✅ **Kept** `mysql`, `redis`, and `adminer` services
- **Purpose**: Local development environment

#### `docker-compose.production-only.yaml` (Production - NEW)
- ❌ **No** `app` service (Laravel application runs directly)
- ❌ **No** `nginx` service (nginx runs directly on server)
- ✅ **Kept** `mysql`, `redis`, and `adminer` services
- **Purpose**: Production database services only

#### `docker-compose.production.yaml` (Production - UNCHANGED)
- **Purpose**: Full Docker production setup (alternative option)
- **Usage**: Can still use full Docker setup if preferred

### 2. Nginx Configuration

#### `nginx/direct-hosting-production.conf` (Production)
- Full SSL configuration
- Production security settings
- Optimized caching and compression
- Certbot integration for Let's Encrypt

### 3. Setup Scripts

#### `scripts/setup-direct-hosting.sh` (Linux/macOS)
- Automated setup script for Unix systems
- Checks prerequisites
- Configures environment
- Sets up Docker services

#### `scripts/setup-direct-hosting.ps1` (Windows)
- PowerShell version for Windows
- Same functionality as bash script
- Windows-specific instructions

## Environment Setup

### Local Development (No Changes)

Your local development environment remains exactly as it was:

```bash
# Start local development
docker-compose up -d

# Access points
# Laravel App: http://localhost:8000
# Adminer: http://localhost:8080
# MySQL: localhost:3306
# Redis: localhost:6379

# PHP commands (unchanged)
docker-compose exec app php artisan migrate
docker-compose exec app php artisan tinker
docker-compose exec app composer install
```

### Production Setup (Direct Hosting)

#### Step 1: Install nginx and PHP-FPM
```bash
# Ubuntu/Debian
sudo apt install nginx php8.3-fpm

# CentOS/RHEL
sudo yum install nginx php-fpm
```

#### Step 2: Configure nginx
```bash
sudo cp nginx/direct-hosting-production.conf /etc/nginx/sites-available/seraph3
sudo ln -s /etc/nginx/sites-available/seraph3 /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
```

#### Step 3: Start Docker services
```bash
docker-compose -f docker-compose.production-only.yaml up -d
```

#### Step 4: Configure Laravel
```bash
# Update .env for direct hosting
DB_HOST=127.0.0.1
REDIS_HOST=127.0.0.1

# Run Laravel commands directly
php artisan migrate
php artisan config:cache
php artisan route:cache
```

## Command Changes

### Local Development (Unchanged)
```bash
# PHP commands
docker-compose exec app php artisan migrate
docker-compose exec app php artisan tinker
docker-compose exec app composer install

# Database access
docker-compose exec mysql mysql -u root -p
```

### Production (Direct Hosting)
```bash
# PHP commands
php artisan migrate
php artisan tinker
composer install

# Database access
mysql -h 127.0.0.1 -P 3306 -u root -p
```

## Service Management

### Local Development (Docker)
```bash
# Start all services
docker-compose up -d

# Stop all services
docker-compose down

# View logs
docker-compose logs app
docker-compose logs mysql
docker-compose logs redis
```

### Production (Direct Hosting + Docker)

#### Docker Services
```bash
# Start database services
docker-compose -f docker-compose.production-only.yaml up -d

# Stop database services
docker-compose -f docker-compose.production-only.yaml down

# View logs
docker-compose -f docker-compose.production-only.yaml logs mysql
docker-compose -f docker-compose.production-only.yaml logs redis
```

#### Direct Hosting Services
```bash
# Nginx
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl reload nginx

# PHP-FPM
sudo systemctl start php8.3-fpm
sudo systemctl stop php8.3-fpm
sudo systemctl restart php8.3-fpm
```

## Access Points

### Local Development
| Service | URL/Port | Description |
|---------|----------|-------------|
| Laravel App | http://localhost:8000 | Main application |
| Adminer | http://localhost:8080 | Database administration |
| MySQL | localhost:3306 | Database server |
| Redis | localhost:6379 | Cache server |

### Production
| Service | URL/Port | Description |
|---------|----------|-------------|
| Laravel App | http://your-domain.com | Main application |
| Adminer | http://your-domain.com:8080 | Database administration |
| MySQL | localhost:3306 | Database server |
| Redis | localhost:6379 | Cache server |

## Troubleshooting

### Common Issues

1. **PHP-FPM not running**:
```bash
sudo systemctl status php8.3-fpm
sudo journalctl -u php8.3-fpm
```

2. **Nginx configuration errors**:
```bash
sudo nginx -t
```

3. **Database connection issues**:
```bash
docker-compose -f docker-compose.production-only.yaml logs mysql
```

4. **Permission issues**:
```bash
sudo chown -R www-data:www-data /path/to/your/laravel/app
```

### Log Files

- **Nginx**: `/var/log/nginx/error.log`
- **PHP-FPM**: `/var/log/php8.3-fpm.log`
- **Laravel**: `storage/logs/laravel.log`
- **Docker**: `docker-compose -f docker-compose.production-only.yaml logs [service]`

## Performance Considerations

### Optimizations for Direct Hosting

1. **PHP OPcache**:
```ini
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
```

2. **Redis Caching**:
```env
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_DRIVER=redis
```

3. **Nginx Caching**:
- Static file caching
- Gzip compression
- Browser caching headers

## Security Considerations

1. **File Permissions**: Ensure proper ownership and permissions
2. **SSL/TLS**: Use HTTPS in production
3. **Firewall**: Configure firewall rules
4. **Database Security**: Use strong passwords and limit access
5. **Regular Updates**: Keep nginx, PHP, and Docker images updated

## Rollback Plan

If you need to rollback to the full Docker production setup:

1. **Restore original docker-compose.production.yaml**
2. **Stop direct hosting services**:
```bash
sudo systemctl stop nginx php8.3-fpm
```
3. **Start Docker services**:
```bash
docker-compose -f docker-compose.production.yaml up -d
```
4. **Update .env** to use Docker service names:
```env
DB_HOST=mysql
REDIS_HOST=redis
```

## Summary

- **Local Development**: Uses `docker-compose.yml` (unchanged) - full Docker setup
- **Production**: Uses `docker-compose.production-only.yaml` + direct hosting
- **Benefits**: Better performance in production, easier debugging, simpler development workflow
- **Flexibility**: Can easily switch between setups as needed
- **No Local Changes**: Your local development workflow remains exactly the same

## Support

For detailed setup instructions, see:
- [Direct Hosting Setup Guide](DIRECT_HOSTING_SETUP.md)
- [Nginx Configuration Documentation](https://nginx.org/en/docs/)
- [PHP-FPM Documentation](https://www.php.net/manual/en/install.fpm.php) 