# 🚀 cPanel Node.js Deployment Guide

This guide will help you deploy the Ilmwave Backend to cPanel Node.js hosting.

## 📦 Files to Upload to cPanel

Create a ZIP file with the following files and folders:

### ✅ Required Files for ZIP:
```
📁 Your-Ilmwave-Backend.zip
├── 📁 dist/                     # Built application files
├── 📁 node_modules/             # Dependencies (or install on server)  
├── 📁 src/                      # Source code (optional for production)
├── 📄 app.js                    # cPanel startup file
├── 📄 package.json              # Dependencies and scripts
├── 📄 package-lock.json         # Exact dependency versions
├── 📄 .env.production           # Production environment config
├── 📄 database-schema.sql       # Database schema file
├── 📄 Ilmwave-API.postman_collection.json  # API testing
├── 📄 README.md                 # Documentation
└── 📄 CPANEL_DEPLOYMENT_GUIDE.md # This guide
```

### ❌ Files NOT to Include in ZIP:
- `.env` (contains development credentials)
- `node_modules/` (can be installed on server to save space)
- `.git/` 
- `test/`
- `coverage/`
- `.DS_Store`

## 🛠️ Pre-Deployment Setup

### 1. Database Setup in cPanel
1. **Create MySQL Database:**
   - Login to cPanel
   - Go to "MySQL Databases"
   - Create new database: `ilmwave_db` (or your preferred name)
   - Create database user with full privileges
   - Note down: hostname, username, password, database name

2. **Import Database Schema:**
   - Go to phpMyAdmin in cPanel
   - Select your database
   - Click "Import" tab
   - Upload `database-schema.sql`
   - Click "Go" to import

### 2. Update Production Environment File
Edit `.env.production` with your actual cPanel details:

```env
# Node.js Environment  
NODE_ENV=production
PORT=3000

# Database Configuration - UPDATE WITH YOUR CPANEL DETAILS
DB_HOST=localhost                    # Usually localhost
DB_PORT=3306                         # Usually 3306
DB_USERNAME=cpanel_username_db_user  # Your cPanel DB user
DB_PASSWORD=your_secure_db_password  # Your cPanel DB password
DB_DATABASE=cpanel_username_ilmwave  # Your cPanel DB name

# JWT Configuration - CHANGE TO SECURE SECRET
JWT_SECRET=your_super_secure_jwt_secret_key_change_this_in_production
JWT_EXPIRES_IN=7d

# CORS Configuration - UPDATE WITH YOUR DOMAIN
CORS_ORIGIN=https://yourdomain.com,https://www.yourdomain.com
```

## 🚢 Deployment Steps

### Step 1: Create ZIP File
Create a ZIP file containing only the required files listed above.

### Step 2: Upload to cPanel
1. Login to cPanel
2. Go to "File Manager" 
3. Navigate to your Node.js app directory (usually `public_html/` or a subdirectory)
4. Upload and extract the ZIP file

### Step 3: Setup Node.js Application in cPanel
1. Go to "Node.js" in cPanel
2. Click "Create Application"
3. Fill in the details:
   - **Node.js Version**: Latest available (14+ recommended)
   - **Application Mode**: Production
   - **Application Root**: Path to your uploaded files
   - **Application URL**: Your desired subdomain/path
   - **Application Startup File**: `app.js`
   - **Environment Variables**: Import from `.env.production`

### Step 4: Install Dependencies
1. In cPanel Node.js interface, click "Run NPM Install"
2. Or use terminal: `npm install --production`

### Step 5: Configure Environment Variables
In cPanel Node.js interface, add these environment variables:
```
NODE_ENV=production
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=your_db_user
DB_PASSWORD=your_db_password
DB_DATABASE=your_database
JWT_SECRET=your_jwt_secret
CORS_ORIGIN=https://yourdomain.com
```

### Step 6: Start Application
1. Click "Start Application" in cPanel Node.js interface
2. Check application logs for any errors

## 🔧 Troubleshooting

### Common Issues:

1. **Application Won't Start**
   - Check Node.js logs in cPanel
   - Verify all environment variables are set
   - Ensure database credentials are correct

2. **Database Connection Failed**
   - Verify database exists and user has permissions
   - Check hostname (usually localhost)
   - Test database connection in phpMyAdmin

3. **Module Not Found Errors**
   - Run `npm install` in the application directory
   - Check that `node_modules` folder exists

4. **CORS Issues**
   - Update `CORS_ORIGIN` environment variable
   - Include both `https://yourdomain.com` and `https://www.yourdomain.com`

### Logs Location:
- cPanel Node.js logs: Available in cPanel interface
- Application logs: Check cPanel File Manager in `logs/` directory

## 🧪 Testing Deployment

### 1. API Health Check
Visit: `https://yourdomain.com/v1/` (should return app info)

### 2. Login Test
Use Postman collection:
1. Import `Ilmwave-API.postman_collection.json`
2. Update `baseUrl` to your domain
3. Test login with: `admin@ilmwave.com` / `admin123456`

### 3. Content API Test
After login, test content endpoints:
- `GET /v1/admin/content/home-page`
- `GET /v1/admin/content/services-page`

## 🔐 Security Notes

1. **Change Default Credentials**
   ```sql
   UPDATE users SET password = '$2a$12$newHashedPassword' WHERE email = 'admin@ilmwave.com';
   ```

2. **Secure JWT Secret**
   - Use a strong, random JWT secret
   - Never use the default/example secret in production

3. **Database Security**
   - Use strong database passwords
   - Limit database user permissions
   - Regular database backups

## 📊 Performance Optimization

1. **Enable Gzip Compression** in cPanel
2. **Use PM2** for process management (if available)
3. **Monitor Memory Usage** in cPanel Node.js interface
4. **Database Indexing** - Already included in schema

## 🆘 Support

If you encounter issues:
1. Check cPanel Node.js application logs
2. Verify database connection in phpMyAdmin  
3. Test API endpoints with Postman
4. Check environment variables are correctly set

---

## 📝 Quick Deployment Checklist

- [ ] Database created in cPanel MySQL
- [ ] Schema imported via phpMyAdmin
- [ ] ZIP file created with required files
- [ ] Files uploaded and extracted in cPanel
- [ ] Node.js application created in cPanel
- [ ] Environment variables configured
- [ ] Dependencies installed (`npm install`)
- [ ] Application started successfully
- [ ] API endpoints tested with Postman
- [ ] Default admin password changed

🎉 **Your Ilmwave Backend is now live on cPanel!**