# miniSISKA — Deployment Guide

Complete guide for moving miniSISKA from XAMPP localhost to cloud hosting.

---

## Architecture Overview

miniSISKA is a PHP 8 + MySQL application with no build step.  
Deployment = copy files + configure .env + import DB.

---

## Supported Hosting Environments

| Platform | Compatibility | Notes |
|----------|--------------|-------|
| XAMPP (local) | ✅ Full | Primary dev environment |
| VPS (Ubuntu/Debian) | ✅ Full | Recommended for production |
| Shared hosting (cPanel) | ✅ Full | Requires SSH for cron |
| Hostinger | ✅ Full | PHP 8.1+, MySQL 8 |
| Render | ⚠️ Partial | No cron, no persistent uploads |
| Railway | ⚠️ Partial | No persistent disk |
| Cloudflare Workers | ❌ No | PHP not supported |

**Recommended production stack**: Ubuntu 22.04 VPS + Apache/Nginx + MySQL 8 + PHP 8.2

---

## XAMPP → VPS Migration

### Step 1: Export database
```bash
# On XAMPP (local)
C:\xampp\mysql\bin\mysqldump.exe -u root mini_siska > mini_siska_export.sql

# Or use the Dashboard > Developer Tools > Backup Database button
```

### Step 2: Prepare VPS
```bash
# Ubuntu 22.04
sudo apt update
sudo apt install -y apache2 mysql-server php8.2 php8.2-mysql php8.2-curl \
     php8.2-mbstring php8.2-gd php8.2-fileinfo php8.2-openssl ffmpeg

sudo a2enmod rewrite
sudo systemctl restart apache2
```

### Step 3: Upload files
```bash
# Via SCP
scp -r C:\xampp\htdocs\mini-siska user@your-server:/var/www/html/mini-siska

# Or via Git (recommended)
git clone https://github.com/your-username/mini-siska.git /var/www/html/mini-siska
```

### Step 4: Create .env on server
```bash
cd /var/www/html/mini-siska
cp .env.example .env
nano .env
# Fill in: DB credentials, OpenAI key, social tokens, etc.
```

### Step 5: Import database
```bash
mysql -u root -p -e "CREATE DATABASE mini_siska CHARACTER SET utf8mb4;"
mysql -u root -p mini_siska < mini_siska_export.sql
```

### Step 6: Set permissions
```bash
sudo chown -R www-data:www-data /var/www/html/mini-siska
sudo chmod -R 755 /var/www/html/mini-siska
sudo chmod -R 775 /var/www/html/mini-siska/storage/uploads
sudo chmod -R 775 /var/www/html/mini-siska/logs
sudo chmod -R 775 /var/www/html/mini-siska/backups
sudo chmod 600 /var/www/html/mini-siska/.env
```

### Step 7: Apache virtual host
```apache
<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/html/mini-siska/public

    <Directory /var/www/html/mini-siska/public>
        Options -Indexes
        AllowOverride All
        Require all granted
    </Directory>

    # Block sensitive directories
    <Directory /var/www/html/mini-siska/config>
        Require all denied
    </Directory>
    <Directory /var/www/html/mini-siska/logs>
        Require all denied
    </Directory>
    <Directory /var/www/html/mini-siska/.env>
        Require all denied
    </Directory>
</VirtualHost>
```

### Step 8: HTTPS (Let's Encrypt)
```bash
sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d yourdomain.com
```

### Step 9: Update .env for production
```env
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com
```

### Step 10: Set up cron jobs
```bash
# Edit cron table
crontab -e

# Add these lines:
*/5 * * * * php /var/www/html/mini-siska/cron/post_scheduler.php >> /var/www/html/mini-siska/logs/scheduler.log 2>&1
*/15 * * * * php /var/www/html/mini-siska/cron/retry_failed.php >> /var/www/html/mini-siska/logs/retry.log 2>&1
0 3 * * 0 php /var/www/html/mini-siska/cron/refresh_tokens.php >> /var/www/html/mini-siska/logs/refresh_tokens.log 2>&1
```

---

## Shared Hosting (cPanel / Hostinger)

### File structure
Upload everything **except**:
- `.env` (create manually on server)
- `storage/uploads/*` (not needed on new install)
- `backups/database/*.sql` (local only)
- `logs/*` (auto-generated)

### cPanel cron
In cPanel → Cron Jobs:
```
*/5 * * * *   php /home/username/public_html/mini-siska/cron/post_scheduler.php
0 3 * * 0     php /home/username/public_html/mini-siska/cron/refresh_tokens.php
```

### PHP version
Ensure PHP 8.1+ in cPanel PHP Version Selector.

Required extensions: `curl`, `json`, `mbstring`, `mysqli`, `gd`, `fileinfo`, `openssl`

---

## Environment Variables Reference

| Variable | Required | Description |
|----------|----------|-------------|
| `OPENAI_API_KEY` | ✅ | GPT-4o API key |
| `OPENAI_MODEL` | ✅ | Usually `gpt-4o` |
| `DB_HOST` | ✅ | MySQL host |
| `DB_NAME` | ✅ | Database name |
| `DB_USER` | ✅ | MySQL user |
| `DB_PASS` | ✅ | MySQL password |
| `APP_ENV` | ✅ | `local` or `production` |
| `APP_URL` | ✅ | Full URL to app root |
| `CRON_SECRET` | ✅ | Random secret for cron HTTP auth |
| `TOKEN_ENCRYPTION_KEY` | Rec. | AES-256 key for token encryption |
| `WHATSAPP_NUMBER` | No | For WA closing in AI output |
| `MAX_UPLOAD_MB` | No | Upload size limit (default: 50) |
| `META_PAGE_ACCESS_TOKEN` | For FB | Facebook Page token |
| `INSTAGRAM_ACCOUNT_ID` | For IG | Instagram Business ID |
| `THREADS_ACCESS_TOKEN` | For Threads | Threads long-lived token |
| `TELEGRAM_BOT_TOKEN` | For TG | Telegram bot token |

---

## Production Checklist

Before going live:

- [ ] `.env` has `APP_ENV=production` and `APP_DEBUG=false`
- [ ] `.env` is NOT accessible via browser (test: `curl https://yourdomain.com/.env`)
- [ ] `TOKEN_ENCRYPTION_KEY` is set (run token migration)
- [ ] `CRON_SECRET` is a strong random string
- [ ] HTTPS is enabled (Certbot/SSL certificate)
- [ ] `storage/uploads/` is writable but not web-browsable (test directory listing)
- [ ] All cron jobs are registered and running
- [ ] Database has been imported and tested
- [ ] Social account tokens have been tested via Accounts page
- [ ] Error logs are being written to `logs/`
- [ ] Backup system is working (Dashboard > Backup Database)
- [ ] GitHub repo is private

---

## ngrok (External Access for Testing)

For testing from mobile or external network while on XAMPP localhost:

```bash
# Install ngrok
winget install ngrok

# Authenticate (free account at ngrok.com)
ngrok authtoken YOUR_TOKEN

# Start tunnel for XAMPP (port 80)
ngrok http 80
```

Copy the `https://xxxxx.ngrok-free.app` URL and open on your phone.

**Important**: Update `PUBLIC_BASE_URL` in social account settings to the ngrok URL when testing image/video posting. The URL changes every time ngrok restarts (paid plan fixes this with reserved domains).

---

## Domain-Ready URL Routing

When moving from `localhost/mini-siska/` to a domain root:

1. Update `APP_URL` in `.env`
2. Update `.htaccess` if using subdirectory
3. Update `start_url` in `public/manifest.json` if changing structure
4. Social media webhooks (if any) need new domain registered in developer portals

---

## Cloud Storage for Uploads (Future)

For production, consider moving uploads to S3/R2/Cloudflare instead of local disk:
- Survives server reboots
- Shareable URLs for Meta Graph API image uploads
- No disk space concerns

Libraries: Flysystem + AWS SDK (when adding Composer later)

---

## Security Hardening for Production

Additional steps for public-facing deployment:

```bash
# Disable PHP error display
php.ini: display_errors = Off

# Enable PHP error logging
php.ini: log_errors = On
php.ini: error_log = /var/www/html/mini-siska/logs/php_errors.log

# Rate limit login/API (nginx example)
limit_req_zone $binary_remote_addr zone=api:10m rate=30r/m;
limit_req zone=api burst=10 nodelay;

# Fail2ban for repeated bad requests
# (standard setup — see fail2ban docs)
```
