# cPanel Deployment Guide - Farm Website

## Prerequisites
- cPanel hosting account with Node.js support
- SSH access (optional but recommended)
- FTP/File Manager access

## Step-by-Step Deployment

### 1. Prepare Your Application

**On your local machine:**

```bash
# Remove node_modules and create a clean package
rm -rf node_modules
zip -r farm-website.zip . -x "node_modules/*" -x ".git/*"
```

### 2. Upload to cPanel

**Option A: Using File Manager**
1. Login to cPanel
2. Open "File Manager"
3. Navigate to your desired directory (e.g., `public_html/farm`)
4. Click "Upload" and upload `farm-website.zip`
5. Right-click the zip file and select "Extract"
6. Delete the zip file after extraction

**Option B: Using FTP**
1. Connect via FTP client (FileZilla, etc.)
2. Upload all files to your target directory
3. Ensure all files are transferred

### 3. Setup Node.js Application

1. **Find "Setup Node.js App" in cPanel**
   - Located in Software section
   - If not available, contact your hosting provider

2. **Create New Application**
   - Click "Create Application"
   
   **Configuration:**
   ```
   Node.js version: 14.x or higher (select latest available)
   Application mode: Production
   Application root: /home/yourusername/public_html/farm
   Application URL: yourdomain.com (or subdomain)
   Application startup file: server.js
   ```

3. **Click "Create"**

### 4. Install Dependencies

**EASIEST METHOD - Use cPanel Interface:**

1. In the "Setup Node.js App" page, find your application
2. Look for the "Run NPM Install" button
3. Click it and wait for completion (may take 1-2 minutes)
4. You'll see a success message when done

**ALTERNATIVE METHOD - If button doesn't work:**

1. Go to cPanel "Terminal" (in Advanced section)
2. Copy and paste these commands ONE BY ONE:

```bash
cd public_html/farm
```

Then:

```bash
npm install
```

**If you get permission errors:**

```bash
/home/yourusername/nodevenv/public_html/farm/14/bin/npm install
```

(Replace `yourusername` with your actual cPanel username and `14` with your Node.js version)

**MANUAL METHOD - If Terminal is not available:**

1. On your LOCAL computer, run:
```bash
npm install --production
```

2. Zip the entire folder INCLUDING node_modules:
```bash
zip -r farm-website-with-modules.zip .
```

3. Upload this zip to cPanel and extract it
4. This includes all dependencies pre-installed

### 5. Create Required Directories

**Using cPanel File Manager:**

1. Open "File Manager" in cPanel
2. Navigate to your app directory (e.g., `public_html/farm`)
3. Click "+ Folder" button
4. Create a folder named `uploads`
5. Right-click the `uploads` folder → "Change Permissions"
6. Set to `755` (or check: Owner: Read/Write/Execute, Group: Read/Execute, World: Read/Execute)
7. Click "Change Permissions"

**Using Terminal (if available):**

```bash
cd public_html/farm
mkdir uploads
chmod 755 uploads
```

### 6. Configure Environment (Optional)

In the Node.js App interface, you can add environment variables:

```
NODE_ENV=production
```

Note: PORT is automatically set by cPanel.

### 7. Start the Application

**In cPanel Node.js App interface:**
- Click "Start" or "Restart" button
- Wait for status to show "Running"

**Check Application Status:**
- Green indicator = Running
- Red indicator = Stopped (check logs)

### 8. Verify Deployment

1. **Visit your website:**
   - `http://yourdomain.com` or `http://yourdomain.com/farm`

2. **Test admin panel:**
   - `http://yourdomain.com/admin/login`
   - Login with: admin / admin123

3. **Test functionality:**
   - Browse products
   - Submit a quote request
   - Login to admin panel
   - Add/edit a product

### 9. Security Configuration

**Change Default Admin Password:**

1. **Generate new password hash:**

```bash
# In your app directory
node -e "console.log(require('bcryptjs').hashSync('YourNewPassword123', 10))"
```

2. **Edit data/store.js:**

```javascript
admin: {
  username: 'admin',
  password: '$2a$10$YOUR_NEW_HASH_HERE'
}
```

3. **Restart the application**

### 10. Domain Configuration

**If using subdomain (e.g., farm.yourdomain.com):**

1. Create subdomain in cPanel
2. Point subdomain document root to your app directory
3. Update Application URL in Node.js App settings

**If using main domain:**
- Ensure Application URL matches your domain
- May need to adjust .htaccess if needed

## Troubleshooting

### Application Won't Start

**Check logs:**
```bash
# In cPanel Node.js App interface, click "View Logs"
# Or via SSH:
cd /home/yourusername/public_html/farm
cat logs/error.log
```

**Common issues:**
- Missing dependencies: Run `npm install` again
- Port conflicts: cPanel manages ports automatically
- File permissions: Ensure files are readable

### 500 Internal Server Error

1. Check Node.js App status (should be "Running")
2. Verify `server.js` exists and is correct
3. Check error logs
4. Restart the application

### Images Not Uploading

```bash
# Fix uploads directory permissions
chmod 755 uploads
chown yourusername:yourusername uploads
```

### Application Stops Randomly

1. Check memory usage in cPanel
2. Review error logs for crashes
3. Consider upgrading hosting plan if needed
4. Add error handling and logging

## Maintenance

### Updating the Application

```bash
# Backup current version
cp -r /home/yourusername/public_html/farm /home/yourusername/farm-backup

# Upload new files via FTP or File Manager
# Install any new dependencies
npm install

# Restart application
# Use cPanel Node.js App interface or:
touch tmp/restart.txt
```

### Monitoring

**Check application status regularly:**
- cPanel Node.js App interface
- Monitor error logs
- Test website functionality

### Backup

**Regular backups:**
```bash
# Backup application files
tar -czf farm-backup-$(date +%Y%m%d).tar.gz /home/yourusername/public_html/farm

# Backup uploads
tar -czf uploads-backup-$(date +%Y%m%d).tar.gz /home/yourusername/public_html/farm/uploads
```

## Performance Tips

1. **Enable compression** in Express (add to server.js):
```javascript
const compression = require('compression');
app.use(compression());
```

2. **Use production mode:**
```bash
NODE_ENV=production
```

3. **Optimize images** before uploading

4. **Consider CDN** for static assets

## Database Migration (Future)

When ready to add a database:

1. Create MySQL database in cPanel
2. Install database driver: `npm install mysql2` or `npm install mongoose`
3. Update `data/store.js` to use database
4. Migrate existing data
5. Update connection settings

## Support Resources

- **cPanel Documentation**: Check your hosting provider's docs
- **Node.js App Logs**: Available in cPanel interface
- **Error Logs**: `/home/yourusername/public_html/farm/logs/`
- **Hosting Support**: Contact your hosting provider for Node.js specific issues

## Quick Reference Commands

```bash
# Activate Node.js environment
source /home/yourusername/nodevenv/public_html/farm/14/bin/activate

# Navigate to app
cd /home/yourusername/public_html/farm

# Install dependencies
npm install

# Check Node version
node --version

# Check npm version
npm --version

# View running processes
ps aux | grep node

# Restart app (if using passenger)
touch tmp/restart.txt
```

## Success Checklist

- [ ] Files uploaded to cPanel
- [ ] Node.js App created and configured
- [ ] Dependencies installed (`npm install`)
- [ ] uploads directory created with proper permissions
- [ ] Application started and showing "Running"
- [ ] Website accessible at configured URL
- [ ] Admin panel accessible and functional
- [ ] Quote form working
- [ ] Product images uploading successfully
- [ ] Default admin password changed
- [ ] Backup created

## Next Steps

Once deployed successfully:
1. Add your actual farm products
2. Update content on all pages
3. Add real product images
4. Configure email notifications
5. Set up SSL certificate (if not already)
6. Plan for payment gateway integration
7. Consider database migration for scalability

---

**Need Help?**
- Check cPanel error logs first
- Verify all steps were completed
- Contact your hosting provider's support
- Review Node.js version compatibility
