# Python 108 — Deployment Guide

Three ways to run Python 108 beyond your local machine.

---

## Option 1 — Docker (recommended, any platform)

Run the entire app with one command. No Python installation needed.

### Prerequisites
- [Docker Desktop](https://www.docker.com/products/docker-desktop/) installed

### Steps

```bash
# 1. Extract the zip and enter the folder
cd python108_v9

# 2. Create the admin account (only needed once)
python setup_admin.py

# 3. Build and start the container
docker-compose up -d

# 4. Open in browser
# http://localhost:5000
```

### Useful Docker commands

```bash
# View logs
docker-compose logs -f

# Stop the app
docker-compose down

# Rebuild after code changes
docker-compose up -d --build

# Access the database inside container
docker exec -it python108_app python setup_admin.py
```

The database is stored in a Docker volume (`python108_data`) —
it persists even when you stop or rebuild the container.

---

## Option 2 — Railway (free cloud hosting)

Railway gives you a public URL your students can access from anywhere.

### Steps

1. Create a free account at [railway.app](https://railway.app)

2. Install the Railway CLI:
```bash
npm install -g @railway/cli
railway login
```

3. From your project folder:
```bash
cd python108_v9
railway init          # creates a new project
railway up            # deploys the app
```

4. Set environment variables in the Railway dashboard:
```
SECRET_KEY  = (generate a long random string)
DB_PATH     = /app/data/python108.db
```

5. Add a persistent volume for the database in Railway dashboard:
   - Volume mount path: `/app/data`

6. Create admin account by running a one-off command:
```bash
railway run python setup_admin.py
```

7. Your app is live at the Railway-generated URL.

---

## Option 3 — Linux VPS (full control)

For a school server or any Ubuntu/Debian VPS.

### Prerequisites
- A server running Ubuntu 22.04+
- SSH access
- Domain name (optional but recommended)

### Steps

```bash
# 1. SSH into your server
ssh user@your-server-ip

# 2. Install Python and dependencies
sudo apt update
sudo apt install -y python3 python3-pip python3-venv nginx

# 3. Upload your project (from your local machine)
scp -r python108_v9/ user@your-server-ip:/home/user/python108

# 4. On the server — set up virtual environment
cd /home/user/python108
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install gunicorn

# 5. Create admin account
python setup_admin.py

# 6. Create a systemd service so it runs on boot
sudo nano /etc/systemd/system/python108.service
```

Paste this into the file:
```ini
[Unit]
Description=Python 108 Learning App
After=network.target

[Service]
User=user
WorkingDirectory=/home/user/python108
Environment="SECRET_KEY=your-long-random-secret-key-here"
Environment="DB_PATH=/home/user/python108/data/python108.db"
ExecStart=/home/user/python108/venv/bin/gunicorn \
          --workers 2 \
          --bind 127.0.0.1:5000 \
          app:app
Restart=always

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

```bash
# 7. Start and enable the service
sudo mkdir -p /home/user/python108/data
sudo systemctl daemon-reload
sudo systemctl start python108
sudo systemctl enable python108

# 8. Configure nginx as a reverse proxy
sudo nano /etc/nginx/sites-available/python108
```

Paste this nginx config:
```nginx
server {
    listen 80;
    server_name your-domain.com;   # or your server IP

    location / {
        proxy_pass         http://127.0.0.1:5000;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
```

```bash
# 9. Enable the site
sudo ln -s /etc/nginx/sites-available/python108 /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

# 10. (Optional) Add HTTPS with Let's Encrypt
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com
```

Your app is now live at `http://your-domain.com` with HTTPS.

---

## Environment Variables Reference

| Variable    | Default                  | Description                        |
|-------------|--------------------------|------------------------------------|
| SECRET_KEY  | dev key (insecure)       | Flask session secret — change this |
| DB_PATH     | ./python108.db           | Path to SQLite database file       |
| PORT        | 5000                     | Port to listen on                  |

---

## Security checklist before going live

- [ ] Change `SECRET_KEY` to a long random string (32+ characters)
- [ ] Set a strong admin password via `setup_admin.py`
- [ ] Store the database file outside the web root
- [ ] Enable HTTPS (Let's Encrypt is free)
- [ ] Set up regular database backups:
  ```bash
  # Backup cron job (runs daily at 2am)
  0 2 * * * cp /home/user/python108/data/python108.db \
              /home/user/backups/python108_$(date +%Y%m%d).db
  ```

---

## Troubleshooting

**App won't start**
```bash
# Check logs
journalctl -u python108 -n 50
# or
docker-compose logs python108
```

**Database permission error**
```bash
# Make sure the data directory is writable
chmod 755 /home/user/python108/data
```

**Students can't connect**
```bash
# Check firewall — port 80 and 443 must be open
sudo ufw allow 80
sudo ufw allow 443
```
