# Village Connector - VPS Deployment Package

## Server Configuration
- **Server IP**: http://198.38.87.175
- **Frontend Port**: 3010
- **Backend Port**: 3011

---

## Step 1: Upload Files

Upload this entire folder to your VPS:
```bash
scp -r village_connector_upload/* user@198.38.87.175:/var/www/village_connector/
```

---

## Step 2: Server Setup

SSH into your server and run:

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

# Install dependencies
sudo apt install -y curl wget git nginx mysql-server

# Install Node.js 18
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo bash -
sudo apt install -y nodejs

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

---

## Step 3: Database Setup

```bash
sudo mysql
```

In MySQL console:
```sql
CREATE DATABASE village_connector CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
EXIT;
```

---

## Step 4: Backend Setup

```bash
cd /var/www/village_connector/backend

# Edit .env with your MySQL password
nano .env

# Install dependencies
npm install --production

# Start with PM2
pm2 start ../deploy/pm2/ecosystem.config.js
pm2 save
pm2 startup
```

---

## Step 5: Frontend Setup

```bash
cd /var/www/village_connector/frontend

# Install dependencies
npm install

# Build for production (already built if you see .next folder)
npm run build

# Start frontend
PORT=3010 pm2 start npm --name "village-connector-frontend" -- start
pm2 save
```

---

## Step 6: Nginx Configuration

```bash
# Copy nginx config
sudo cp /var/www/village_connector/deploy/nginx/village-connector.conf /etc/nginx/sites-available/

# Enable site
sudo ln -sf /etc/nginx/sites-available/village-connector.conf /etc/nginx/sites-enabled/

# Test and restart nginx
sudo nginx -t
sudo systemctl restart nginx
```

---

## Step 7: Update Backend .env

Edit `/var/www/village_connector/backend/.env`:
```env
PORT=3011
DB_HOST=localhost
DB_PORT=3306
DB_NAME=village_connector
DB_USER=root
DB_PASSWORD=YOUR_MYSQL_PASSWORD
JWT_SECRET=village_connector_secure_key_2024
JWT_EXPIRE=7d
SEAT_LOCK_TIMEOUT=300000
NODE_ENV=production
```

---

## Access URLs

| Service | URL |
|---------|-----|
| **Frontend (via Nginx)** | http://198.38.87.175 |
| **Frontend (Direct)** | http://198.38.87.175:3010 |
| **Backend API** | http://198.38.87.175:3011/api |
| **Health Check** | http://198.38.87.175:3011/api/health |

---

## Useful Commands

```bash
# Backend logs
pm2 logs village-connector-backend

# Frontend logs
pm2 logs village-connector-frontend

# Restart services
pm2 restart all

# Check status
pm2 status

# Stop all
pm2 delete all
```

---

## Default Test Credentials

- **Admin**: phone: `9999999999`, password: `admin123`
- **Driver**: phone: `8888888888`, password: `driver123`
- **Passenger**: phone: `7777777777`, password: `passenger123`

---

## Troubleshooting

### Port Already in Use
```bash
sudo lsof -i :3010
sudo lsof -i :3011
pm2 delete all
```

### Database Connection Failed
```bash
sudo mysql -u root -p
SHOW DATABASES;
```

### Nginx 502 Error
```bash
curl http://localhost:3011/api/health
sudo tail -f /var/log/nginx/error.log
```
