# cPanel Deployment for erp.advertleads.com

This guide deploys PAK ERP Web separately from the existing `www.advertleads.com` e-commerce store.

## 1. Create the MySQL database

In **cPanel > MySQL Databases**:

1. Create a new database, for example `advertle_pakerp` (cPanel may add the account prefix automatically).
2. Create a new database user with a long URL-safe password.
3. Add that user to the new database with **ALL PRIVILEGES**.
4. Record the complete database name, complete database username, and password. Do not reuse the e-commerce database or its database user.

## 2. Upload the separate web project

1. Upload the contents of `pak-erp-web` to a private folder outside `public_html`, for example `/home/advertle/pak-erp-web`.
2. Do not upload `node_modules`, `.env`, `secrets`, `apps/api/dist`, or `apps/web/dist` from Windows. cPanel must install and build Linux dependencies itself.
3. Keep the e-commerce folders under `public_html` unchanged.

## 3. Create the Node.js application

In **cPanel > Setup Node.js App**:

- Node.js version: `20.20+`
- Application mode: `Production`
- Application root: `pak-erp-web`
- Application URL: `erp.advertleads.com`
- Application startup file: `app.js`

Create the application. cPanel will show the application virtual environment and a button to restart the app.

## 4. Configure environment variables

In the Node.js application environment-variable screen, add:

```text
NODE_ENV=production
PORT=<the port supplied by cPanel>
PUBLIC_DOMAIN=erp.advertleads.com
CORS_ORIGIN=https://erp.advertleads.com
DATABASE_URL=mysql://DATABASE_USER:URL_ENCODED_PASSWORD@localhost:3306/DATABASE_NAME
JWT_SECRET=<at least 32 random characters>
BOOTSTRAP_TOKEN=<one-time 24+ character setup token>
INSTALLATION_ID=<unique 12+ character server ID>
CPANEL_AUTOBUILD=true
PAK_ERP_APP_ROOT=/home/advertle/erp
NPM_CONFIG_PRODUCTION=false
PAK_ERP_LICENSE_PUBLIC_KEY_PATH=/home/advertle/erp/secrets/license-public.pem
```

Replace `DATABASE_USER` and `DATABASE_NAME` with the full cPanel-prefixed values. If the password contains special characters, URL-encode it before adding it to `DATABASE_URL`.

Create `/home/advertle/pak-erp-web/secrets/license-public.pem` and upload only the PAK ERP licensing public key. Never upload `private.pem` to cPanel.

## 5. Install and build on cPanel

Open **Terminal** in cPanel, then run:

```bash
cd /home/advertle/pak-erp-web
npm ci --no-audit --no-fund
npm run db:generate
npm run build
npm run db:push
```

`db:push` is run once only when creating the new empty PAK ERP MySQL database. Future production upgrades must use reviewed Prisma migrations.

## 6. Start and activate

1. Return to **Setup Node.js App** and click **Restart**.
2. Open `https://erp.advertleads.com`.
3. Complete the initial tenant/company/administrator setup using the bootstrap token.
4. Send the domain and installation ID shown on the activation screen to PAK ERP administration.
5. Paste the issued signed license key to activate the ERP.

## Troubleshooting

- A `503` page usually means the Node app has not been restarted, `npm run build` did not complete, or an environment variable is missing.
- A database connection error usually means the cPanel-prefixed MySQL name/user or URL-encoded password is wrong.
- Do not set the application root to `/home/advertle/public_html`; that belongs to the existing e-commerce store.
