# Commersio Platform - Production Deployment Guide

## Quick Deployment

### Option 1: Automated Deployment Script
```bash
chmod +x deploy.sh
./deploy.sh yourdomain.com
```

### Option 2: Manual Configuration

1. **Update Environment Variables**
   ```bash
   cp .env.example .env
   # Edit .env with your domain and credentials
   ```

2. **Configure Frontend**
   ```bash
   cd user-panel
   # Update .env.production with your domain
   REACT_APP_API_URL=https://yourdomain.com/api
   npm run build
   cd ..
   ```

3. **Install Dependencies**
   ```bash
   npm install --production
   ```

## Server Configuration

### Environment Variables
- `DOMAIN`: Your domain name (e.g., yourdomain.com)
- `FRONTEND_URL`: Full frontend URL (e.g., https://yourdomain.com)
- `BASE_URL`: Base URL for webhooks (e.g., https://yourdomain.com)
- `PORT`: Server port (default: 3002)
- `NODE_ENV`: Set to 'production'

### SSL & Reverse Proxy
1. **Install Nginx**
   ```bash
   sudo apt update
   sudo apt install nginx
   ```

2. **Configure SSL (Let's Encrypt)**
   ```bash
   sudo apt install certbot python3-certbot-nginx
   sudo certbot --nginx -d yourdomain.com
   ```

3. **Setup Nginx**
   ```bash
   sudo cp nginx.conf.example /etc/nginx/sites-available/commersio
   sudo ln -s /etc/nginx/sites-available/commersio /etc/nginx/sites-enabled/
   sudo nginx -t
   sudo systemctl reload nginx
   ```

## Process Management

### Using PM2 (Recommended)
```bash
npm install -g pm2
pm2 start index.js --name "commersio"
pm2 startup
pm2 save
```

### Using systemd
```bash
sudo cp commersio.service /etc/systemd/system/
sudo systemctl enable commersio
sudo systemctl start commersio
```

## Domain Configuration Checklist

- [ ] DNS A record points to your server IP
- [ ] SSL certificate installed and configured
- [ ] Environment variables updated with your domain
- [ ] Frontend built with correct API URL
- [ ] Nginx/reverse proxy configured
- [ ] Firewall configured (ports 80, 443, 3002)
- [ ] Process manager (PM2/systemd) configured

## Testing Deployment

1. **Check server status**
   ```bash
   curl https://yourdomain.com/api/test
   ```

2. **Test WebSocket connection**
   ```bash
   curl -I https://yourdomain.com/socket.io/
   ```

3. **Verify frontend**
   ```bash
   curl https://yourdomain.com
   ```

## Troubleshooting

### Common Issues
- **CORS errors**: Check FRONTEND_URL in .env
- **Socket.IO not working**: Verify Nginx WebSocket configuration
- **API not accessible**: Check reverse proxy configuration
- **SSL issues**: Verify certificate paths in Nginx config

### Logs
```bash
# Application logs
pm2 logs commersio

# Nginx logs
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/nginx/access.log
```

## Security Considerations

- Use strong JWT_SECRET and SESSION_SECRET
- Enable HTTPS only in production
- Configure proper CORS origins
- Set up firewall rules
- Regular security updates
- Monitor logs for suspicious activity

## Scaling

For high-traffic deployments:
- Use load balancer (Nginx/HAProxy)
- Redis for session storage
- MongoDB replica set
- CDN for static assets
- Horizontal scaling with multiple instances