# Deploying to HostMídia site hosting (cPanel)

Use the PRO plan or higher. The package `dextra-site-hostmidia.zip` already contains the built site; the server only runs it.

Before you start, HostMídia support must confirm:
- Node.js **24.22 or newer** is offered in "Setup Node.js App" ("Configurar aplicativo Node.js").
- PostgreSQL is available. The `pgvector` extension is optional; without it the AI advisor uses full-text search.

## Package layout

```
dextra-site/
  app/               the site (server.js and everything it needs). Replaced on every update.
  media/             images and files uploaded in the admin. Never overwritten.
  package.json       small manifest cPanel needs. Do not run "NPM Install".
  DEPLOY-HOSTMIDIA.md
```

## 1. Create the database

cPanel → **PostgreSQL Databases**:
1. Create a database, e.g. `dextra`. cPanel prefixes it with your username, e.g. `usuario_dextra`.
2. Create a user with a long password and add it to the database with all privileges.
3. Your connection string is:
   `postgresql://usuario_dbuser:PASSWORD@localhost:5432/usuario_dextra`
   If the password contains `@`, `:` or `/`, replace them with `%40`, `%3A`, `%2F`.

Optional: ask support to run `CREATE EXTENSION vector;` on that database.

## 2. Upload the files

cPanel → **File Manager**:
1. Upload `dextra-site-hostmidia.zip` to your home folder (not `public_html`).
2. Extract it. You get `/home/USUARIO/dextra-site/`.

## 3. Create the Node.js application

cPanel → **Setup Node.js App** → **Create Application**:

| Field | Value |
| --- | --- |
| Node.js version | 24.x (24.22 or newer) |
| Application mode | Production |
| Application root | `dextra-site` |
| Application URL | your domain, e.g. `dextraventures.com` |
| Application startup file | `app/server.js` |

Add these **environment variables** in the same screen:

| Name | Value |
| --- | --- |
| `NODE_ENV` | `production` |
| `DATABASE_URL` | the connection string from step 1 |
| `PAYLOAD_SECRET` | a long random string (at least 32 characters). Keep it safe and never change it. |
| `SITE_URL` | `https://dextraventures.com` |
| `MEDIA_DIR` | `/home/USUARIO/dextra-site/media` |
| `CONTACT_EMAIL` | `contact@dextraventures.com` |
| `ANTHROPIC_API_KEY` | for the AI advisor |
| `SMTP_HOST`, `SMTP_PORT`, `SMTP_USER`, `SMTP_PASS`, `SMTP_FROM` | emails through your HostMídia mailbox (see step 7) |
| `MARKET_EMAIL` | optional: where marketplace alerts go (defaults to `CONTACT_EMAIL`) |
| `WHATSAPP_TOKEN`, `WHATSAPP_PHONE_NUMBER_ID`, `WHATSAPP_APP_SECRET`, `WHATSAPP_VERIFY_TOKEN`, `PARTNER_WHATSAPP` | for the WhatsApp relay |
| `CRON_SECRET` | a long random string; protects the daily-brief job |
| `TURNSTILE_SITE_KEY`, `TURNSTILE_SECRET_KEY`, `GA_ID` | optional: form protection and analytics |

Click **Create**. **Do not click "Run NPM Install"**: everything the site needs is already inside `app/`.

## 4. First start

Click **Start App** (or **Restart**). On the very first start the site:
- creates its database tables,
- loads the brief's content (outcome cases, AI knowledge base, Diesel EN590 questions),
- builds the AI advisor's search index.

This takes about a minute. Then open `https://your-domain/admin` and create the first account; it becomes the admin.

## 5. SSL and domain

- cPanel → **SSL/TLS Status** → run **AutoSSL** for the domain (free certificate).
- If you use Cloudflare, set SSL mode to **Full (strict)** after AutoSSL has issued the certificate.

## 6. Daily market brief

Each morning the site reads the news sources set in Admin → Settings → Daily brief, writes a brief in EN/ES/PT/FR, saves it in Insights as a **draft** and emails you a "Review and publish" link. Nothing is published until you click Publish.

cPanel → **Cron Jobs** → add, once a day (e.g. 06:30):

```
curl -s -X POST -H "Authorization: Bearer YOUR_CRON_SECRET" https://dextraventures.com/api/site/cron/daily-brief/ > /dev/null
```

Needs `ANTHROPIC_API_KEY` and `CRON_SECRET`. To rewrite today's draft, add `?force=1` to the URL.

## 7. Email through your HostMídia mailbox

cPanel → **Email Accounts** → create e.g. `no-reply@dextraventures.com` → **Connect Devices** shows the outgoing (SMTP) server and port. Set:
`SMTP_HOST` (e.g. `mail.dextraventures.com`), `SMTP_PORT` = `465`, `SMTP_USER` and `SMTP_FROM` = the mailbox address, `SMTP_PASS` = its password.
All site emails go out this way: lead alerts, newsletter confirmation, marketplace sign-up confirmation, password resets and notifications.

## 8. Running the marketplace (Admin → Marketplace)

- **Members:** new companies arrive as *pending*. Open the member, check the documents they uploaded (Verification documents), screen them, then set **Status → verified**. They get an email and can trade.
- **Commodity listings:** new listings arrive as *pending*. Review and set **Status → live** (or *rejected* with a reason). Listings are anonymous to other members.
- **Listing interests:** each interest emails you both companies' details. Introduce them yourself, then set the interest to *introduced*.
- **Service requests and quotes:** run themselves. Verified providers in the matching service get an email, send quotes, and the requester hires one; both receive each other's contacts. Payment is agreed between them, outside the site.
- Expired listings (60 days by default) close automatically in the daily cron job.

## Updating the site

1. On your computer, in the project folder: `bash scripts/pack-hostmidia.sh`
2. In File Manager, delete the old `dextra-site/app` folder, upload the new zip and extract it over `dextra-site` (the `media/` folder stays).
3. Setup Node.js App → **Restart**. New database changes apply automatically on start.

## Backups

- cPanel → **Backup**: download a full backup regularly; it includes the PostgreSQL database and `media/`.
- Before every update, download a database backup from cPanel → **Backup** → PostgreSQL databases.

## When something goes wrong

- **Logs:** `dextra-site/stderr.log` (File Manager), or the log link in Setup Node.js App.
- **"Cannot find module" or a crash on start:** check the startup file is `app/server.js` and the Node version is 24.22+.
- **Database error on start:** check `DATABASE_URL` (user, password, database name with the cPanel prefix).
- **Images don't process:** the package includes the Linux image library; if the server is not x64 Linux, tell me and I'll rebuild for it.
- **Out of memory:** move up a plan (BUSINESS) or ask support to raise the Node memory limit.
