diff --git a/DEPLOYMENT_SOCKET_IO.md b/DEPLOYMENT_SOCKET_IO.md index b231412..744d30d 100644 --- a/DEPLOYMENT_SOCKET_IO.md +++ b/DEPLOYMENT_SOCKET_IO.md @@ -9,10 +9,6 @@ Socket.IO läuft jetzt direkt auf HTTPS-Port 3051 (nicht über Apache-Proxy). ```bash # UFW (Ubuntu Firewall) sudo ufw allow 3051/tcp - -# Oder iptables -sudo iptables -A INPUT -p tcp --dport 3051 -j ACCEPT -sudo iptables-save ``` ### 2. Apache-Konfiguration aktualisieren @@ -24,11 +20,20 @@ sudo systemctl restart apache2 ### 3. Backend neu starten +**WICHTIG:** Der Backend-Server muss neu gestartet werden, damit der HTTPS-Server auf Port 3051 läuft! + ```bash cd /var/www/tt-tagebuch.de/backend + +# Falls als systemd-Service: sudo systemctl restart tt-tagebuch-backend + # Oder falls als PM2-Prozess: pm2 restart tt-tagebuch-backend + +# Oder falls direkt mit node: +# Stoppe den laufenden Prozess und starte neu: +# node server.js ``` ### 4. Prüfen, ob HTTPS-Server läuft @@ -36,6 +41,8 @@ pm2 restart tt-tagebuch-backend ```bash # Prüfe, ob Port 3051 geöffnet ist sudo netstat -tlnp | grep 3051 +# Oder: +sudo ss -tlnp | grep 3051 # Prüfe Backend-Logs sudo journalctl -u tt-tagebuch-backend -f @@ -48,7 +55,19 @@ Du solltest folgende Meldung sehen: 🚀 HTTPS-Server für Socket.IO läuft auf Port 3051 ``` -### 5. Testen +### 5. Diagnose-Skript ausführen + +```bash +cd /var/www/tt-tagebuch.de/backend +node scripts/checkSocketIOServer.js +``` + +Dieses Skript prüft: +- Ob SSL-Zertifikate existieren +- Ob Port 3051 geöffnet ist +- Ob der Server erreichbar ist + +### 6. Testen Im Browser sollte Socket.IO jetzt direkt zu `wss://tt-tagebuch.de:3051` verbinden. @@ -56,17 +75,28 @@ Im Browser sollte Socket.IO jetzt direkt zu `wss://tt-tagebuch.de:3051` verbinde ### Port 3051 ist nicht erreichbar -1. Prüfe Firewall: +1. **Prüfe Firewall:** ```bash sudo ufw status + sudo ufw allow 3051/tcp ``` -2. Prüfe, ob der Server läuft: +2. **Prüfe, ob der Server läuft:** ```bash sudo netstat -tlnp | grep 3051 + sudo ss -tlnp | grep 3051 ``` -3. Prüfe Backend-Logs auf Fehler +3. **Prüfe Backend-Logs auf Fehler:** + ```bash + sudo journalctl -u tt-tagebuch-backend -n 50 + # Oder: + pm2 logs tt-tagebuch-backend --lines 50 + ``` + +4. **Prüfe, ob HTTPS-Server gestartet wurde:** + - Suche in den Logs nach: `🚀 HTTPS-Server für Socket.IO läuft auf Port 3051` + - Falls nicht vorhanden, prüfe auf Fehler: `⚠️ HTTPS-Server konnte nicht gestartet werden` ### SSL-Zertifikat-Fehler @@ -75,9 +105,31 @@ Stelle sicher, dass die Zertifikate existieren: ls -la /etc/letsencrypt/live/tt-tagebuch.de/ ``` +Falls die Zertifikate nicht existieren: +```bash +sudo certbot certonly --standalone -d tt-tagebuch.de +``` + ### Frontend verbindet nicht -1. Prüfe Browser-Konsole auf Fehler -2. Prüfe, ob `import.meta.env.PROD` korrekt gesetzt ist -3. Prüfe, ob die Socket.IO-URL korrekt ist (`https://tt-tagebuch.de:3051`) +1. **Prüfe Browser-Konsole auf Fehler** +2. **Prüfe, ob `import.meta.env.PROD` korrekt gesetzt ist:** + - In Produktion sollte die Socket.IO-URL `https://tt-tagebuch.de:3051` sein + - In Entwicklung sollte sie `http://localhost:3005` sein +3. **Prüfe, ob die Socket.IO-URL korrekt ist:** + - Öffne Browser-Entwicklertools → Network + - Suche nach WebSocket-Verbindungen + - Die URL sollte `wss://tt-tagebuch.de:3051/socket.io/...` sein + +### Server lauscht nur auf localhost + +Der Server sollte auf `0.0.0.0` lauschen (nicht nur auf `localhost`). +Dies ist bereits in der Konfiguration eingestellt: +```javascript +httpsServer.listen(httpsPort, '0.0.0.0', () => { + console.log(`🚀 HTTPS-Server für Socket.IO läuft auf Port ${httpsPort}`); +}); +``` + +Falls der Server trotzdem nicht erreichbar ist, prüfe die Backend-Logs. diff --git a/backend/scripts/checkSocketIOServer.js b/backend/scripts/checkSocketIOServer.js new file mode 100644 index 0000000..9024186 --- /dev/null +++ b/backend/scripts/checkSocketIOServer.js @@ -0,0 +1,84 @@ +#!/usr/bin/env node + +/** + * Prüft, ob der Socket.IO HTTPS-Server auf Port 3051 läuft + */ + +import https from 'https'; +import { readFileSync } from 'fs'; + +const httpsPort = process.env.HTTPS_PORT || 3051; + +console.log(`🔍 Prüfe Socket.IO HTTPS-Server auf Port ${httpsPort}...\n`); + +// Prüfe, ob Zertifikate existieren +try { + const keyPath = '/etc/letsencrypt/live/tt-tagebuch.de/privkey.pem'; + const certPath = '/etc/letsencrypt/live/tt-tagebuch.de/fullchain.pem'; + + readFileSync(keyPath); + readFileSync(certPath); + console.log('✅ SSL-Zertifikate gefunden'); +} catch (err) { + console.error('❌ SSL-Zertifikate nicht gefunden:', err.message); + console.error(' Erwartete Pfade:'); + console.error(' - /etc/letsencrypt/live/tt-tagebuch.de/privkey.pem'); + console.error(' - /etc/letsencrypt/live/tt-tagebuch.de/fullchain.pem'); + process.exit(1); +} + +// Prüfe, ob Port geöffnet ist +import { exec } from 'child_process'; +import { promisify } from 'util'; + +const execAsync = promisify(exec); + +try { + const { stdout } = await execAsync(`netstat -tlnp 2>/dev/null | grep :${httpsPort} || ss -tlnp 2>/dev/null | grep :${httpsPort} || echo "Port nicht gefunden"`); + if (stdout.includes(':' + httpsPort)) { + console.log(`✅ Port ${httpsPort} ist geöffnet und lauscht`); + console.log(` ${stdout.trim()}`); + } else { + console.log(`❌ Port ${httpsPort} ist nicht geöffnet oder lauscht nicht`); + console.log(' → Backend-Server muss neu gestartet werden'); + } +} catch (err) { + console.error('⚠️ Konnte Port-Status nicht prüfen:', err.message); +} + +// Versuche Verbindung zum Server +console.log(`\n🔌 Versuche Verbindung zu https://localhost:${httpsPort}/socket.io/...`); + +const options = { + hostname: 'localhost', + port: httpsPort, + path: '/socket.io/?EIO=4&transport=polling', + method: 'GET', + rejectUnauthorized: false +}; + +const req = https.request(options, (res) => { + console.log(`✅ Verbindung erfolgreich! Status: ${res.statusCode}`); + res.on('data', (chunk) => { + console.log(` Response: ${chunk.toString().substring(0, 100)}...`); + }); + res.on('end', () => { + console.log('\n✅ Socket.IO HTTPS-Server läuft korrekt!'); + }); +}); + +req.on('error', (err) => { + console.error(`❌ Verbindung fehlgeschlagen: ${err.message}`); + console.error(' → Backend-Server läuft möglicherweise nicht'); + console.error(' → Oder HTTPS-Server wurde nicht gestartet'); + process.exit(1); +}); + +req.setTimeout(5000, () => { + console.error('❌ Timeout beim Verbindungsversuch'); + req.destroy(); + process.exit(1); +}); + +req.end(); + diff --git a/backend/server.js b/backend/server.js index 5fbe3fd..42a9847 100644 --- a/backend/server.js +++ b/backend/server.js @@ -283,7 +283,7 @@ app.get('*', (req, res) => { // Initialisiere Socket.IO auf HTTPS-Server initializeSocketIO(httpsServer); - httpsServer.listen(httpsPort, () => { + httpsServer.listen(httpsPort, '0.0.0.0', () => { console.log(`🚀 HTTPS-Server für Socket.IO läuft auf Port ${httpsPort}`); }); } catch (err) {