# thechat.online — cPanel + MySQL deployment

This project is a **Next.js 16 / Node.js** application, not a PHP script. Your cPanel hosting plan must include **Setup Node.js App** (or an equivalent Node.js application manager), Node.js **20.9+** (prefer Node 22), SSH/Terminal or a way to run npm commands, and outbound HTTPS access to the AI/payment APIs. Standard PHP-only shared hosting cannot run this app.

## 1. Create the MySQL database

1. In cPanel → **MySQL Database Wizard**, create a database and database user.
2. Assign the user to the database with **ALL PRIVILEGES**.
3. Keep the actual cPanel-prefixed database and username exactly as cPanel displays them.
4. Do not include `CREATE DATABASE` in SQL/phpMyAdmin; cPanel provisions the database for you.

## 2. Upload and install

1. Upload this ZIP to a directory outside `public_html` where possible, e.g. `/home/CPANELUSER/thechat` and extract it. Do not upload `.env` from another environment.
2. In cPanel → **Setup Node.js App**, create an application using Node 20.9+ / 22, production mode, and the application root above. Set the application URL/domain to `thechat.online` if the panel supports it.
3. Set the startup file to `app-start.cjs`. This wrapper changes into `.next/standalone` before loading Next.js. The build prepares the standalone assets automatically.
4. Use the corrected dependency versions in this package: `nodemailer` is pinned to the NextAuth v4-compatible major (`^6.10.1`) and `@types/nodemailer` to `^6.4.17`. If you previously ran a failed install, remove the stale npm lockfile and incomplete dependencies before reinstalling: `rm -rf node_modules package-lock.json`, then run `npm install`. Do not use `--force` or `--legacy-peer-deps` to hide peer conflicts. If your host provides a specific “Run NPM Install” button, use it after replacing the package files.
5. Copy `.env.example` to `.env` if shell access is available, but preferably enter secrets in the Node.js App environment-variable UI. Never commit or publish real credentials.

Set these environment variables (replace the placeholders):

```env
DATABASE_URL=mysql://CPANEL_DB_USER:URL_ENCODED_PASSWORD@localhost:3306/CPANEL_DB_NAME
AUTH_SECRET=LONG_RANDOM_SECRET
APP_URL=https://thechat.online
NEXTAUTH_URL=https://thechat.online
NODE_ENV=production
ADMIN_EMAIL=you@example.com
ADMIN_PASSWORD=ONE_TIME_STRONG_PASSWORD
```

URL-encode special characters in the database password (for example `@` becomes `%40`). Use the database host supplied by your hosting company if it is not `localhost`.

## 3. Generate Prisma client, create tables, build

Run from the project root in cPanel Terminal (or the host's Node.js environment):

```bash
npm install
npx prisma generate
npx prisma db push
npm run db:seed
npm run build
npm run cpanel:check
```

`prisma db push` creates/updates the tables from `prisma/schema.prisma`; it does not import the old SQLite `db/custom.db`. Back up any existing production data before changing schemas. Do not use `--accept-data-loss` on production.

If `npm run db:seed` fails because the host uses a different TypeScript runner, run `npx tsx prisma/seed.ts` after installing dependencies. Keep `ADMIN_PASSWORD` private and change it after initial setup if the application exposes an admin password workflow.

## 4. Start the application

The build creates `.next/standalone/server.js`. Set `PORT` to the port assigned by cPanel (often supplied automatically), then start with:

```bash
NODE_ENV=production node .next/standalone/server.js
```

The project includes `app-start.cjs` in the application root. It performs the required standalone startup setup:

```js
const path = require('node:path');
const standaloneDir = path.join(__dirname, '.next', 'standalone');
process.chdir(standaloneDir);
require(path.join(standaloneDir, 'server.js'));
```

Set the Node.js App startup file to `app-start.cjs` and restart the app. Some cPanel providers require a Passenger-specific startup file and do not support arbitrary Next.js standalone processes; follow the provider's Node.js application instructions. If the host does not support persistent Node.js apps or WebSocket/SSE streaming, move to a Node-capable host.

## 5. Connect domain and SSL

- Point `thechat.online` DNS to the hosting account as directed by the host.
- In cPanel → Domains, ensure the domain is assigned to the app according to the host's Node.js/Passenger setup.
- Enable AutoSSL/Let's Encrypt.
- Force HTTPS only after the certificate is active.

## 6. Smoke tests

After starting, open `https://thechat.online/api/health`. Expected JSON should report `status: "ok"` and `database: "up"`. If it reports `degraded`, verify `DATABASE_URL`, database privileges, generated Prisma Client, and the cPanel error log.

Also test registration/login, creating a conversation, sending a prompt with a configured AI provider, admin login, billing in sandbox mode, and mobile layout. AI responses require valid provider credentials and provider/model configuration; PayPal and crypto payments require valid merchant credentials and webhook configuration.

## Important cPanel limits

- Node.js support varies by host; some low-cost shared plans do not support Next.js 16 or long-running streaming requests.
- Chat streaming uses server-sent events (SSE). Confirm the host/proxy does not buffer or terminate streaming responses.
- Upload limits, process memory, execution time, outbound connections, and app restart behavior are controlled by the hosting provider.
- Never expose `.env`, `.git`, `prisma/`, logs, database backups, or source files through a public web directory.
- Rotate any real API/payment/database secrets that were ever included in an uploaded archive or committed to Git.


## Additional production features

- **Developer API keys:** `Dashboard → Developer API` creates up to five active keys per account. Keys are random, stored as SHA-256 hashes, shown once, and revocable. Keep them server-side only.
- **Developer chat endpoint:** `POST /api/v1/chat` accepts `{ "model": "MODEL_SLUG", "messages": [{"role":"user","content":"Hello"}] }` and returns an OpenAI-compatible completion object. Use `Authorization: Bearer tchat_live_...`. Streaming is not currently implemented on this endpoint. Requests still pass through plan/model gates, rate limits, and provider spend guards.
- **Conversation export:** existing chat UI supports Markdown and JSON export. The authenticated endpoint `GET /api/conversations/{id}/export?format=md|txt` also supports downloads.
- **Schema update:** back up MySQL first, then run `npx prisma generate` and `npx prisma db push`. Review Prisma output before accepting any destructive change.

The app requires cPanel Node.js application support; ordinary PHP-only hosting is insufficient. Confirm your provider supports Node.js 22 (or a compatible Next.js 16 runtime) and persistent Node.js processes. Official cPanel instructions: https://docs.cpanel.net/knowledge-base/web-services/how-to-install-a-node.js-application/
