# SiloMart — cPanel Deployment Guide (No SSH / No Terminal)

This guide is for deploying SiloMart using **only** the cPanel website tools:

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

You do **not** need SSH, Command Prompt, Docker, or PM2.

---

## Before you begin (on your computer)

Someone with the project must prepare a package on a normal computer:

1. Install Node.js 20+ locally  
2. In the project folder, run:
   - `npm install`
   - `npm run deploy:local-package`
3. Find the file under `dist-cpanel/`:
   - `project-prebuilt.zip` (cPanel File Manager)
   - `project-prebuilt.tar.gz` (preferred if ZIP extract fails)

A printed walkthrough is also in **LIVE-DEPLOY.md**.

That archive already contains the built website (`.next`).  
**Do not build the site on cPanel.**

---

## Step 1 — Upload the package

1. Open **cPanel → File Manager**
2. Turn on **Show Hidden Files** (Settings / Preferences)
3. Go to the folder used by your Node.js application (application root)
4. Upload `project-prebuilt.zip` (or `project-prebuilt.tar.gz`)
5. Right-click the archive → **Extract**
6. Confirm these exist in the application root:
   - `server.js`
   - `package.json`
   - `.next` (hidden folder — only visible with Show Hidden Files)
   - `public`
   - `prisma`
   - `scripts`
   - `deploy`

If files extracted into a subfolder (for example `silomart/`), move everything up into the real application root.

---

## Step 2 — Create / import the database

1. Open **cPanel → MySQL® Databases** (or MariaDB)
2. Create a database and a database user
3. Add the user to the database with **ALL PRIVILEGES**
4. Open **phpMyAdmin**
5. Select your database
6. Import **one file**:

`deploy/silomart-import.sql`

This creates all tables, SiloMart settings, policy pages, and starter admin/customer users.  
Grocery bins on the storefront ship with the app. Extra products can be added in Admin.

---

## Step 3 — Setup Node.js App

1. Open **Setup Node.js App**
2. Create / edit the application:
   - **Node.js version:** 20 or newer  
   - **Application mode:** Production  
   - **Application root:** the folder where you extracted the files  
   - **Application startup file:** `server.js`  
3. Save

---

## Step 4 — Environment Variables

In the Node.js App screen, add these variables.

### Required

| Name | Example / notes |
|------|------------------|
| `NODE_ENV` | `production` |
| `AUTH_SECRET` | long random password-like string |
| `NEXT_PUBLIC_SITE_URL` | `https://your-domain.com` |
| `DB_HOST` | usually `localhost` |
| `DB_PORT` | usually `3306` |
| `DB_USER` | your MySQL user |
| `DB_PASSWORD` | your MySQL password |
| `DB_NAME` | your database name |

You may set `DATABASE_URL` instead of the `DB_*` fields if your host gives you one full connection string.

### Required for Stripe payments

| Name | Notes |
|------|--------|
| `STRIPE_SECRET_KEY` | from Stripe Dashboard (secret) |
| `NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY` | publishable key |
| `STRIPE_WEBHOOK_SECRET` | from Stripe webhook endpoint |

### Keep off unless debugging

| Name | Value |
|------|--------|
| `ENABLE_STATUS_ENDPOINT` | leave unset / `false` |
| `ALLOW_DEMO_CHECKOUT` | leave unset / `false` |

Never put real secrets into `DEPLOYMENT.md`, chat, or screenshots.

---

## Step 5 — Install dependencies

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

This installs packages and runs Prisma generate.  
It is **not** a website build step.

---

## Step 6 — One-time helper scripts

Still in **Setup Node.js App**, use **Run JS Script** (one at a time):

1. `scripts/cpanel-prisma-generate.js`  
   Generates Prisma client (or restores bundled Linux engines)  
2. `scripts/cpanel-fix-paths.js`  
   Fixes Windows build paths for Linux hosting (also auto-runs on boot)  
3. `scripts/cpanel-fix-perms.js`  
   Sets normal permissions (folders 755, files 644)  
4. `scripts/cpanel-doctor.js`  
   Checks setup without printing secret values  

If doctor reports failures, fix those first.

---

## Step 7 — Start the app

1. Click **Stop**
2. Click **Start**  
   (Prefer Stop → Start instead of only Restart)

---

## Step 8 — Verify

Open these in your browser:

1. `https://your-domain.com/`  
   - Should show the SiloMart storefront
2. `https://your-domain.com/api/health`  
   - Must be **JSON** (`application/json`), for example:

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

3. `https://your-domain.com/login`  
4. Admin login (change the seed password immediately if still using demo seed data)

Optional Stripe webhook:

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

---

## Common problems

| What you see | What to do |
|--------------|------------|
| NPM Install: content-type changed to `text/plain` | App crashed after install. Re-upload latest package, run prisma-generate + fix-paths, Stop→Start. Then `/api/health` must stay JSON. |
| Site shows `Internal Server Error` (plain text) | Run `scripts/cpanel-prisma-generate.js`, then Stop→Start. Confirm `.next` sits next to `server.js`. |
| App inaccessible / 503 | Stop → Start; run doctor; confirm `server.js` is the startup file |
| Cannot find module `next` | Confirm extract path = application root; Run NPM Install again |
| `EACCES` on `.next/static` | Re-upload using `.tar.gz` (not Windows ZIP); run fix-perms |
| Health says prisma_client_missing | Run `scripts/cpanel-prisma-generate.js` |
| Health says db unchecked / pages empty | Check `DB_*` values; import `deploy/silomart-import.sql` in phpMyAdmin |
| Build / WASM out of memory | You tried to build on the server — don’t. Upload prebuilt `.next` |

---

## Writable folder

Only `public/uploads` should be writable for media uploads.  
Do not make the whole application world-writable.

---

## Security reminders

- Change default admin password after first login  
- Keep `AUTH_SECRET` and Stripe secrets only in cPanel Environment Variables  
- Leave `/__status` disabled in normal production  
- Prefer Stop → Start after every env or file change  
