# AegisTrade AI — Production Deployment Guide

## 1. Deployment Topology

AegisTrade AI is packaged for robust deployment on standard Linux VPS instances (e.g. Ubuntu 22.04 LTS on AWS, DigitalOcean, Hetzner, Linode) or containerized environments via Docker.

```
+--------------------------------------------------------------+
|                         Linux VPS                            |
|                                                              |
|   +-----------------------+     +------------------------+   |
|   | Reverse Proxy (Nginx) | --> | FastAPI Server (:8000) |   |
|   | SSL / TLS Termination |     | Uvicorn Workers        |   |
|   +-----------------------+     +------------------------+   |
|                                             |                |
|                                 +------------------------+   |
|                                 | SQLite / Postgres DB   |   |
|                                 +------------------------+   |
+--------------------------------------------------------------+
```

---

## 2. Docker & Docker Compose Deployment

### 2.1 Using Docker Compose
Build and run the containerized service:
```bash
docker compose up -d --build
```
Check container logs:
```bash
docker compose logs -f aegistrade-api
```
The application will be accessible at port `8000`.

---

## 3. Systemd Service Deployment on VPS

To run as a resilient system service that restarts automatically:

Create `/etc/systemd/system/aegistrade.service`:
```ini
[Unit]
Description=AegisTrade AI Trading Platform
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/opt/aegistrade-ai
ExecStart=/opt/aegistrade-ai/.venv/bin/uvicorn backend.app.main:app --host 127.0.0.1 --port 8000 --workers 1
Restart=always
RestartSec=5
EnvironmentFile=/opt/aegistrade-ai/.env

[Install]
WantedBy=multi-user.target
```

Enable and start:
```bash
sudo systemctl daemon-reload
sudo systemctl enable aegistrade
sudo systemctl start aegistrade
sudo systemctl status aegistrade
```

---

## 4. Nginx Reverse Proxy Configuration

```nginx
server {
    listen 80;
    server_name your-trading-domain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name your-trading-domain.com;

    ssl_certificate /etc/letsencrypt/live/your-trading-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-trading-domain.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # WebSocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
```
