# Deployment Guide: Local AI Chatbot on Linux Server

This guide outlines the steps to deploy the Node.js AI Chatbot REST API (powered by `node-llama-cpp`) to a Linux production server (e.g., Ubuntu/Debian).

## Prerequisites
Your Linux server must have an internet connection and basic administrative privileges (`sudo`).

## Step 1: Install System Dependencies
Update your server's package index and install Node.js along with the build tools required to compile `node-llama-cpp` bindings.

```bash
# Update system
sudo apt update && sudo apt upgrade -y

# Install build tools required for node-llama-cpp native compilation
sudo apt install build-essential python3 -y

# Install Node.js (Node 20.x is recommended)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
```

*(Optional: If your server has a GPU and you set `GPU_LAYERS` to anything other than `0`, ensure proper Nvidia drivers and CUDA toolkit are installed).*

## Step 2: Transfer Project Files
Copy your project files to the server. You can use `scp`, `rsync`, or Git.

If copying from your local machine via SCP (do not copy the `node_modules` folder):
```bash
scp -r ./myai user@your_server_ip:/home/user/myai
```

## Step 3: Install Node Packages
Navigate to your project directory and install the dependencies. This process may take several minutes as `node-llama-cpp` might compile native code.

```bash
cd /home/user/myai
npm install
```

## Step 4: Environment and Model Configuration
Configure your `.env` variables and ensure the model file is accessible.

1. **Copy the example environment file:**
   ```bash
   cp .env.example .env
   nano .env
   ```
2. Ensure the `MODEL_PATH` variable correctly points to your `.gguf` file location on the server.
3. **Upload the model:** Ensure your `.gguf` file (e.g., `Qwen3.5-9B-UD-Q2_K_XL.gguf`) is placed inside the `models/` directory on the server.

## Step 5: Start the Application using PM2
Use PM2 to ensure your Node.js application stays running in the background and restarts automatically if it crashes or if the server reboots.

```bash
# Install PM2 globally
sudo npm install pm2 -g

# Start the Node.js application
pm2 start server.js --name "myai-chatbot"

# Generate the startup script to run PM2 on server boot
pm2 startup

# Save the current PM2 process list
pm2 save
```

## Step 6: Expose the Server to the Internet

Make sure your express server is configured to listen on `0.0.0.0` (all network interfaces) rather than just `127.0.0.1` or `localhost`. 

### Option A: Open Direct Port (Testing)
If using `ufw`, open the port specified in your `.env` (default 3000):
```bash
sudo ufw allow 3000
```
Access via `http://your_server_ip:3000`

### Option B: Nginx Reverse Proxy (Production Recommended)
Set up Nginx to handle traffic on standard ports (80/443) and forward it to your Node.js application.

1. **Install Nginx:**
   ```bash
   sudo apt install nginx -y
   ```
2. **Create configuration:**
   ```bash
   sudo nano /etc/nginx/sites-available/myai
   ```
3. **Paste the following config** (replace `your_domain_or_IP` with your actual domain or IP):
   ```nginx
   server {
       listen 80;
       server_name your_domain_or_IP;

       location / {
           proxy_pass http://localhost:3000;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection 'upgrade';
           proxy_set_header Host $host;
           proxy_cache_bypass $http_upgrade;
       }
   }
   ```
4. **Enable and restart Nginx:**
   ```bash
   sudo ln -s /etc/nginx/sites-available/myai /etc/nginx/sites-enabled/
   sudo nginx -t
   sudo systemctl restart nginx
   ```
   
Your chatbot API is now deployed and accessible!
