# TimeClock v3 - Apache2 Deployment Guide Spezielle Anleitung für das Deployment mit **Apache2** statt Nginx auf Ubuntu 22.04. ## Schnellstart ### Automatisches Deployment mit Apache2 Das `deploy.sh` Script ist bereits für Apache2 konfiguriert: ```bash cd /var/www/timeclock ./deploy.sh install ``` Das Script erkennt automatisch, dass Apache2 verwendet wird (Variable `WEBSERVER="apache2"` in Zeile 45). ## Manuelle Apache2-Installation ### 1. Apache2 und Module installieren ```bash # Apache2 installieren sudo apt update sudo apt install -y apache2 # Benötigte Module aktivieren sudo a2enmod proxy proxy_http ssl rewrite headers deflate expires # Apache2 neustarten sudo systemctl restart apache2 ``` ### 2. VirtualHost konfigurieren ```bash # Konfiguration kopieren sudo cp /var/www/timeclock/apache2.conf /etc/apache2/sites-available/stechuhr3.tsschulz.de.conf # Site aktivieren sudo a2ensite stechuhr3.tsschulz.de # Optional: Default-Site deaktivieren sudo a2dissite 000-default # Konfiguration testen sudo apache2ctl configtest # Apache2 neuladen sudo systemctl reload apache2 ``` ### 3. SSL mit Certbot ```bash # Certbot für Apache installieren sudo apt install -y certbot python3-certbot-apache # Zertifikat erstellen sudo certbot --apache -d stechuhr3.tsschulz.de ``` ## Apache2-Konfiguration erklärt Die `apache2.conf` enthält: ### Proxy-Konfiguration für Backend-API ```apache ProxyPass /api http://localhost:3010/api retry=0 ProxyPassReverse /api http://localhost:3010/api ``` Dies leitet alle `/api/*` Anfragen an das Node.js Backend auf Port 3010 weiter. ### SPA-Routing (Vue.js) ```apache RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/api RewriteRule . /index.html [L] ``` Dies sorgt dafür, dass alle Nicht-API-Anfragen an `index.html` geleitet werden (für Vue Router). ### Compression (gzip) ```apache AddOutputFilterByType DEFLATE text/html text/css application/javascript # ... weitere MIME-Types ``` ### Caching ```apache ExpiresActive On ExpiresByType text/css "access plus 1 year" ExpiresByType application/javascript "access plus 1 year" # ... weitere Cache-Regeln ``` ### Security Headers ```apache Header always set X-Frame-Options "SAMEORIGIN" Header always set X-Content-Type-Options "nosniff" Header always set Strict-Transport-Security "max-age=31536000" # ... weitere Security-Header ``` ## Wichtige Apache2-Befehle ### Sites verwalten ```bash # Site aktivieren sudo a2ensite stechuhr3.tsschulz.de # Site deaktivieren sudo a2dissite stechuhr3.tsschulz.de # Alle aktivierten Sites anzeigen ls -la /etc/apache2/sites-enabled/ ``` ### Module verwalten ```bash # Modul aktivieren sudo a2enmod proxy sudo a2enmod ssl sudo a2enmod rewrite # Modul deaktivieren sudo a2dismod module_name # Aktivierte Module anzeigen apache2ctl -M ``` ### Apache2 steuern ```bash # Status anzeigen sudo systemctl status apache2 # Starten sudo systemctl start apache2 # Stoppen sudo systemctl stop apache2 # Neustarten (Downtime) sudo systemctl restart apache2 # Neuladen (ohne Downtime) sudo systemctl reload apache2 # Konfiguration testen sudo apache2ctl configtest # oder sudo apachectl -t ``` ### Logs anzeigen ```bash # Access-Log sudo tail -f /var/log/apache2/stechuhr3-access.log # Error-Log sudo tail -f /var/log/apache2/stechuhr3-error.log # Alle Apache-Logs sudo tail -f /var/log/apache2/*.log ``` ## Troubleshooting ### Apache startet nicht ```bash # Detaillierte Fehlerausgabe sudo apache2ctl configtest # Systemd-Logs sudo journalctl -u apache2 -n 50 # Konfigurationsdateien prüfen sudo apache2ctl -S ``` ### Proxy funktioniert nicht ```bash # Prüfe ob Modul aktiviert ist apache2ctl -M | grep proxy # Falls nicht aktiviert: sudo a2enmod proxy proxy_http sudo systemctl restart apache2 # Backend-Verfügbarkeit prüfen curl http://localhost:3010/api/health ``` ### SSL-Probleme ```bash # Zertifikat prüfen sudo certbot certificates # Zertifikat erneuern sudo certbot renew --apache # SSL-Modul prüfen apache2ctl -M | grep ssl # Falls nicht aktiviert: sudo a2enmod ssl sudo systemctl restart apache2 ``` ### .htaccess wird ignoriert ```bash # Stelle sicher, dass AllowOverride gesetzt ist # In der VirtualHost-Konfiguration: AllowOverride All # Rewrite-Modul aktivieren sudo a2enmod rewrite sudo systemctl restart apache2 ``` ### Permissions-Probleme ```bash # Korrekter Besitzer sudo chown -R www-data:www-data /var/www/timeclock/frontend/dist # Korrekte Berechtigungen sudo find /var/www/timeclock/frontend/dist -type f -exec chmod 644 {} \; sudo find /var/www/timeclock/frontend/dist -type d -exec chmod 755 {} \; ``` ## Performance-Optimierung ### EnableKeepAlive Füge in `/etc/apache2/apache2.conf` hinzu: ```apache KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 5 ``` ### MPM-Modul optimieren ```bash # Zeige aktives MPM apache2ctl -V | grep MPM # Für Event MPM (empfohlen): sudo nano /etc/apache2/mods-available/mpm_event.conf ``` Beispiel-Konfiguration: ```apache StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 150 MaxConnectionsPerChild 0 ``` ### HTTP/2 aktivieren ```bash # HTTP/2 Modul aktivieren sudo a2enmod http2 # In VirtualHost hinzufügen: # Protocols h2 http/1.1 sudo systemctl restart apache2 ``` ### Caching verbessern Aktiviere mod_cache: ```bash sudo a2enmod cache sudo a2enmod cache_disk sudo systemctl restart apache2 ``` In der VirtualHost-Konfiguration: ```apache CacheQuickHandler off CacheLock on CacheLockPath /tmp/mod_cache-lock CacheLockMaxAge 5 CacheIgnoreHeaders Set-Cookie ``` ## Sicherheit ### mod_security installieren (Web Application Firewall) ```bash # Installieren sudo apt install -y libapache2-mod-security2 # Aktivieren sudo a2enmod security2 # Basis-Konfiguration sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf # SecRuleEngine auf "On" setzen sudo nano /etc/modsecurity/modsecurity.conf # SecRuleEngine On sudo systemctl restart apache2 ``` ### mod_evasive (DDoS-Schutz) ```bash # Installieren sudo apt install -y libapache2-mod-evasive # Konfigurieren sudo nano /etc/apache2/mods-available/evasive.conf ``` ```apache DOSHashTableSize 3097 DOSPageCount 5 DOSSiteCount 100 DOSPageInterval 1 DOSSiteInterval 1 DOSBlockingPeriod 10 DOSEmailNotify admin@tsschulz.de ``` ```bash sudo a2enmod evasive sudo systemctl restart apache2 ``` ### fail2ban für Apache ```bash # Installieren sudo apt install -y fail2ban # Apache-Jail aktivieren sudo nano /etc/fail2ban/jail.local ``` ```ini [apache-auth] enabled = true [apache-badbots] enabled = true [apache-noscript] enabled = true [apache-overflows] enabled = true ``` ```bash sudo systemctl restart fail2ban ``` ## Vergleich Nginx vs Apache2 | Feature | Nginx | Apache2 | |---------|-------|---------| | **Performance** | Sehr hoch (Event-driven) | Hoch (Process/Thread-based) | | **Konfiguration** | Einfacher | Komplexer, aber mächtiger | | **.htaccess** | Nicht unterstützt | Unterstützt | | **Module** | Weniger, aber effizienter | Sehr viele verfügbar | | **Best for** | Reverse Proxy, statische Dateien | .htaccess, komplexe Setups | | **Memory** | Geringer | Höher | Für TimeClock ist **beides** geeignet, Apache2 bietet mehr Flexibilität, Nginx mehr Performance. ## Migration von Nginx zu Apache2 Falls du bereits Nginx installiert hast: ```bash # Nginx stoppen und deaktivieren sudo systemctl stop nginx sudo systemctl disable nginx # Apache2 installieren und einrichten (siehe oben) # Firewall anpassen sudo ufw delete allow 'Nginx Full' sudo ufw allow 'Apache Full' # SSL-Zertifikat ist kompatibel, keine Änderung nötig ``` ## Nützliche Links - [Apache2 Dokumentation](https://httpd.apache.org/docs/2.4/) - [Apache2 auf Ubuntu](https://ubuntu.com/server/docs/web-servers-apache) - [Let's Encrypt mit Apache](https://certbot.eff.org/instructions?ws=apache&os=ubuntufocal) - [Apache Security Best Practices](https://geekflare.com/apache-web-server-hardening-security/) --- **Apache2 läuft! 🚀** Bei Fragen oder Problemen: Siehe `DEPLOYMENT.md` oder prüfe die Logs!