# Apache + Ubuntu Setup for IFRAP Multi-Tenant

## Prerequisites

- Ubuntu 20.04 or later
- Apache2 installed
- PHP 8.3 installed
- PostgreSQL or MySQL installed

---

## Step 1: Configure Hosts File

Edit: `/etc/hosts`

```bash
sudo nano /etc/hosts
```

Add:
```
127.0.0.1 dev.ifrap.com
127.0.0.1 dev.localtenant.com
127.0.0.1 test.localtenant.com
```

Save: `Ctrl+X → Y → Enter`

---

## Step 2: Configure Apache Vhost

Create: `/etc/apache2/sites-available/ifrap.conf`

```bash
sudo nano /etc/apache2/sites-available/ifrap.conf
```

Paste:
```apache
<VirtualHost *:80>
    ServerName dev.ifrap.com
    ServerAlias *.localtenant.com localhost
    DocumentRoot /var/www/dev.ifrap.com/IFRAP/public

    <Directory /var/www/dev.ifrap.com/IFRAP/public>
        AllowOverride All
        Require all granted
        
        <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^ index.php [QSA,L]
        </IfModule>
    </Directory>

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

Save: `Ctrl+X → Y → Enter`

---

## Step 3: Enable Required Modules

```bash
sudo a2enmod rewrite
sudo a2enmod headers
```

---

## Step 4: Enable Vhost

```bash
sudo a2ensite ifrap
```

Test Apache config:
```bash
sudo apache2ctl configtest
```

Should show: `Syntax OK`

---

## Step 5: Reload Apache

```bash
sudo systemctl reload apache2
```

Or:
```bash
sudo service apache2 reload
```

---

## Step 6: Place Project Files

```bash
cd /var/www/dev.ifrap.com/IFRAP
```

If not exists, clone or copy project there.

Set permissions:
```bash
sudo chown -R www-data:www-data .
sudo chmod -R 755 storage bootstrap/cache
```

---

## Step 7: Laravel Setup

```bash
composer install
php artisan key:generate
cp .env.example .env
```

---

## Step 8: Configure .env

Edit: `/var/www/dev.ifrap.com/IFRAP/.env`

```env
APP_NAME="IFRAP"
APP_URL=http://dev.ifrap.com
APP_ENV=local
APP_DEBUG=true

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=ifrap_central
DB_USERNAME=postgres
DB_PASSWORD=postgres

CENTRAL_DOMAINS=dev.ifrap.com,localhost,127.0.0.1
TENANT_DOMAIN_EXPECTED_HOST=dev.ifrap.com
TENANT_DOMAIN_TARGETS=dev.ifrap.com,127.0.0.1
```

---

## Step 9: Setup Database

PostgreSQL:
```bash
sudo -u postgres psql
```

```sql
CREATE DATABASE ifrap_central WITH ENCODING 'UTF8';
\q
```

Or MySQL:
```bash
mysql -u root -p
```

```sql
CREATE DATABASE ifrap_central CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
EXIT;
```

---

## Step 10: Run Migrations

```bash
php artisan migrate
php artisan optimize:clear
```

---

## Step 11: Test

Open: `http://dev.ifrap.com`

You should see app loaded.

---

## Step 12: Add Test Domain

1. Login
2. Settings → Tenant Domain tab
3. Add: `dev.localtenant.com`
4. Open: `http://dev.localtenant.com`

---

## Troubleshooting

**403 Forbidden?**
```bash
sudo chown -R www-data:www-data /var/www/dev.ifrap.com/IFRAP
sudo chmod -R 755 /var/www/dev.ifrap.com/IFRAP/public
```

**Rewrite not working?**
```bash
sudo a2enmod rewrite
sudo apache2ctl configtest
sudo systemctl reload apache2
```

**Permission denied (storage)?**
```bash
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 755 storage bootstrap/cache
```

**Domain not resolving?**
```bash
sudo systemd-resolve --flush-caches
ping dev.ifrap.com
```

Should show `127.0.0.1`

**Apache won't start?**
```bash
sudo apache2ctl configtest
```

Fix errors shown, then:
```bash
sudo systemctl restart apache2
```

**PHP not loading?**
```bash
sudo apt install libapache2-mod-php8.3
sudo a2enmod php8.3
sudo systemctl reload apache2
```
