# Guía de Solución - Error 500 xgestion.com.ar

## Pasos para Solucionar

### 1. Conectarse al servidor EC2

```bash
ssh -i "xGestion-Prod-Server.pem" admin@34.199.75.144
# o
ssh -i "xGestion-Prod-Server.pem" ubuntu@34.199.75.144
# o
ssh -i "xGestion-Prod-Server.pem" ec2-user@34.199.75.144
```

### 2. Verificar el problema actual

```bash
# Ver el error actual
curl -I http://localhost/

# Debería mostrar: HTTP/1.1 500 Internal Server Error
```

### 3. Opción A: Usar el script automático (RECOMENDADO)

```bash
# Copiar el script al servidor (desde tu máquina local)
scp -i "xGestion-Prod-Server.pem" fix-database-config.sh admin@34.199.75.144:~/

# En el servidor, ejecutar:
chmod +x fix-database-config.sh
bash fix-database-config.sh
```

### 4. Opción B: Solución manual

```bash
# Hacer backup
sudo cp /var/www/html/includes/config.php /var/www/html/includes/config.php.backup

# Editar el archivo
sudo nano /var/www/html/includes/config.php

# Buscar estas líneas (alrededor de la línea 30-35):
$conn_serv = 'localhost';
$conn_user = 'xgestion';
$conn_pass = 'Lucecitas1967';
$conn_db   = 'system';

# Cambiarlas por:
$conn_serv = 'hummingdb.c8h5m8x9v0zq.us-east-1.rds.amazonaws.com';
$conn_user = 'hudbsuperadmin';
$conn_pass = 'Ep0WsrgCK6o5XuON4CnT';
$conn_db   = 'system';

# Guardar: Ctrl+O, Enter, Ctrl+X

# Reiniciar Apache
sudo systemctl restart apache2
```

### 5. Verificar que funciona

```bash
# Probar el sitio
curl -I http://localhost/

# Debería mostrar: HTTP/1.1 200 OK (o 302 redirect)

# Probar con el dominio
curl -I -H "Host: xgestion.com.ar" http://localhost/

# Ver logs si hay problemas
sudo tail -f /var/log/apache2/error.log
```

### 6. Verificar el Target Group desde tu máquina local

```bash
aws elbv2 describe-target-health \
  --target-group-arn arn:aws:elasticloadbalancing:us-east-1:788424700802:targetgroup/xGestion-TargetGroup/13587337443234cb \
  --region us-east-1 \
  --profile xgestion
```

Debería mostrar: `"State": "healthy"`

### 7. Probar la API de vuelos

```bash
# Desde tu máquina local
curl -I https://hummingairways.xgestion.com.ar/intranet/sys/mods/flights/motor.php
```

## Troubleshooting

### Si sigue sin funcionar:

```bash
# Ver logs de Apache
sudo tail -50 /var/log/apache2/error.log

# Ver logs de PHP
sudo tail -50 /var/log/php*.log

# Verificar que Apache está corriendo
sudo systemctl status apache2

# Verificar conectividad a RDS
mysql -h hummingdb.c8h5m8x9v0zq.us-east-1.rds.amazonaws.com \
      -u hudbsuperadmin \
      -pEp0WsrgCK6o5XuON4CnT \
      -e "SELECT 'OK' as status;"

# Si no está instalado mysql-client:
sudo apt-get update
sudo apt-get install mysql-client -y
```

### Si el archivo config.php está en otra ubicación:

```bash
# Buscar el archivo
find /var/www -name "config.php" -type f 2>/dev/null | grep includes

# Una vez encontrado, usar esa ruta en los comandos anteriores
```

### Verificar permisos del archivo:

```bash
ls -la /var/www/html/includes/config.php

# Debería ser:
# -rw-r--r-- 1 www-data www-data (o similar)

# Si no, corregir:
sudo chown www-data:www-data /var/www/html/includes/config.php
sudo chmod 644 /var/www/html/includes/config.php
```

## Verificación Final

Una vez solucionado, verificar:

1. ✅ `curl -I https://xgestion.com.ar/` → HTTP 200 o 302
2. ✅ Target Group → State: healthy
3. ✅ API de vuelos → Responde correctamente
4. ✅ Certificado SSL → Válido y funcionando

## Rollback (si algo sale mal)

```bash
# Restaurar el backup
sudo cp /var/www/html/includes/config.php.backup /var/www/html/includes/config.php
sudo systemctl restart apache2
```

---

**Tiempo estimado**: 5-10 minutos  
**Riesgo**: Bajo (se hace backup antes de modificar)  
**Impacto**: Soluciona el error 500 y restaura el funcionamiento del sitio
