# 🚀 Lesa AI — cPanel Shared Hosting Setup Guide

Most modern cPanel hosts (Namecheap, Hostinger, A2 Hosting, etc.) support Node.js.
This guide walks you through deploying Lesa AI on shared hosting in under 10 minutes.

---

## ✅ Prerequisites

- A cPanel account that includes **"Setup Node.js App"** in the Software section
- SSH access (or the cPanel File Manager + Terminal)
- Your free Groq API key (https://console.groq.com)

---

## STEP 1 — Upload the project files

### Option A: File Manager (no SSH)
1. Log into cPanel → **File Manager**
2. Navigate to your domain root (usually `public_html` or a subfolder)
3. Create a new folder: `lesa-ai`
4. Upload `lesa-ai.zip` into that folder
5. Right-click the zip → **Extract** → extract into `lesa-ai/`

### Option B: SSH (faster)
```bash
# Upload via scp from your computer
scp lesa-ai.zip youruser@yourhost.com:~/

# SSH into the server
ssh youruser@yourhost.com

# Unzip
cd ~
unzip lesa-ai.zip
```

---

## STEP 2 — Create the Node.js App in cPanel

1. Go to cPanel → **Software** → **Setup Node.js App**
2. Click **+ Create Application**
3. Fill in:

| Field | Value |
|-------|-------|
| Node.js version | 18.x or 20.x (pick the latest available) |
| Application mode | Production |
| Application root | `/home/yourusername/lesa-ai` |
| Application URL | Your domain or subdomain (e.g. `lesa.yourdomain.com`) |
| Application startup file | `src/server.js` |

4. Click **Create**

---

## STEP 3 — Set Environment Variables

Still on the Node.js App page, scroll to **Environment Variables** and add:

| Key | Value |
|-----|-------|
| `GROQ_API_KEY` | your_groq_api_key_here |
| `GROQ_MODEL` | llama-3.3-70b-versatile |
| `PORT` | (leave blank — cPanel sets this automatically) |
| `MAX_SHORT_TERM_MESSAGES` | 20 |
| `DB_PATH` | /home/yourusername/lesa-ai/data/lesa.db |

> ⚠️ Replace `yourusername` with your actual cPanel username.

Click **Save**.

---

## STEP 4 — Install Dependencies

### Option A: Via cPanel Terminal
1. cPanel → **Advanced** → **Terminal**
2. Run:
```bash
cd ~/lesa-ai
npm install --production
mkdir -p data
```

### Option B: Via SSH
```bash
cd ~/lesa-ai
npm install --production
mkdir -p data
```

---

## STEP 5 — Start the App

1. Go back to **Setup Node.js App**
2. Find your Lesa AI app in the list
3. Click **▶ Start** (or **Restart** if it's already running)
4. The status should show **Running**

---

## STEP 6 — Test It

Open your browser and go to your domain:

```
https://yourdomain.com
```

You should see the **Lesa AI chat interface**.

Test the API directly:
```bash
curl -X POST https://yourdomain.com/api/chat \
  -H "Content-Type: application/json" \
  -d '{"session_id":"test-1","message":"Hello Lesa!"}'
```

---

## 🔄 Restarting After Changes

If you update any files:
1. Re-upload the changed files via File Manager or SSH
2. Go to **Setup Node.js App** → click **Restart**

Or via SSH:
```bash
cd ~/lesa-ai
# cPanel manages the process — use their restart button
# OR if you have pm2 access:
pm2 restart lesa-ai
```

---

## 📁 Where Things Live on the Server

```
/home/yourusername/lesa-ai/
├── src/              ← All application code
├── public/           ← Frontend (index.html)
│   └── index.html
├── data/
│   └── lesa.db       ← SQLite database (auto-created, BACK THIS UP)
├── node_modules/     ← Installed by npm install
├── package.json
└── .env.example
```

> ⚠️ Do NOT put lesa-ai inside `public_html` — put it in your home directory.
> cPanel's Node.js app manager handles routing to your domain.

---

## 🛡️ Security Notes

- Never put your `.env` file inside `public_html`
- The `data/lesa.db` file contains all memories — back it up regularly
- Use HTTPS (most cPanel hosts provide free SSL via Let's Encrypt — enable it in cPanel → SSL/TLS → Let's Encrypt)

---

## ❓ Troubleshooting

| Problem | Fix |
|---------|-----|
| App won't start | Check Node.js version is 18+ in cPanel |
| `GROQ_API_KEY` error | Make sure env variable is saved in cPanel (no quotes around the value) |
| Database error | Run `mkdir -p ~/lesa-ai/data` and check DB_PATH env variable points to correct path |
| 502 Bad Gateway | App crashed — check cPanel error logs, likely a missing env variable |
| Can't install npm packages | Use cPanel Terminal or SSH, not File Manager |

### View error logs:
cPanel → **Logs** → **Errors** — or in SSH:
```bash
cat ~/.cpanel/logs/node_app_error.log
```

---

## 🌐 Hosting Providers That Support This (tested)

- ✅ Namecheap Shared Hosting (EasyWP excluded)
- ✅ A2 Hosting
- ✅ Hostinger Business plan and above
- ✅ SiteGround GrowBig and above
- ✅ InMotion Hosting
- ❌ Basic WordPress-only hosting (no Node.js support)

---

## 💾 Backup Your Data

The SQLite database at `~/lesa-ai/data/lesa.db` holds all of Lesa's long-term memories.
Back it up regularly:

```bash
# Via SSH — copy to your home dir backup folder
cp ~/lesa-ai/data/lesa.db ~/backups/lesa-$(date +%Y%m%d).db
```

Or download it via cPanel File Manager periodically.
