# 🔧 cPanel Deployment Fix Guide

## ❌ **Problem Solved: `nest: command not found`**

The error occurs because cPanel servers don't have the NestJS CLI installed globally. Here are **3 solutions**:

---

## 🎯 **Solution 1: Upload Pre-Built Files (RECOMMENDED)**

**✅ Best for cPanel deployment**

### Steps:
1. **Build locally before uploading:**
   ```bash
   npm run build
   ```

2. **Create a new deployment ZIP with `dist/` folder:**
   ```bash
   ./create-deployment-zip.sh
   ```

3. **Upload the ZIP with compiled files**
   - The `dist/` folder contains all compiled JavaScript
   - No building required on server

4. **In cPanel Node.js setup:**
   ```
   Application Startup File: app.js
   ```

5. **Install only production dependencies:**
   ```bash
   npm install --production
   ```

---

## 🎯 **Solution 2: Manual Build on cPanel**

**✅ If you need to build on server**

### Steps:
1. **Upload your files without `node_modules/`**

2. **In cPanel Terminal or File Manager, run:**
   ```bash
   # Install ALL dependencies (including dev dependencies)
   npm install
   
   # Build the project  
   npm run build
   
   # Clean up dev dependencies (optional)
   npm prune --production
   ```

3. **Start the application**

---

## 🎯 **Solution 3: Alternative Build Method**

**✅ Using TypeScript compiler directly**

### Steps:
1. **Install dependencies:**
   ```bash
   npm install
   ```

2. **Build using TypeScript directly:**
   ```bash
   npx tsc -p tsconfig.build.json
   ```

3. **Start application:**
   ```bash
   npm start
   ```

---

## 📦 **Updated Package.json Scripts**

Your `package.json` now has these cPanel-friendly scripts:

```json
{
  "scripts": {
    "build": "tsc -p tsconfig.build.json",    // Uses TypeScript directly
    "start": "node app.js",                   // cPanel startup
    "cpanel:install": "npm install --production && npm run build"
  }
}
```

---

## 🚀 **Recommended cPanel Deployment Process**

### **Step-by-Step:**

1. **Local Preparation:**
   ```bash
   cd Ilmwave-Backend
   npm run build                 # Build locally
   ./create-deployment-zip.sh    # Create ZIP with dist/ folder
   ```

2. **cPanel Upload:**
   - Upload `ilmwave-backend-cpanel.zip`
   - Extract in your Node.js app directory

3. **cPanel Node.js Setup:**
   ```
   Node.js Version: Latest available
   Application Mode: Production
   Application Root: /path/to/your/app/
   Application Startup File: app.js
   ```

4. **Install Dependencies:**
   ```bash
   npm install --production
   ```
   
   **⚠️ Don't run `npm install` without `--production` flag to avoid dev dependencies**

5. **Start Application:**
   - Click "Start Application" in cPanel Node.js interface

---

## 🔍 **Troubleshooting**

### **If build still fails:**

1. **Check Node.js version in cPanel:**
   - Ensure you're using Node.js 14+ 
   - TypeScript requires modern Node.js

2. **Manual file upload:**
   - Build locally: `npm run build`
   - Upload only `dist/` folder manually
   - Skip the build step entirely

3. **Check cPanel logs:**
   - Look for specific error messages
   - Common issues: file permissions, missing files

### **Common cPanel Issues:**

**Issue:** `Cannot find module '@nestjs/core'`
**Fix:** Run `npm install --production` in the app directory

**Issue:** `Port already in use`  
**Fix:** cPanel handles port assignment automatically

**Issue:** `Database connection failed`
**Fix:** Verify database credentials in `.env` file

---

## ✅ **Verification Steps**

After deployment:

1. **Check Application Status:**
   - cPanel Node.js interface shows "Running"

2. **Test API Endpoint:**
   ```bash
   curl https://yourdomain.com/v1/
   ```

3. **Check Logs:**
   - Look for "🚀 Ilmwave Backend is running on port XXXX"

4. **Test Login:**
   ```bash
   curl -X POST https://yourdomain.com/v1/auth/login \
        -H "Content-Type: application/json" \
        -d '{"email":"admin@ilmwave.com","password":"admin123456"}'
   ```

---

## 🎉 **Success Indicators**

✅ **Application shows "Running" in cPanel**
✅ **API responds at `https://yourdomain.com/v1/`**  
✅ **Login returns JWT token**
✅ **Content endpoints return data**

---

## 📞 **Still Having Issues?**

If problems persist:

1. **Share the exact error message** from cPanel logs
2. **Verify file structure** matches the deployment guide
3. **Check database connection** via phpMyAdmin
4. **Confirm environment variables** are set correctly

**The recommended approach is Solution 1 - uploading pre-built files!** 🎯