# Deploy DAR ZELLIGE on cPanel (Node.js)

This guide deploys the storefront as a Node.js application managed by cPanel's **Setup Node.js App** (Phusion Passenger). The repository includes `app.js`, the Passenger entry point. Your normal local command stays `npm start`; cPanel uses `app.js`.

## Before you start

- cPanel with **Setup Node.js App** and **Terminal/SSH** (recommended for the production build).
- A domain or subdomain with SSL enabled. A subdomain such as `zellige.example.com` is recommended so the project does not replace your main portfolio site.
- Node.js **22 LTS** selected in cPanel. Node 24 also works if your host offers it; avoid unsupported/old Node versions.
- Your project files. Never upload `node_modules`, `.next`, `.env`, `prisma/dev.db`, or `prisma/prod.db` from your computer.

> **Application URL:** use a domain or subdomain root, not a folder such as `example.com/portfolio`. Next.js uses absolute routes; a subfolder requires a deliberate `basePath` change. Link to the subdomain from your portfolio instead.

## 1. Create the cPanel application

1. In cPanel, create the subdomain first if you are using one (for example `zellige.example.com`).
2. Open **Setup Node.js App** and click **Create Application**.
3. Set:
   - **Node.js version:** 22 (or highest supported)
   - **Application mode:** Production
   - **Application root:** `apps/dar-zellige` (outside `public_html`)
   - **Application URL:** `zellige.example.com`
   - **Application startup file:** `app.js`
4. Create the application. If cPanel shows a virtualenv activation command, copy it; the path normally looks like:

```bash
source /home/YOUR_USER/nodevenv/apps/dar-zellige/22/bin/activate
```

Keeping the app outside `public_html` prevents `.env`, source files and the SQLite database from being downloadable.

## 2. Upload the source

Put the project in `/home/YOUR_USER/apps/dar-zellige` using cPanel **File Manager** (upload a ZIP, then extract) or cPanel **Git Version Control**.

Exclude local-only files:

- `node_modules/`
- `.next/`
- `.env`
- `prisma/dev.db` and `prisma/prod.db`
- temporary files and local backups

`public/uploads/` may be uploaded if it contains products you want to keep. Otherwise make sure the directory exists on the server.

## 3. Create the production environment file

In the application root create `.env` (not inside `public_html`):

```bash
cd /home/YOUR_USER/apps/dar-zellige
cat > .env <<'EOF'
DATABASE_URL="file:./prod.db"

AUTH_SECRET="PASTE_A_LONG_RANDOM_SECRET"
AUTH_TRUST_HOST=true
NEXTAUTH_URL="https://zellige.example.com"
NEXT_PUBLIC_SITE_URL="https://zellige.example.com"

# Optional: persistent cloud uploads
CLOUDINARY_CLOUD_NAME=""
CLOUDINARY_API_KEY=""
CLOUDINARY_API_SECRET=""

# Optional email
SMTP_HOST=""
SMTP_PORT=587
SMTP_SECURE=false
SMTP_USER=""
SMTP_PASSWORD=""
SMTP_FROM="DAR ZELLIGE <hello@zellige.example.com>"

# Used only when you run the seed command
SEED_ADMIN_EMAIL="admin@example.com"
SEED_CLIENT_EMAIL="client@example.com"
SEED_PASSWORD="REPLACE_WITH_A_STRONG_PASSWORD"

# Admin roles for the demo. Use a long, unique superadmin password.
SEED_SUPERADMIN_EMAIL="owner@example.com"
SEED_SUPERADMIN_PASSWORD="REPLACE_WITH_A_LONG_RANDOM_SUPERADMIN_PASSWORD"
DEMO_ADMIN_EMAIL="admin@example.com"
DEMO_ADMIN_PASSWORD="REPLACE_WITH_A_DEMO_PASSWORD"
EOF
chmod 600 .env
```

Generate the auth secret with:

```bash
openssl rand -base64 32
```

`DATABASE_URL="file:./prod.db"` resolves to `prisma/prod.db`. The database helper, seed command and Prisma CLI all use this same file.

Use the exact HTTPS URL in `NEXTAUTH_URL` and `NEXT_PUBLIC_SITE_URL`; do not add a trailing slash. Set the same values in cPanel's environment-variable UI if your host provides it. Changing a `NEXT_PUBLIC_*` value requires a rebuild.

## 4. Install, create the database and build

Use cPanel **Terminal** (or SSH). Activate the exact Node environment cPanel showed you:

```bash
source /home/YOUR_USER/nodevenv/apps/dar-zellige/22/bin/activate
cd /home/YOUR_USER/apps/dar-zellige
node -v
npm ci
npx prisma generate
npx prisma db push
npm run db:seed
npm run build
```

`npm run db:seed` is **destructive**: it deletes and recreates the demo catalogue, users and orders. Run it only on the first deployment or when you deliberately want to reset the demo. For later deployments, skip it.

If you already have a database and only need to create or reset the two admin accounts, run:

```bash
npm run admin:setup
```

This does **not** delete products or orders. It creates:

- **Demo admin** (`role = ADMIN`) — can open `/admin` and browse every screen, but create/update/delete is blocked on both the UI and the server.
- **Superadmin** (`role = SUPERADMIN`) — full access. If `SUPERADMIN_PASSWORD` is not set, the script generates a strong random password and prints it once.

`npm ci` installs development dependencies too; the build needs Tailwind, TypeScript and Prisma. Do not use `--omit=dev` for the build.

If the build runs out of memory:

```bash
NODE_OPTIONS=--max-old-space-size=2048 npm run build
```

## 5. Permissions and restart

```bash
mkdir -p public/uploads tmp
chmod 775 public/uploads
touch tmp/restart.txt
```

Then click **Restart** in Setup Node.js App, or reload the site. Passenger uses `tmp/restart.txt` to reload the application.

## 6. First-run checks

Open and verify:

- `https://zellige.example.com/fr` — storefront and images
- `https://zellige.example.com/fr/shop` — product listing
- `https://zellige.example.com/fr/login` — sign in with the seeded admin/client account
- `https://zellige.example.com/admin` — admin dashboard after signing in
- Product image upload in the admin, if you plan to use it

If the site loads without CSS, the browser is probably not reaching the built app or the `public` directory is incomplete. Check the application log first.

## 7. Future deployments

Back up the database and uploads first:

```bash
cp prisma/prod.db prisma/prod.db.backup-$(date +%F)
tar -czf uploads-backup-$(date +%F).tar.gz public/uploads
```

Then upload the changed files and rebuild:

```bash
source /home/YOUR_USER/nodevenv/apps/dar-zellige/22/bin/activate
cd /home/YOUR_USER/apps/dar-zellige
npm ci
npx prisma generate
npm run build
touch tmp/restart.txt
```

Run `npx prisma db push` after a schema change. Do **not** seed again unless you intend to erase the live demo data.

## 8. Troubleshooting

| Symptom | Check |
| --- | --- |
| 503 / Passenger error page | Startup file is `app.js`; application root is correct; `npm run build` created `.next/BUILD_ID`; restart the app. |
| `Could not find a production build` | Build was not run on the server, or `.next` is missing. Run `npm run build` in the application root. |
| Prisma engine / `query_engine` error | Do not upload `node_modules` or `.next` from Windows. Run `npm ci` and `npx prisma generate` on the Linux server. |
| SQLite read-only or locked error | `prisma/` and `prisma/prod.db` must be owned and writable by the cPanel user; never run two apps against the same SQLite file. |
| Login redirects fail or reports an untrusted host | `AUTH_SECRET`, `AUTH_TRUST_HOST=true`, `NEXTAUTH_URL` and the site URL must all use the final HTTPS domain. |
| Images return 500 | Ensure `sharp` installed on the server; run `npm rebuild sharp`, then restart. Verify `public/uploads` exists and is writable. |
| Admin upload succeeds but image disappears after redeploy | Local uploads are stored in `public/uploads`. Configure Cloudinary for durable uploads, or include that directory in backups. |
| Build is killed / OOM | Retry with `NODE_OPTIONS=--max-old-space-size=2048`; otherwise build on a host with more memory and deploy the same Node minor version. |
| Env changes have no effect | Restart Passenger; remember `NEXT_PUBLIC_*` values are compiled at build time, so rebuild after changing them. |

## 9. Security and demo limits

- **Demo admin (`ADMIN`) is read-only.** Give these credentials to your client. Every mutation server action requires `SUPERADMIN`, so the demo account cannot insert, update or delete anything even with a crafted request.
- **Keep the superadmin credentials private.** Set a long random `SEED_SUPERADMIN_PASSWORD` / `SUPERADMIN_PASSWORD` in `.env` (for example `openssl rand -base64 36`) and never commit or email it. Do not give the client the superadmin email.
- Change `SEED_PASSWORD` before seeding and do not publish the admin credentials. The seed gives the demo admin and all seeded customers the same password by default.
- Keep `.env`, `prisma/prod.db` and `public/uploads` outside public web access. cPanel AutoSSL should be enabled and HTTP redirected to HTTPS.
- SQLite is sufficient for a portfolio demo. For real traffic, move to PostgreSQL (or MySQL after a Prisma provider migration), use a shared rate limiter and configure a real email/payment provider.
- Cloudinary is recommended for uploads that must survive redeploys. Without it, include `public/uploads` in every backup.
