# Production Deployment Guide

A concise operational guide for deploying and maintaining **arb.admedia.com**.

---

## 1. Key Architectural Facts

* **PHP 8.5 & Laravel 13:** CLI commands and PHP-FPM must run on PHP 8.5 (`php8.5`).
* **`vendor/` is Git-Tracked:** Do **not** run `composer install` in production. New dependencies are committed to Git in dev.
* **No Frontend Build (`npm run build`):** UI uses Bootstrap 5.3 CDN and Livewire/Volt. There is no Vite or Webpack bundler.
* **Puppeteer Runtime:** Node and Chrome binaries are required for backend scraper scripts (Meta Ad Library and Brand Signal extraction).
* **Permissions:** Web server (`www-data` or `nginx`) needs write access to `storage/` and `bootstrap/cache/`. In production, set web server group ownership with `775` (or POSIX ACLs). On shared dev environments without sudo access, `./pf.sh` / `777` is provided as a local fallback.

---

## 2. Server Prerequisites

Ask the sysadmin to confirm these are in place before deploying:

* **PHP 8.5:** `php8.5-fpm`, `php8.5-cli`, extensions: `pdo_mysql`, `gd`, `curl`, `mbstring`, `xml`, `pcntl`, `posix`, `bcmath`, `zip`.
* **Node.js & Chrome Dependencies:** Node 22+ LTS and system libraries for headless Chrome: `libnss3`, `libatk1.0-0`, `libx11-xcb1`, `libgbm1`, `libasound2`.
* **Nginx & MySQL:** Web server pointing to `<project-dir>/public`; MySQL database pre-created (`arb` or as configured in `.env`).
* **systemd:** Runs the persistent background queue worker in production.
* **Cron or systemd timer:** Runs Laravel's scheduler.

---

## 3. First-Time Server Setup

> **Note:** The `.env` file is provided separately by the developer. Place it at the project root before proceeding.

### Step 1: Clone and enter the repository

```bash
git clone <repo-url> /var/www/arb.admedia.com
cd /var/www/arb.admedia.com
```

### Step 2: Place the `.env` file

Copy the provided `.env` file to the project root and lock it down:

```bash
chmod 600 .env
```

### Step 3: Generate `APP_KEY` (one-time only)

```bash
php8.5 artisan key:generate --no-interaction
```

### Step 4: Run the deploy script

The deploy script handles Puppeteer install, migrations, first-time seeding (auto-detected), caches, permissions, and OPcache reload. Import the tracked location snapshot after the script finishes.

```bash
chmod +x deploy.sh
./deploy.sh
php8.5 artisan db:seed --class=CountryLocationSeeder --force --no-interaction
php8.5 artisan locations:snapshot import database/location-catalog-2026-10-07.jsonl --no-interaction
```

**What the script does automatically on first run:**
- Installs Node modules and Puppeteer Chrome browser.
- Runs all database migrations.
- **Detects first-time setup** (checks if the `avatars` table is empty) and runs `ProductionSeeder` and `PlatformConnectionSeeder` automatically — seeding avatars, voices, ad accounts, Facebook pages, and platform connections.
- Warms all Laravel caches.
- Creates the `public/storage` symlink.
- Signals queue workers to restart.
- Re-applies file permissions.
- Reloads PHP-FPM.

On subsequent deployments, the seeder is skipped automatically.

### Step 5: Install the scheduler and systemd queue worker (see Section 4)

---

## 4. Scheduler and Queue Worker

### A. Scheduler Cron (Mandatory — install once)

Required for daily OAuth token refresh (`platforms:refresh-tokens`), campaign performance sync (`campaigns:sync-performance`), database queue pruning, and scheduled tasks. 

#### Method 1: `admediacrons` Centralized Runner (AdMedia Company Standard)
If cron tasks are maintained in the centralized `admediacrons` repository:
1. Deploy `html/arb_cron.php` into the `admediacrons.com` repository.
2. In the `admediacrons` server's master crontab:
```cron
# /var/admediacrons.com/html/arb_cron.php triggers https://arb.admedia.com/api/cron/run
* * * * * cd /var/admediacrons.com/html && /usr/bin/php arb_cron.php schedule:run >> /tmp/arb_cron.log 2>&1
```
*(The runner uses AdMedia Redis locking, sends NOC alerts on failure, and triggers Laravel tasks on the application server under PHP 8.5).*

#### Method 2: Drop-in File via `/etc/cron.d/` (On ARB Server)
Create `/etc/cron.d/arb`:
```cron
# /etc/cron.d/arb - Laravel task scheduler
* * * * * www-data cd /var/www/arb.admedia.com && /usr/bin/php8.5 artisan schedule:run >> /dev/null 2>&1
```
Ensure permissions:
```bash
sudo chmod 0644 /etc/cron.d/arb
```

#### Method 3: Systemd Timer (For systemd-only environments without cron)
1. Create `/etc/systemd/system/arb-scheduler.service`:
```ini
[Unit]
Description=ARB Laravel Scheduler
After=network.target

[Service]
Type=oneshot
User=www-data
Group=www-data
WorkingDirectory=/var/www/arb.admedia.com
ExecStart=/usr/bin/php8.5 /var/www/arb.admedia.com/artisan schedule:run
```

2. Create `/etc/systemd/system/arb-scheduler.timer`:
```ini
[Unit]
Description=Run ARB Laravel Scheduler every minute

[Timer]
OnCalendar=*:0/1
AccuracySec=1s
Persistent=true

[Install]
WantedBy=timers.target
```

3. Enable and start:
```bash
sudo systemctl daemon-reload
sudo systemctl enable --now arb-scheduler.timer
```

#### Method 4: User Crontab (`crontab -e`)
Add to `crontab -e` for user `www-data`:
```cron
* * * * * cd /var/www/arb.admedia.com && php8.5 artisan schedule:run >> /dev/null 2>&1
```


### B. systemd Queue Worker (Install once)

Production runs its persistent database queue worker under systemd. Keep `SCHEDULE_QUEUE_WORKER=false` in `.env` so the scheduler does not launch another worker each minute. If a worker unit already exists, use that unit and do not create a second one; the example below is for a new server.

Create `/etc/systemd/system/arb-worker.service`:

```ini
[Unit]
Description=ARB Laravel Queue Worker
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/arb.admedia.com
ExecStart=/usr/bin/php8.5 /var/www/arb.admedia.com/artisan queue:work database --sleep=3 --tries=1 --timeout=2000 --max-time=3600
Restart=always
RestartSec=5
TimeoutStopSec=2160

[Install]
WantedBy=multi-user.target
```

Enable and start it:
```bash
sudo systemctl daemon-reload
sudo systemctl enable --now arb-worker.service
```

If no persistent worker is available, set `SCHEDULE_QUEUE_WORKER=true` and run the scheduler every minute to start a short-lived `queue:work --stop-when-empty` process. Leave it disabled when the systemd worker is active.

---

## 5. Routine Deployments (After Every Code Push)

Using the automated script:
```bash
./deploy.sh
# or for a specific branch:
./deploy.sh production
```

After the script migrates the database, seed the country catalog and import the tracked location snapshot. The import is repeatable and does not call ad platform APIs:

```bash
php8.5 artisan db:seed --class=CountryLocationSeeder --force --no-interaction
php8.5 artisan locations:snapshot import database/location-catalog-2026-10-07.jsonl --no-interaction
```

---

## 6. Manual Instructions for Sysadmins (If unable to run `deploy.sh`)

If company policy or environment restrictions prevent executing shell automation scripts, sysadmins can perform all actions manually:

### A. One-Time Setup (Run Once on New Server)

Execute these commands in order from `/var/www/arb.admedia.com`:

```bash
cd /var/www/arb.admedia.com

# 1. Environment configuration
cp .env.example .env
chmod 600 .env
# Edit .env with database credentials and AI API keys
php8.5 artisan key:generate --no-interaction

# 2. Scraper dependencies (Puppeteer Chrome)
npm install
npx puppeteer browsers install chrome

# 3. Database migrations
php8.5 artisan migrate --force --no-interaction

# 3a. Country and provider location catalog
php8.5 artisan db:seed --class=CountryLocationSeeder --force --no-interaction
php8.5 artisan locations:snapshot import database/location-catalog-2026-10-07.jsonl --no-interaction

# 4. Initial database seeding (One-time only)
# Seeds avatars, voices, ad accounts, Facebook pages, and platform credentials
php8.5 artisan db:seed --class=ProductionSeeder --force --no-interaction
php8.5 artisan db:seed --class=PlatformConnectionSeeder --force --no-interaction

# 5. Public storage symlink
php8.5 artisan storage:link --no-interaction

# 6. Optimize and warm caches
php8.5 artisan optimize:clear --no-interaction
php8.5 artisan optimize --no-interaction

# 7. File permissions (Production standard: 775 with web server group ownership)
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
# If deploying as a separate user (e.g. deploy), add user to group and enable SGID:
# sudo usermod -a -G www-data $USER && sudo chmod -R g+s storage bootstrap/cache
# (Fallback for unprivileged environments without sudo: ./pf.sh)

# 8. Setup Scheduler & Queue Worker (One-time)
# Scheduler: create /etc/cron.d/arb (or systemd timer) — see Section 4A
# Worker: configure the systemd service — see Section 4B
```

---

### B. Routine Release Steps (Run on Every Deployment / Code Release)

Whenever a new code release or branch update is deployed:

```bash
cd /var/www/arb.admedia.com

# 1. Pull latest code
git fetch origin production
git pull origin production

# 2. Update Node dependencies (only if package.json was modified)
npm install
npx puppeteer browsers install chrome

# 3. Run pending database migrations
php8.5 artisan migrate --force --no-interaction

# 4. Update the location catalog from the tracked snapshot
php8.5 artisan db:seed --class=CountryLocationSeeder --force --no-interaction
php8.5 artisan locations:snapshot import database/location-catalog-2026-10-07.jsonl --no-interaction

# 5. Flush and re-warm Laravel caches
php8.5 artisan optimize:clear --no-interaction
php8.5 artisan optimize --no-interaction

# 6. Signal the systemd queue worker to restart after its current job
php8.5 artisan queue:restart --no-interaction

# 7. Verify file permissions
sudo chmod -R 775 storage bootstrap/cache 2>/dev/null || ./pf.sh

# 8. Reload PHP-FPM to flush OPcache
sudo systemctl reload php8.5-fpm

# 8. Check query strings reach PHP — do not skip, see C below
curl -fsS "https://arb.admedia.com/up/query?ping=hello"
```

*(Note: `composer install` is **never** run because `vendor/` is tracked in git. `npm run build` is **never** run because Bootstrap 5.3 CDN + Livewire is used).*

---
### C. The query string must reach PHP

Twice now this vhost has been deployed with the PHP fallback written as
`try_files $uri $uri/ /index.php;`, dropping the `?$query_string` that the
reference config in section 7 has. Every GET parameter on the site is then
discarded, which breaks signed URLs, email verification and password-reset links,
OAuth redirects, pagination, and `?new=1`, so a new chat opens on the user's most
recent conversation and whatever approval it was parked on.

It hides well. The right commit is deployed, the files on disk are correct, the
application boots, `/up` returns 200, and the chat itself keeps working because it
sends over POST. The second occurrence ran for five weeks.

So check it by hand after every release, and any time the vhost is edited. `/up/query`
exists for exactly this and needs no sign-in, no token and no session:

```bash
curl -fsS "https://arb.admedia.com/up/query?ping=hello"
# {"ping":"hello"}   query strings are reaching PHP
# {"ping":null}      they are not; fix the try_files line in section 7
```

`./deploy.sh` does **not** run this. It was added there and then taken back out: the
mistake is made in the vhost, which deploying never touches, so a deploy-time check
cannot prevent it and would only notice on the next release after nginx was already
broken — at the cost of a new way for a deploy to abort, including when the only real
problem is that the box cannot curl its own public URL.

### Step 7 can fail quietly

`sudo systemctl reload php8.5-fpm 2>/dev/null || echo "Notice: ..."` in `deploy.sh`
cannot fail the script, because `echo` exits 0, and `2>/dev/null` discards the reason.
So a deploy that flushed nothing still ends in "Deployment completed successfully!".
Read the output rather than the last line.

How much that matters depends on one setting, which is worth confirming on the box:

```bash
php8.5 -i | grep -E 'opcache.enable|validate_timestamps|revalidate_freq'
```

`validate_timestamps=1` (the PHP default) means new code is picked up within
`revalidate_freq` seconds on its own and a missed reload mostly self-heals.
`validate_timestamps=0` means prod keeps serving the **old** code until FPM is
reloaded, and a missed reload is a deploy that silently did nothing. If it is 0, make
the reload non-optional and give the deploy user that one command:

```bash
echo "$(id -un) ALL=(root) NOPASSWD: /usr/bin/systemctl reload php8.5-fpm" \
  | sudo tee /etc/sudoers.d/arb-deploy
```

## 7. Nginx VirtualHost Reference

```nginx
server {
    listen 443 ssl http2;
    server_name arb.admedia.com;
    root /var/www/arb.admedia.com/public;
    index index.php;

    charset utf-8;
    client_max_body_size 50m;

    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; }

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

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

---

## 8. Verifying the Deployment

```bash
# App health check (should return 200)
curl -s -o /dev/null -w "%{http_code}" https://arb.admedia.com/up

# Check migration status
php8.5 artisan migrate:status --no-interaction

# Check the queue worker is running
sudo systemctl status arb-worker.service

# Check for failed jobs
php8.5 artisan queue:failed --no-interaction
```
