# 🔧 Fix 500 Internal Server Error

## Run These Commands in Your Terminal

### Step 1: Check Nginx Error Logs
```bash
sudo tail -50 /var/log/nginx/error.log
```

This will show you the EXACT error. Look for lines mentioning:
- Permission denied
- No such file or directory
- Connection refused

---

### Step 2: Check File Permissions
```bash
ls -la /home/plagis/workspace/plagis_aumentum/web_frontend/
```

The files should be readable by nginx (www-data user).

**If you see permission issues, fix with:**
```bash
sudo chmod -R 755 /home/plagis/workspace/plagis_aumentum/web_frontend/
sudo chown -R $USER:$USER /home/plagis/workspace/plagis_aumentum/web_frontend/
```

---

### Step 3: Verify API is Running
```bash
ps aux | grep aumentum_api
```

**If you DON'T see `python3 aumentum_api.py`, start it:**
```bash
cd /home/plagis/workspace/plagis_aumentum
source venv/bin/activate
nohup python3 aumentum_api.py > api.log 2>&1 &
```

**Then verify it's accessible:**
```bash
curl http://127.0.0.1:8001/
```

Should return: `{"service":"Aumentum Browser API"...}`

---

### Step 4: Test Nginx Configuration
```bash
sudo nginx -t
```

Should show: `syntax is ok` and `test is successful`

---

### Step 5: Check Nginx is Serving Files
```bash
curl http://127.0.0.1:7000/
```

**Expected:** HTML content (<!DOCTYPE html>...)  
**If error:** Check error log from Step 1

---

### Step 6: Test API Proxy
```bash
curl http://127.0.0.1:7000/api/
```

**Expected:** `{"service":"Aumentum Browser API"...}`  
**If error:** API might not be running or proxy config wrong

---

## 🎯 Common Issues & Fixes

### Issue 1: Permission Denied
```
Error log shows: "Permission denied"
```

**Fix:**
```bash
sudo chmod -R 755 /home/plagis/workspace/plagis_aumentum/web_frontend/
```

---

### Issue 2: File Not Found
```
Error log shows: "No such file or directory"
```

**Fix:** Check nginx config has correct path
```bash
cat /etc/nginx/sites-available/aumentum | grep root
```

Should show:
```
root /home/plagis/workspace/plagis_aumentum/web_frontend;
```

**If wrong path, edit:**
```bash
sudo nano /etc/nginx/sites-available/aumentum
# Fix the root path
sudo systemctl restart nginx
```

---

### Issue 3: API Not Running
```
curl http://127.0.0.1:8001/ fails
```

**Fix:**
```bash
cd /home/plagis/workspace/plagis_aumentum
source venv/bin/activate
python3 aumentum_api.py
```

Keep this terminal open, or run in background:
```bash
nohup python3 aumentum_api.py > api.log 2>&1 &
```

---

### Issue 4: SELinux or AppArmor Blocking
```
Error log shows: "Permission denied" even with correct permissions
```

**Fix:**
```bash
# Check if SELinux is enforcing
getenforce

# If "Enforcing", temporarily set to permissive:
sudo setenforce 0

# Or for AppArmor:
sudo aa-complain /usr/sbin/nginx
```

---

## 📊 Complete Diagnostic Commands

Run all of these and share the output:

```bash
echo "=== 1. Nginx Error Log ==="
sudo tail -20 /var/log/nginx/error.log

echo ""
echo "=== 2. Nginx Config ==="
cat /etc/nginx/sites-available/aumentum

echo ""
echo "=== 3. File Permissions ==="
ls -la /home/plagis/workspace/plagis_aumentum/web_frontend/ | head -10

echo ""
echo "=== 4. API Status ==="
ps aux | grep aumentum_api | grep -v grep
curl -s http://127.0.0.1:8001/ | head -3

echo ""
echo "=== 5. Nginx Status ==="
sudo systemctl status nginx --no-pager | head -15

echo ""
echo "=== 6. Port Listening ==="
sudo netstat -tlnp | grep 7000

echo ""
echo "=== 7. Firewall Status ==="
sudo ufw status | grep 7000

echo ""
echo "=== 8. Test Direct Access ==="
curl -I http://127.0.0.1:7000/

echo ""
echo "=== 9. Test API Proxy ==="
curl -I http://127.0.0.1:7000/api/
```

---

## 🎯 Most Likely Issues

Based on 500 error, it's usually one of these:

### 1. **API Not Running** (Most Common)
```bash
cd /home/plagis/workspace/plagis_aumentum
source venv/bin/activate
nohup python3 aumentum_api.py > api.log 2>&1 &
```

### 2. **File Permissions**
```bash
sudo chmod -R 755 /home/plagis/workspace/plagis_aumentum/web_frontend/
```

### 3. **Incorrect Root Path in Nginx**
Check nginx config has:
```
root /home/plagis/workspace/plagis_aumentum/web_frontend;
```

---

## ✅ Quick Fix Script

Run this to fix most common issues:

```bash
#!/bin/bash

# Fix permissions
sudo chmod -R 755 /home/plagis/workspace/plagis_aumentum/web_frontend/

# Start API if not running
cd /home/plagis/workspace/plagis_aumentum
source venv/bin/activate
pkill -f aumentum_api  # Stop old instances
nohup python3 aumentum_api.py > api.log 2>&1 &

# Restart nginx
sudo systemctl restart nginx

# Allow firewall
sudo ufw allow 7000/tcp

# Wait for services
sleep 3

# Test
echo "Testing..."
curl -I http://127.0.0.1:7000/
```

---

**Run the diagnostic commands above and share what you see in the error log!**

