# Deploying FirstSittings to cPanel

A one-time setup, then every push to `main` that passes CI deploys automatically.
Do **staging first** (`staging.firstsittings.co.za`), then repeat for the live site.

## 1. Check the server (ask your host if unsure)

- [ ] PHP **8.3 or newer** for the domain (cPanel → *MultiPHP Manager*), with `intl`, `bcmath`, `gd`, `pdo_mysql`, `fileinfo`, `zip`, `exif` (cPanel → *Select PHP Version → Extensions*)
- [ ] **SSH access** (cPanel → *SSH Access* / *Terminal*) and `composer` available (`composer --version`)
- [ ] `git` available over SSH (`git --version`)
- [ ] Cron jobs that can run **every minute**
- [ ] **MySQL 8** or **MariaDB 10.6+**
- [ ] Resource limits: ask for the *entry processes* and *RAM* limits. 20+ entry processes and 1–2 GB of RAM is comfortable for the 12:00 drop.

## 2. Create the database

cPanel → *MySQL Databases*:
1. Create a database, for example `youruser_firstsittings`.
2. Create a user with a long random password.
3. Add the user to the database with **All Privileges**.

## 3. Put the code on the server

Over SSH:

```bash
cd ~
git clone git@github.com:<you>/first-sittings.git firstsittings
cd firstsittings
composer install --no-dev --optimize-autoloader
cp .env.example .env
php artisan key:generate
```

The server needs read access to the private GitHub repo. Create a key on the server with `ssh-keygen -t ed25519`, then add `~/.ssh/id_ed25519.pub` to the repo under *Settings → Deploy keys* (read-only).

## 4. Fill in `.env` on the server

Edit `~/firstsittings/.env` (cPanel *File Manager* or `nano`):

```
APP_NAME=FirstSittings
APP_ENV=production
APP_DEBUG=false
APP_URL=https://firstsittings.co.za
APP_TIMEZONE=Africa/Johannesburg

DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=youruser_firstsittings
DB_USERNAME=youruser_fsapp
DB_PASSWORD=...

SESSION_SECURE_COOKIE=true
```

PayFast and mail settings get added in Phase 2. **Never commit `.env`.** Then run:

```bash
php artisan migrate --force
php artisan db:seed --class=Database\\Seeders\\CitySeeder --force
php artisan storage:link
php artisan optimize
php artisan filament:optimize
```

On **staging only**, you can also run `php artisan db:seed --force` to load the demo restaurants and logins. Set `SEED_PASSWORD` first. This step refuses to create demo logins when `APP_ENV=production`.

## 5. Point the domain at `/public`

cPanel → *Domains* → the domain → **Document Root** = `firstsittings/public`.
If your host won't let you change it, ask them. Never put the whole app inside `public_html`.

Then cPanel → *SSL/TLS Status* → run **AutoSSL** so the site is on HTTPS.

## 6. Add the cron job

cPanel → *Cron Jobs* → **Every minute** (`* * * * *`):

```
cd /home/youruser/firstsittings && php artisan schedule:run >> /dev/null 2>&1
```

Use the full path to PHP 8.3+ if plain `php` is older, for example `/opt/cpanel/ea-php83/root/usr/bin/php`.
This one job runs the 12:00 table drop, expires checkout holds, and sends reminders (from Phase 1 onwards).

## 7. Switch on automatic deploys

1. On your computer, create a key just for deploys: `ssh-keygen -t ed25519 -f firstsittings_deploy`.
2. cPanel → *SSH Access → Manage SSH Keys* → **Import** the `.pub` file, then **Authorize** it.
3. GitHub repo → *Settings → Secrets and variables → Actions* → add:

| Secret | Value |
|---|---|
| `CPANEL_HOST` | your server hostname |
| `CPANEL_USER` | your cPanel username |
| `CPANEL_PORT` | SSH port (often `22`; some hosts use `2222`) |
| `CPANEL_PATH` | `/home/youruser/firstsittings` |
| `CPANEL_SSH_KEY` | the contents of the **private** key file `firstsittings_deploy` |
| `CPANEL_KNOWN_HOSTS` | *(recommended)* the server's SSH fingerprint line(s). Get it with `ssh-keyscan -p <port> <host>` from a machine you trust, and check it against what your host shows. |
| `CPANEL_PHP` | *(optional)* full path to PHP 8.3+ if `php` is older |
| `CPANEL_COMPOSER` | *(optional)* how to run Composer with that PHP, if plain `composer` uses the older one. For example: `/opt/cpanel/ea-php83/root/usr/bin/php /opt/cpanel/composer/bin/composer` |

Each deploy checks out the **exact commit CI tested** on the server (as a detached HEAD), so the code and the uploaded CSS and JS always match. Don't edit files on the server; they get overwritten.

Only a successful CI run of a **push to this repo's own `main`** deploys. Pull requests, including ones from forks, never do.

From then on: push to `main` → CI runs the tests on MySQL 8 → if they pass, **Deploy to cPanel** pulls the code, installs dependencies, uploads the built CSS and JS, runs migrations and refreshes the caches.
You can also run it by hand from GitHub → *Actions → Deploy to cPanel → Run workflow*.
