# Indian Books Worldwide - cPanel Deployment Guide

This guide explains how to deploy the **Indian Books Worldwide** monorepo to your cPanel hosting account using the optimized production bundle (`production-package.zip`).

---

## Prerequisites
1. **Node.js Selector** enabled in cPanel (usually found as "Setup Node.js App").
2. **MySQL Database Wizard** access in cPanel.
3. Node.js version recommended: **v18+** or **v20+** (the project is compatible up to Node v24).

---

## Step 1: Upload and Extract Code
1. Log in to cPanel.
2. Open **File Manager** and navigate to your home directory (e.g., `/home/username/`).
3. Create a new folder for your application (e.g., `/home/username/ibw-app/`).
4. Upload the compiled `production-package.zip` into this folder.
5. Right-click on the uploaded zip file and select **Extract**.

---

## Step 2: Set up Database (MySQL)
1. Go to cPanel -> **MySQL Database Wizard**.
2. Create a new database (e.g., `username_ibw_db`).
3. Create a database user (e.g., `username_ibw_user`) and a secure password.
4. Associate the user with the database and grant **All Privileges**.
5. Save the credentials; you will need them for the environment config.

---

## Step 3: Configure Environment Variables
You need to set up environment variables for both backend and frontend applications.

### Backend Configurations
Rename `apps/backend/.env.example` to `.env` inside your extracted directory or configure these using the cPanel Node.js App interface:
* `NODE_ENV=production`
* `PORT=5000` (Passenger will manage the port binding automatically)
* `DATABASE_URL="mysql://username_ibw_user:YOUR_PASSWORD@127.0.0.1:3306/username_ibw_db"`
* `JWT_SECRET="generate-a-long-random-string"`
* `JWT_REFRESH_SECRET="generate-another-long-random-string"`
* `REDIS_HOST="127.0.0.1"` (if Redis is supported on your cPanel, else configure external Redis or local instance)
* `REDIS_PORT=6379`
* `REDIS_PASSWORD=""`
* `MEILI_HOST=""` (if Meilisearch is used)
* `MEILI_MASTER_KEY=""`

### Frontend Configurations
Rename `apps/frontend/.env.local.example` to `.env.local`:
* `NEXT_PUBLIC_API_URL="https://yourdomain.com/api/v1"` (Replace with your backend URL/domain)

---

## Step 4: Configure Node.js Applications in cPanel

Since this is a monorepo, you will run the backend and frontend as two separate applications.

### A. Setup the Backend API Application
1. Navigate to cPanel -> **Setup Node.js App**.
2. Click **Create Application**.
3. Configure the following:
   * **Node.js version**: Select `20.x` or higher.
   * **Application Mode**: `production`.
   * **Application root**: `ibw-app` (where the root files are).
   * **Application URL**: `yourdomain.com/api` (or a subdomain like `api.yourdomain.com`).
   * **Application startup file**: `apps/backend/dist/server.js`.
4. Add environment variables under **Environment Variables** (matching Step 3).
5. Click **Create**.
6. Once created, click the **Run NPM Install** button in the cPanel interface. (This will install only the necessary production dependencies for the entire monorepo automatically!).

### B. Run DB Migrations (Prisma)
1. In the **Setup Node.js App** edit page for the **Backend** app:
2. Temporarily change the **Application startup file** to: `apps/backend/migrate-cpanel.js`.
3. Click **Save** and **Restart**.
4. Open the Backend URL in your web browser (e.g. `https://yourdomain.com/api` or `https://api.yourdomain.com`).
5. The page will execute the migrations and print the log showing `Prisma Migration Completed Successfully!`.
6. Once successful, change the **Application startup file** back to `apps/backend/dist/server.js` and click **Restart**.

---

### C. Setup the Frontend Web Application
1. Go back to **Setup Node.js App**.
2. Click **Create Application**.
3. Configure the following:
   * **Node.js version**: `20.x` or higher.
   * **Application Mode**: `production`.
   * **Application root**: `ibw-app`.
   * **Application URL**: `yourdomain.com` (your main domain).
   * **Application startup file**: `apps/frontend/server.js`.
4. Click **Create**.
5. Click **Run NPM Install** if dependencies are not yet installed (only need to run it once).
6. Click **Restart**.

---

Your application is now fully deployed and configured to serve the Next.js storefront on your main domain and routing the backend APIs correctly!
