# Backend Installation Guide (Shared Hosting & VPS)

This guide covers deploying the **Wareed** Laravel backend to two common environments:

1.  **Shared Hosting (cPanel)**
2.  **Virtual Private Server (VPS) - Ubuntu/Debian**

---

## 🏗 Option 1: Shared Hosting (cPanel)

> **⚠️ Important Note on Real-Time Features:**
> This project uses **Laravel Reverb** for real-time updates (WebSockets). Reverb requires a long-running background process/server.
>
> -   **Most shared hosting providers DO NOT allow** long-running processes (using `supervisor` or `nohup`).
> -   If your host is strict, the real-time features might not work. You may need to switch the `BROADCAST_CONNECTION` in `.env` to `pusher` and use a paid Pusher.com account, or upgrade to a VPS.

### 1. Prepare the Files

1.  On your local machine, run:
    ```bash
    composer install --optimize-autoloader --no-dev
    ```
2.  Compress the entire project folder into a **ZIP** file.

### 2. Upload to Server

1.  Log in to **cPanel** -> **File Manager**.
2.  Create a folder strictly for your app code (NOT inside `public_html`), e.g., `/home/username/wareed_app`.
3.  Upload and extract your ZIP there.

### 3. Setup Database

1.  Go to **MySQL Databases** in cPanel.
2.  Create a new Database (e.g., `wareed_db`).
3.  Create a new User and assign it to the database with **All Privileges**.

### 4. Configure Application

1.  Rename `.env.example` to `.env`.
2.  Edit `.env`:
    -   **App URL:** `APP_URL=https://yourdomain.com`
    -   **Database:** Fill in `DB_DATABASE`, `DB_USERNAME`, `DB_PASSWORD`.
    -   **Firebase:** Add your Firebase credentials (as text content in `FIREBASE_PRIVATE_KEY` etc).
    -   **Filesystem:** `FILESYSTEM_DISK=public`

### 5. Link Public Folder

The `public` folder inside Laravel must be the one accessible from the web.

-   **Method A (Subdomain/Addon Domain):**
    -   When creating the domain in cPanel, set the **Document Root** to `/home/username/wareed_app/public`.
-   **Method B (Symlink - if you have SSH access):**
    -   `ln -s /home/username/wareed_app/public /home/username/public_html`

### 6. Run Migrations

If you have SSH access (Terminal) in cPanel:

```bash
cd /home/username/wareed_app
php artisan migrate --force
php artisan storage:link
php artisan config:cache
```

_If no SSH:_ You may need to import the SQL file via **phpMyAdmin** manually or create a temporary Route in `web.php` to run `Artisan::call('migrate')` (delete immediately after use).

### 7. Cron Jobs

Set up the scheduler to run queues (since Supervisor isn't available). In cPanel **Cron Jobs**:

-   **Command:** `cd /home/username/wareed_app && php artisan schedule:run >> /dev/null 2>&1`
-   **Frequency:** Every minute (\* \* \* \* \*)

---

## 🚀 Option 2: VPS (Ubuntu/Nginx) - Recommended

This is the preferred method for full support of Reverb/WebSockets and Queues.

### 1. Server Prerequisites

Assuming Ubuntu 22.04/24.04:

```bash
sudo apt update
sudo apt install nginx mysql-server php8.2-fpm php8.2-mysql php8.2-xml php8.2-mbstring php8.2-curl php8.2-zip unzip supervisor redis-server
```

### 2. Setup Database

```bash
sudo mysql
> CREATE DATABASE wareed_db;
> CREATE USER 'wareed_user'@'localhost' IDENTIFIED BY 'your_secure_password';
> GRANT ALL PRIVILEGES ON wareed_db.* TO 'wareed_user'@'localhost';
> EXIT;
```

### 3. Install Application

```bash
cd /var/www
git clone https://github.com/YusrSudan/Wareed-laravel-backend.git wareed
cd wareed

# Install Dependencies
composer install --optimize-autoloader --no-dev
# Setup Environment
cp .env.example .env
nano .env  # Update DB, App URL, Reverb, Firebase credentials
```

### 4. Permissions

```bash
chown -R www-data:www-data /var/www/wareed
chmod -R 775 /var/www/wareed/storage /var/www/wareed/bootstrap/cache
```

### 5. Finalize App

```bash
php artisan key:generate
php artisan storage:link
php artisan migrate --force
php artisan config:cache
```

### 6. Configure Supervisor (Crucial for Queues & Reverb)

Create config file `/etc/supervisor/conf.d/wareed.conf`:

```ini
[program:wareed-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/wareed/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/wareed/storage/logs/worker.log

[program:wareed-reverb]
process_name=%(program_name)s
command=php /var/www/wareed/artisan reverb:start
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/var/www/wareed/storage/logs/reverb.log
```

Start updated configurations:

```bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start all
```

### 7. Configure Nginx

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

```nginx
server {
    listen 80;
    server_name api.yourdomain.com;
    root /var/www/wareed/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;
    }

    # Reverb WebSocket Proxy
    location /app {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

Enable site and restart Nginx:

```bash
sudo ln -s /etc/nginx/sites-available/wareed /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
```

### 8. SSL (HTTPS)

Secure your site (Reverb requires secure WebSockets `wss://`):

```bash
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d api.yourdomain.com
```

Your API is now live! 🚀
