# Nginx + Ubuntu Setup for IFRAP Multi-Tenant

## Prerequisites

- Ubuntu 20.04 or later
- Nginx installed
- PHP 8.3 with FPM 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 Nginx Vhost

Create: `/etc/nginx/sites-available/ifrap`

```bash
sudo nano /etc/nginx/sites-available/ifrap
```

Paste:
```nginx
server {
    listen 80;
    server_name ~^(?<domain>.+)\.ifrap\.com$ dev.ifrap.com *.localtenant.com localhost;

    root /var/www/dev.ifrap.com/IFRAP/public;
    index index.php;

    client_max_body_size 100M;

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

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

    location ~ /\. {
        deny all;
    }
}
```

Save: `Ctrl+X → Y → Enter`

---

## Step 3: Enable Vhost

```bash
sudo ln -s /etc/nginx/sites-available/ifrap /etc/nginx/sites-enabled/
```

Test Nginx config:
```bash
sudo nginx -t
```

Should show: `nginx: the configuration file ... syntax is ok`

---

## Step 4: Reload Nginx

```bash
sudo systemctl reload nginx
```

Or:
```bash
sudo service nginx reload
```

---

## Step 5: Ensure PHP-FPM is Running

Check if FPM socket exists:
```bash
ls -la /var/run/php/php8.3-fpm.sock
```

If not running:
```bash
sudo systemctl start php8.3-fpm
sudo systemctl enable php8.3-fpm
```

---

## 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

**502 Bad Gateway?**
```bash
sudo systemctl status php8.3-fpm
sudo systemctl restart php8.3-fpm
```

**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`

**nginx: command not found?**
```bash
sudo apt update
sudo apt install nginx
```

**PHP 8.3 not found?**
```bash
sudo apt update
sudo apt install php8.3-fpm php8.3-pgsql php8.3-mysql
```
