up
This commit is contained in:
@@ -0,0 +1,270 @@
|
||||
# 🚀 Configuración SSE para HestiaCP
|
||||
|
||||
## ✅ Pasos para Activar SSE en tu Servidor HestiaCP
|
||||
|
||||
### 1. Verificar que el código esté en el servidor
|
||||
```bash
|
||||
# Subir por FTP/SFTP o Git
|
||||
cd /home/tu-usuario/web/tu-dominio.com/public_html/
|
||||
ls -la api/sse_events.php # Debe existir
|
||||
```
|
||||
|
||||
### 2. Configurar Apache para SSE (si usas Apache)
|
||||
|
||||
**Opción A: Editar .htaccess en la raíz**
|
||||
```apache
|
||||
# Agregar al final de .htaccess
|
||||
<IfModule mod_headers.c>
|
||||
# SSE Headers
|
||||
<FilesMatch "sse_events\.php$">
|
||||
Header set Cache-Control "no-cache, no-store, must-revalidate"
|
||||
Header set X-Accel-Buffering "no"
|
||||
SetEnv no-gzip 1
|
||||
</FilesMatch>
|
||||
</IfModule>
|
||||
```
|
||||
|
||||
**Opción B: Crear .htaccess específico en /api/**
|
||||
```bash
|
||||
cd /home/tu-usuario/web/tu-dominio.com/public_html/api/
|
||||
nano .htaccess
|
||||
```
|
||||
|
||||
Contenido:
|
||||
```apache
|
||||
<IfModule mod_headers.c>
|
||||
Header set Cache-Control "no-cache, no-store, must-revalidate"
|
||||
Header set X-Accel-Buffering "no"
|
||||
SetEnv no-gzip 1
|
||||
</IfModule>
|
||||
|
||||
# Prevenir timeout en SSE
|
||||
<IfModule mod_fcgid.c>
|
||||
FcgidIOTimeout 300
|
||||
</IfModule>
|
||||
```
|
||||
|
||||
### 3. Ajustar PHP-FPM (Recomendado)
|
||||
|
||||
Editar configuración PHP-FPM en HestiaCP:
|
||||
|
||||
1. **Panel HestiaCP** → **Web** → **tu dominio** → **Edit**
|
||||
2. **Backend Template**: PHP-FPM
|
||||
3. **PHP Version**: 8.x
|
||||
|
||||
Luego editar el archivo de configuración (vía SSH):
|
||||
```bash
|
||||
# Ubicación típica en HestiaCP
|
||||
sudo nano /etc/php/8.2/fpm/pool.d/tu-usuario.conf
|
||||
|
||||
# Agregar o modificar:
|
||||
request_terminate_timeout = 300s
|
||||
pm.max_requests = 500
|
||||
```
|
||||
|
||||
Reiniciar PHP-FPM:
|
||||
```bash
|
||||
sudo systemctl restart php8.2-fpm
|
||||
```
|
||||
|
||||
### 4. Verificar Nginx (si usas Nginx)
|
||||
|
||||
Si tu dominio usa Nginx como proxy, editar la configuración:
|
||||
|
||||
```bash
|
||||
sudo nano /etc/nginx/conf.d/domains/tu-dominio.com.conf
|
||||
```
|
||||
|
||||
Agregar dentro del bloque `location ~ \.php$`:
|
||||
```nginx
|
||||
# Configuración especial para SSE
|
||||
location ~ ^/api/sse_events\.php$ {
|
||||
fastcgi_buffering off;
|
||||
proxy_buffering off;
|
||||
|
||||
fastcgi_read_timeout 300s;
|
||||
fastcgi_send_timeout 300s;
|
||||
|
||||
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
|
||||
include fastcgi_params;
|
||||
}
|
||||
```
|
||||
|
||||
Reiniciar Nginx:
|
||||
```bash
|
||||
sudo systemctl restart nginx
|
||||
```
|
||||
|
||||
### 5. Probar SSE desde el servidor
|
||||
|
||||
```bash
|
||||
# Conectar por SSH a tu servidor
|
||||
ssh usuario@tu-dominio.com
|
||||
|
||||
# Probar SSE
|
||||
curl -N -m 5 'https://tu-dominio.com/api/sse_events.php?token=demo_token'
|
||||
```
|
||||
|
||||
**Respuesta esperada:**
|
||||
```
|
||||
event: connected
|
||||
data: {"timestamp":1706300000,"user_id":"token_12345678","mode":"authenticated","status":"ready"}
|
||||
|
||||
: heartbeat
|
||||
```
|
||||
|
||||
Si ves esto, **SSE está funcionando** ✅
|
||||
|
||||
### 6. Probar desde el navegador
|
||||
|
||||
1. Abrir tu sitio: `https://tu-dominio.com/conversations.php`
|
||||
2. Abrir Consola del navegador (F12 → Console)
|
||||
3. Buscar:
|
||||
```
|
||||
Conectando a SSE para eventos en tiempo real...
|
||||
SSE URL: https://tu-dominio.com/api/sse_events.php?token=...
|
||||
✅ SSE conectado (🔐 autenticado): {...}
|
||||
```
|
||||
|
||||
### 7. Solución de Problemas
|
||||
|
||||
#### Error: "Failed to fetch" o timeout
|
||||
|
||||
**Causa**: Firewall o mod_security bloqueando
|
||||
**Solución**:
|
||||
```bash
|
||||
# Desactivar mod_security para SSE (temporal)
|
||||
sudo a2dismod security2
|
||||
sudo systemctl restart apache2
|
||||
|
||||
# O agregar excepción en .htaccess:
|
||||
<IfModule mod_security2.c>
|
||||
SecRuleEngine Off
|
||||
</IfModule>
|
||||
```
|
||||
|
||||
#### Error: "EventSource's response has a MIME type"
|
||||
|
||||
**Causa**: Headers incorrectos
|
||||
**Solución**: Verificar que sse_events.php tenga:
|
||||
```php
|
||||
header('Content-Type: text/event-stream');
|
||||
```
|
||||
|
||||
#### Error: Conexión se cierra cada 30 segundos
|
||||
|
||||
**Causa**: Timeout de proxy
|
||||
**Solución**: Aumentar timeouts en nginx.conf o apache2.conf:
|
||||
```
|
||||
# Apache
|
||||
Timeout 300
|
||||
KeepAliveTimeout 300
|
||||
|
||||
# Nginx
|
||||
proxy_read_timeout 300s;
|
||||
```
|
||||
|
||||
### 8. Configuración de Seguridad (Producción)
|
||||
|
||||
#### A. Token seguro en vez de demo_token
|
||||
|
||||
Editar `api/sse_events.php`:
|
||||
```php
|
||||
// Generar token en login
|
||||
$_SESSION['sse_token'] = bin2hex(random_bytes(32));
|
||||
|
||||
// Validar token en SSE
|
||||
$token = $_GET['token'] ?? null;
|
||||
if ($token && isset($_SESSION['sse_token']) && $token === $_SESSION['sse_token']) {
|
||||
$authenticated = true;
|
||||
}
|
||||
```
|
||||
|
||||
En `conversations.php`:
|
||||
```javascript
|
||||
// Usar token de sesión
|
||||
const token = '<?php echo $_SESSION['sse_token'] ?? 'demo_token'; ?>';
|
||||
const sseUrl = `${baseUrl}/api/sse_events.php?token=${token}&t=${Date.now()}`;
|
||||
```
|
||||
|
||||
#### B. Restringir acceso por IP (opcional)
|
||||
|
||||
En `.htaccess`:
|
||||
```apache
|
||||
<FilesMatch "sse_events\.php$">
|
||||
# Solo permitir desde localhost o IPs específicas
|
||||
Require ip 127.0.0.1
|
||||
Require ip tu.ip.publica
|
||||
</FilesMatch>
|
||||
```
|
||||
|
||||
### 9. Monitoreo
|
||||
|
||||
Ver conexiones SSE activas:
|
||||
```bash
|
||||
# Ver procesos PHP activos
|
||||
ps aux | grep sse_events
|
||||
|
||||
# Ver logs de errores
|
||||
tail -f /var/log/php8.2-fpm.log
|
||||
tail -f /var/log/apache2/error.log
|
||||
tail -f /var/log/nginx/error.log
|
||||
```
|
||||
|
||||
### 10. Performance
|
||||
|
||||
Para muchos usuarios simultáneos:
|
||||
|
||||
```bash
|
||||
# Aumentar límites PHP-FPM
|
||||
sudo nano /etc/php/8.2/fpm/pool.d/tu-usuario.conf
|
||||
|
||||
pm = dynamic
|
||||
pm.max_children = 50
|
||||
pm.start_servers = 10
|
||||
pm.min_spare_servers = 5
|
||||
pm.max_spare_servers = 20
|
||||
pm.max_requests = 500
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🎯 Checklist de Verificación
|
||||
|
||||
- [ ] Archivo `api/sse_events.php` subido al servidor
|
||||
- [ ] `.htaccess` configurado con headers SSE
|
||||
- [ ] PHP-FPM timeout aumentado a 300s
|
||||
- [ ] Nginx/Apache configurado (si aplica)
|
||||
- [ ] Test con curl funciona (muestra "event: connected")
|
||||
- [ ] Test desde navegador funciona (console.log muestra "✅ SSE conectado")
|
||||
- [ ] Token de seguridad configurado (no usar demo_token en producción)
|
||||
- [ ] Firewall/mod_security permite conexiones largas
|
||||
|
||||
---
|
||||
|
||||
## 📞 Soporte HestiaCP
|
||||
|
||||
Si tienes problemas:
|
||||
|
||||
1. **Panel HestiaCP**: https://tu-dominio.com:8083
|
||||
2. **Logs**: Panel → Log Files
|
||||
3. **SSH**: Usuario y contraseña en el panel
|
||||
4. **Documentación**: https://docs.hestiacp.com/
|
||||
|
||||
---
|
||||
|
||||
## ✅ Resultado Esperado
|
||||
|
||||
Una vez configurado correctamente:
|
||||
|
||||
```
|
||||
ANTES (desarrollo local):
|
||||
http://localhost:8000/api/sse_events.php
|
||||
❌ Timeout (servidor PHP built-in no soporta SSE)
|
||||
|
||||
AHORA (producción HestiaCP):
|
||||
https://tu-dominio.com/api/sse_events.php
|
||||
✅ Conectado - eventos en tiempo real < 1 segundo
|
||||
```
|
||||
|
||||
**¡Tu sistema de notificaciones en tiempo real estará funcionando!** 🚀
|
||||
Reference in New Issue
Block a user