# Setup Instructions

## Step-by-Step Setup Guide

### 1. Install Dependencies

```bash
composer install
```

### 2. Environment Configuration

Copy the `.env.example` file to `.env`:

```bash
cp .env.example .env
```

Generate application key:

```bash
php artisan key:generate
```

### 3. Database Configuration

Edit the `.env` file and configure your database:

```env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=multi_tenant_cms
DB_USERNAME=root
DB_PASSWORD=your_password
```

### 4. Run Migrations

Create the database tables:

```bash
php artisan migrate
```

### 5. Seed Initial Data

Create a default admin account:

```bash
php artisan db:seed
```

**Default Admin Credentials:**
- Email: `admin@example.com`
- Password: `password`

### 6. Start Development Server

```bash
php artisan serve
```

The application will be available at `http://localhost:8000`

## Access Points

### Admin Panel
- Login: `http://localhost:8000/admin/login`
- Register: `http://localhost:8000/admin/register`
- Dashboard: `http://localhost:8000/admin/dashboard`

### Tenant Sites
- Access tenant sites via their configured domains
- Example: If domain is `example.com`, access it at `http://example.com` (requires DNS configuration)

## How It Works

1. **Admin Registration**: When a new admin registers, a domain is automatically created for them
2. **Domain Management**: Admins can create, edit, and manage multiple domains from the dashboard
3. **Multi-Tenancy**: Each domain is isolated and managed by its assigned admin
4. **Tenant Routing**: The `IdentifyTenant` middleware identifies the domain and routes requests accordingly

## Domain Configuration

For local development with custom domains, you can:

1. Edit your `hosts` file (Windows: `C:\Windows\System32\drivers\etc\hosts`, Linux/Mac: `/etc/hosts`)
2. Add entries like:
   ```
   127.0.0.1 example.com
   127.0.0.1 www.example.com
   ```
3. Access the site via the configured domain

## Creating a New Domain (Linux Server)

1. Log in to the admin panel:
   - `https://your-domain.com/admin`

2. Go to **Domains** → **Create** and add:
   - `domain`: `example.com`
   - `subdomain` (optional): `school1`

3. Update DNS records to point to your server IP:
   ```
   example.com      A   YOUR_SERVER_IP
   *.example.com    A   YOUR_SERVER_IP
   ```

4. Configure your web server to accept the new domain:
   - **Nginx example:**
     ```
     server {
         listen 80;
         server_name example.com *.example.com;
         root /var/www/custom-cms/public;
         index index.php;

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

         location ~ \.php$ {
             include snippets/fastcgi-php.conf;
             fastcgi_pass unix:/run/php/php8.2-fpm.sock;
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         }
     }
     ```

5. Enable SSL (recommended):
   - Use Let’s Encrypt for `example.com` and `*.example.com`

6. Clear caches after changes:
   ```bash
   php artisan config:cache
   php artisan route:cache
   ```

7. Test:
   - `https://example.com`
   - `https://school1.example.com`

## Production Deployment

1. Configure your web server (Apache/Nginx) to point to the `public` directory
2. Set up proper DNS records for each domain
3. Configure SSL certificates for each domain
4. Update `.env` with production settings
5. Run `php artisan config:cache` and `php artisan route:cache`

## Features

- ✅ Multi-tenant architecture
- ✅ Domain-based routing
- ✅ Admin authentication
- ✅ Domain management
- ✅ Automatic domain creation on admin registration
- ✅ Beautiful admin dashboard UI
- ✅ Secure admin panel

