# Complete Deployment Steps for LiveKit VoiceBot

## Current Status

✅ **Completed:**
- Git repository cloned (devmilan branch)
- Frontend production build created
- Backend upgraded to livekit-agents 1.4.6 with Cartesia support
- All dependencies installed
- Services are running in background

## Services Currently Running

1. **Frontend**: Next.js production server (PID: 1029871)
   - Port: 3000
   - Location: `/var/www/html/livekit_frontend/FrontEnd/agent-starter-react/agent-starter-react`

2. **Backend**: Python agent with uv
   - Location: `/var/www/html/livekit_frontend/BackEnd/agent-starter-python`
   - Plugins: Cartesia, ElevenLabs, Silero, Turn Detector

---

## Apache Configuration for Domain 10.40.180.74

### Run these commands to complete the deployment:

```bash
# 1. Copy Apache configuration
sudo cp /tmp/livekit-voicebot.conf /etc/apache2/sites-available/

# 2. Enable the site
sudo a2ensite livekit-voicebot.conf

# 3. Test Apache configuration
sudo apache2ctl configtest

# 4. If test passes, reload Apache
sudo systemctl reload apache2
```

### Alternative: If you want to use a different port (e.g., 8080):

Edit the Apache config to use port 8080 instead:
```bash
sudo nano /etc/apache2/sites-available/livekit-voicebot.conf
# Change <VirtualHost *:80> to <VirtualHost *:8080>
# Then add Listen 8080 to /etc/apache2/ports.conf if not already there
```

---

## Systemd Services (Optional - for auto-restart on reboot)

To install systemd services that will auto-start on server reboot:

```bash
# Run the installation script
bash /var/www/html/livekit_frontend/install-services.sh

# Or manually:
sudo cp /var/www/html/livekit_frontend/livekit-frontend.service /etc/systemd/system/
sudo cp /var/www/html/livekit_frontend/livekit-backend.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable livekit-frontend livekit-backend
sudo systemctl start livekit-frontend livekit-backend
```

---

## Verify Deployment

### 1. Check services are running:
```bash
ps aux | grep -E "next-server|uv run python"
```

### 2. Test frontend locally:
```bash
curl http://localhost:3000
```

### 3. Test through Apache:
```bash
curl http://10.40.180.74
```

### 4. Test from external machine:
Open browser and go to: `http://10.40.180.74`

---

## Port Configuration

| Service | Port | Access |
|---------|------|--------|
| Apache | 80 | Public (0.0.0.0) |
| Frontend | 3000 | Local (proxied by Apache) |
| Backend | Dynamic | Connects to LiveKit Cloud |

---

## Troubleshooting

### If frontend is not accessible on port 3000:

1. Check if port 3000 is being used:
```bash
sudo lsof -i :3000
```

2. Restart frontend:
```bash
cd /var/www/html/livekit_frontend/FrontEnd/agent-starter-react/agent-starter-react
npm run start
```

### If backend has connection issues:

The connection retry warnings are normal. The backend will connect when:
- A user opens the frontend
- A voice session is initiated
- LiveKit Cloud receives the connection request

### Apache proxy not working:

1. Check Apache modules are enabled:
```bash
sudo a2enmod proxy proxy_http proxy_wstunnel rewrite headers
```

2. Check Apache error logs:
```bash
sudo tail -f /var/log/apache2/livekit-voicebot-error.log
```

---

## Files Created

- `/tmp/livekit-voicebot.conf` - Apache virtual host configuration
- `/var/www/html/livekit_frontend/livekit-frontend.service` - Systemd service for frontend
- `/var/www/html/livekit_frontend/livekit-backend.service` - Systemd service for backend
- `/var/www/html/livekit_frontend/ecosystem.config.js` - PM2 configuration (if needed)
- `/var/www/html/livekit_frontend/deploy.sh` - Deployment helper script
- `/var/www/html/livekit_frontend/install-services.sh` - Systemd service installer

---

## Quick Start Commands

**To deploy everything now:**
```bash
# 1. Configure Apache
sudo cp /tmp/livekit-voicebot.conf /etc/apache2/sites-available/
sudo a2ensite livekit-voicebot.conf
sudo apache2ctl configtest && sudo systemctl reload apache2

# 2. Access your application
echo "Frontend accessible at: http://10.40.180.74"
```

**To set up auto-restart on reboot:**
```bash
bash /var/www/html/livekit_frontend/install-services.sh
```

---

## Success Criteria

After running the Apache commands, you should be able to:
1. ✅ Access the application at http://10.40.180.74
2. ✅ See the LiveKit VoiceBot interface
3. ✅ Start a voice session
4. ✅ Backend connects to LiveKit Cloud automatically
5. ✅ Voice AI responds through ElevenLabs or Cartesia

---

## Support

If you encounter issues:
1. Check logs in `/var/www/html/livekit_frontend/logs/`
2. Check Apache logs in `/var/log/apache2/`
3. Verify environment variables in `.env.local` files
4. Ensure LiveKit Cloud credentials are valid
