# cPanel Deployment

This backend is PostgreSQL-only. cPanel MySQL databases cannot run it without a database migration rewrite.

Use one of these layouts.

## Recommended Layout: Full Platform On cPanel

Upload the project with both folders:

```text
compt/
  client/
  server/
```

In cPanel `Setup Node.js App`:

```text
Application root: compt
Application URL: https://groupemekahli.com
Application startup file: server/src/index.js
Node.js version: 20.x or newer
Application mode: Production
```

Run locally before upload or from cPanel terminal:

```bash
npm install
npm run build:cpanel
npm --workspace server run check:cpanel
```

The `build:cpanel` script builds the React app and copies `client/dist` into `server/client/dist`.
That lets the server folder run by itself on cPanel. The project and server `.npmrc` files use `omit=optional`, so shared hosting skips optional packages like Puppeteer.

## API-Only Layout

If cPanel hosts only the API, upload the `server/` folder and configure:

```text
Application root: server
Application URL: https://api.your-domain.com
Application startup file: src/index.js
Node.js version: 20.x or newer
Application mode: Production
```

Then host the frontend separately and set:

```text
CLIENT_URL=https://your-frontend-domain.com
CLIENT_URLS=https://your-frontend-domain.com
```

## Required Environment Variables

Copy values from `.env.cpanel.example` into cPanel's environment variable screen.

Minimum required:

```text
NODE_ENV=production
PORT=<cPanel assigned port or 5000>
DB_URL=postgresql://pmarvkqh_admin:<DB_PASSWORD>@127.0.0.1:5432/pmarvkqh_mekahli
DB_SSL=false
DB_SSL_REJECT_UNAUTHORIZED=false
JWT_SECRET=<long random secret>
CLIENT_URL=https://groupemekahli.com
CLIENT_URLS=https://groupemekahli.com,https://www.groupemekahli.com
DISABLE_PDF_EXPORTS=true
```

Do not use the cPanel MySQL database for `DB_URL`.
Use `127.0.0.1` only if PostgreSQL is running on the same cPanel server. If the database is external, replace it with the real PostgreSQL host.

## PostgreSQL Setup

Use an external PostgreSQL provider or VPS PostgreSQL. Examples: VPS PostgreSQL, Supabase, Neon, Render PostgreSQL, Railway PostgreSQL.

After creating the database, run the SQL files from `server/db` in this order:

```bash
psql "$DB_URL" -f server/db/init.sql
psql "$DB_URL" -f server/db/init_fiscal.sql
psql "$DB_URL" -f server/db/init_fiscal_mapping.sql
psql "$DB_URL" -f server/db/init_mgx.sql
psql "$DB_URL" -f server/db/init_tracking.sql
psql "$DB_URL" -f server/db/seed_accounts.sql
```

If your PostgreSQL provider requires SSL, use:

```text
DB_SSL=true
DB_SSL_REJECT_UNAUTHORIZED=false
```

For local cPanel PostgreSQL on `127.0.0.1`, use:

```text
DB_SSL=false
```

## Uploads

Uploaded files are stored under:

```text
server/uploads
server/src/uploads
```

Make sure these directories are writable by the cPanel Node.js process.

## Known cPanel Limitations

PDF exports use `puppeteer`. Shared cPanel hosting usually does not allow Chromium, so this deployment disables PDF export endpoints:

```text
DISABLE_PDF_EXPORTS=true
```

With this setting, PDF endpoints return HTTP 503 instead of launching Chromium. Excel exports and normal API routes continue to work.

If your host later provides Chromium, remove `DISABLE_PDF_EXPORTS=true` and configure:

```text
PUPPETEER_EXECUTABLE_PATH=/path/to/chromium
```

`pg_dump` backups require PostgreSQL client tools on the host. If unavailable, backup from your PostgreSQL provider instead.

Socket.IO needs WebSocket/proxy support from the hosting provider. If the realtime tracking socket fails on cPanel, keep the API on a VPS.

## Start Or Restart

From cPanel Node.js App screen:

1. Run `npm install`.
2. Run `npm run build:cpanel` before uploading, or upload an already prepared `server/client/dist`.
3. Run `npm --workspace server run check:cpanel`.
4. Restart the Node.js app.
5. Test `https://your-domain.com/health`.

## Upload Only Server Folder

If you want to upload only `server/` to cPanel, run locally first:

```bash
npm install
npm run build:cpanel
```

Then upload:

```text
server/
  client/dist/
  db/
  scripts/
  src/
  uploads/
  package.json
  .npmrc
```

In cPanel:

```text
Application root: server
Application startup file: src/index.js
```
