# PetTua — Live cPanel Deploy (step by step)

This is a **production** deploy using only cPanel (no SSH required):

- File Manager
- Setup Node.js App
- Environment Variables
- Run NPM Install
- Run JS Script
- Stop / Start
- phpMyAdmin

**Do not build the site on the server.** The zip already contains `.next`.

---

## What you upload

From this computer, after packaging, use:

`dist-cpanel/pettua-cpanel.zip`  
(or `dist-cpanel/project-prebuilt.zip`)

Inside the extracted app you need:

| Path | Purpose |
|------|---------|
| `server.js` | Node startup file |
| `.next/` | Prebuilt website (enable **Show Hidden Files**) |
| `public/` | Images, packshots, brand |
| `deploy/pettua-import.sql` | Full database: tables + settings + catalog |
| `DEPLOYMENT.md` | This guide |

---

## Step 1 — Create the MySQL database

1. cPanel → **MySQL® Databases**
2. Create a database (example: `user_pettua`)
3. Create a database user + strong password
4. Add that user to the database with **ALL PRIVILEGES**
5. Write down: database name, user, password, host (`localhost`)

---

## Step 2 — Import SQL (phpMyAdmin)

1. cPanel → **phpMyAdmin**
2. Click your database in the left sidebar
3. **Import** → Choose file → `deploy/pettua-import.sql`
4. Click **Go** and wait until it succeeds

This creates tables, PetTua settings, policy pages, the product catalog, and one admin user.

If phpMyAdmin limits file size, import `deploy/pettua-import.sql` anyway (it is one file and should stay under typical limits). Optional extra: `deploy/pettua-catalog.sql` is the catalog-only file if you ever need to refresh products later.

---

## Step 3 — Upload and extract the zip

1. cPanel → **File Manager**
2. Settings → enable **Show Hidden Files**
3. Open the Node.js **application root** (the folder that will contain `server.js`)
4. Upload `pettua-cpanel.zip` (or `project-prebuilt.zip`)
5. Right-click → **Extract**
6. Confirm these sit **in the application root** (not inside a nested folder):
   - `server.js`
   - `package.json`
   - `.next`
   - `public`
   - `prisma`
   - `scripts`
   - `deploy`

If everything landed in a subfolder (`pettua/` or `fliemart/`), move the contents up one level into the real application root.

---

## Step 4 — Setup Node.js App

1. cPanel → **Setup Node.js App** → Create / Edit
2. Set:
   - **Node.js version:** 20 or newer
   - **Application mode:** Production
   - **Application root:** the folder from Step 3
   - **Application URL:** your live domain (or subdomain)
   - **Application startup file:** `server.js`
3. Save

---

## Step 5 — Environment variables

In the Node.js App screen, add:

### Required

| Name | Value |
|------|--------|
| `NODE_ENV` | `production` |
| `AUTH_SECRET` | long random string (32+ characters) |
| `NEXT_PUBLIC_SITE_URL` | `https://your-domain.com` (no trailing slash) |
| `DB_HOST` | `localhost` |
| `DB_PORT` | `3306` |
| `DB_USER` | MySQL user from Step 1 |
| `DB_PASSWORD` | MySQL password from Step 1 |
| `DB_NAME` | database name from Step 1 |

If the host gives you a full `DATABASE_URL`, you may set that instead of the `DB_*` fields.

### Stripe Hosted Checkout (required for live payments)

| Name | Value |
|------|--------|
| `STRIPE_SECRET_KEY` | Stripe **live** secret key (`sk_live_…`) |
| `NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY` | Stripe **live** publishable key (`pk_live_…`) |
| `STRIPE_WEBHOOK_SECRET` | webhook signing secret (`whsec_…`) |

Create the webhook in Stripe Dashboard → Developers → Webhooks:

- URL: `https://your-domain.com/api/webhooks/stripe`
- Events: `checkout.session.completed`, `payment_intent.succeeded`, `payment_intent.payment_failed`

Do **not** set `ALLOW_DEMO_CHECKOUT`.  
Do **not** set `ENABLE_STATUS_ENDPOINT` on a stable live site.

---

## Step 6 — Install packages

1. In Setup Node.js App, click **Run NPM Install**
2. Wait until it finishes

This installs Node packages. It is **not** a website rebuild.

---

## Step 7 — Helper scripts (Run JS Script)

Run these one at a time from Setup Node.js App → **Run JS Script**:

1. `scripts/cpanel-prisma-generate.js`
2. `scripts/cpanel-fix-paths.js`
3. `scripts/cpanel-fix-perms.js`
4. `scripts/cpanel-doctor.js`

If doctor reports a failure, fix that before starting the app.

---

## Step 8 — Start

1. Click **Stop**
2. Click **Start**  
   (prefer Stop → Start over Restart alone)

---

## Step 9 — Verify

Open:

1. `https://your-domain.com/` — PetTua storefront
2. `https://your-domain.com/api/health` — must be JSON, similar to:

```json
{
  "ok": true,
  "app": "running",
  "checks": { "nextPrepared": true, "prismaClient": true }
}
```

3. `https://your-domain.com/login` — email/password only (no demo accounts shown)
4. `https://your-domain.com/checkout` — Continue to Stripe (after adding a product)

### First admin login (change immediately)

| Field | Value |
|-------|--------|
| Email | `admin@pettua.com` |
| Password | `admin123` |

Then: **Admin → Settings** (branding, Stripe keys if you prefer storing them in admin) → **change the admin password**.

Customers register on `/register`. Do not leave the default admin password in place.

---

## Step 10 — Stripe on the live site

1. Admin → Stripe: enable Stripe and save live keys (or use env vars from Step 5)
2. Add a product to the cart → Checkout → **Continue to Stripe**
3. Complete a **live** or **test** payment on Stripe Hosted Checkout (use test keys only on a staging URL)
4. Confirm you return to `/thank-you/?session_id=…` with the confirmation page

---

## Common problems

| What you see | What to do |
|--------------|------------|
| NPM Install then `text/plain` / Internal Server Error | Re-extract the zip, run prisma-generate + fix-paths, Stop → Start. `/api/health` must stay JSON. |
| Cannot find module `next` | Application root is wrong, or NPM Install did not finish |
| `EACCES` on `.next/static` | Re-extract; run `scripts/cpanel-fix-perms.js` |
| Health: prisma client missing | Run `scripts/cpanel-prisma-generate.js` |
| Health: database error / empty shop | Check `DB_*`. Re-import `deploy/pettua-import.sql` |
| Login page but cannot sign in | Import SQL first; use `admin@pettua.com` then change password |
| Stripe not configured | Add live keys in env **or** Admin → Stripe, then Stop → Start |
| Build / out of memory | You tried to build on cPanel — don’t. Upload this prebuilt zip |

---

## Security

- Change `admin@pettua.com` password after first login
- Keep `AUTH_SECRET` and Stripe secrets only in cPanel Environment Variables
- `public/uploads` is the only folder that should stay writable
- After any env or file change: **Stop → Start**
