Files
stechuhr3/APACHE2_DEPLOYMENT.md

8.5 KiB

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:

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

# 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

# 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

# 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

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)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/api
    RewriteRule . /index.html [L]
</IfModule>

Dies sorgt dafür, dass alle Nicht-API-Anfragen an index.html geleitet werden (für Vue Router).

Compression (gzip)

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css application/javascript
    # ... weitere MIME-Types
</IfModule>

Caching

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    # ... weitere Cache-Regeln
</IfModule>

Security Headers

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

# 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

# Modul aktivieren
sudo a2enmod proxy
sudo a2enmod ssl
sudo a2enmod rewrite

# Modul deaktivieren
sudo a2dismod module_name

# Aktivierte Module anzeigen
apache2ctl -M

Apache2 steuern

# 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

# 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

# Detaillierte Fehlerausgabe
sudo apache2ctl configtest

# Systemd-Logs
sudo journalctl -u apache2 -n 50

# Konfigurationsdateien prüfen
sudo apache2ctl -S

Proxy funktioniert nicht

# 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

# 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

# Stelle sicher, dass AllowOverride gesetzt ist
# In der VirtualHost-Konfiguration:
<Directory /var/www/timeclock/frontend/dist>
    AllowOverride All
</Directory>

# Rewrite-Modul aktivieren
sudo a2enmod rewrite
sudo systemctl restart apache2

Permissions-Probleme

# 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:

KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

MPM-Modul optimieren

# Zeige aktives MPM
apache2ctl -V | grep MPM

# Für Event MPM (empfohlen):
sudo nano /etc/apache2/mods-available/mpm_event.conf

Beispiel-Konfiguration:

<IfModule mpm_event_module>
    StartServers             2
    MinSpareThreads         25
    MaxSpareThreads         75
    ThreadLimit             64
    ThreadsPerChild         25
    MaxRequestWorkers      150
    MaxConnectionsPerChild   0
</IfModule>

HTTP/2 aktivieren

# 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:

sudo a2enmod cache
sudo a2enmod cache_disk
sudo systemctl restart apache2

In der VirtualHost-Konfiguration:

<IfModule mod_cache.c>
    CacheQuickHandler off
    CacheLock on
    CacheLockPath /tmp/mod_cache-lock
    CacheLockMaxAge 5
    CacheIgnoreHeaders Set-Cookie
</IfModule>

Sicherheit

mod_security installieren (Web Application Firewall)

# 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)

# Installieren
sudo apt install -y libapache2-mod-evasive

# Konfigurieren
sudo nano /etc/apache2/mods-available/evasive.conf
<IfModule mod_evasive20.c>
    DOSHashTableSize 3097
    DOSPageCount 5
    DOSSiteCount 100
    DOSPageInterval 1
    DOSSiteInterval 1
    DOSBlockingPeriod 10
    DOSEmailNotify admin@tsschulz.de
</IfModule>
sudo a2enmod evasive
sudo systemctl restart apache2

fail2ban für Apache

# Installieren
sudo apt install -y fail2ban

# Apache-Jail aktivieren
sudo nano /etc/fail2ban/jail.local
[apache-auth]
enabled = true

[apache-badbots]
enabled = true

[apache-noscript]
enabled = true

[apache-overflows]
enabled = true
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:

# 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

Apache2 läuft! 🚀

Bei Fragen oder Problemen: Siehe DEPLOYMENT.md oder prüfe die Logs!