Update: Dokumentation und Production Setup überarbeitet
This commit is contained in:
@@ -1 +1,327 @@
|
||||
|
||||
# MYP Backend - Produktions-Setup Anleitung
|
||||
|
||||
Diese Anleitung beschreibt die Installation und Konfiguration des MYP Backends für den Produktionsbetrieb.
|
||||
|
||||
## Voraussetzungen
|
||||
|
||||
- Linux-Server (Ubuntu 20.04 LTS oder höher empfohlen)
|
||||
- Python 3.8 oder höher
|
||||
- Nginx (optional, für Reverse Proxy)
|
||||
- Systemd (für Service-Management)
|
||||
|
||||
## Installation
|
||||
|
||||
### 1. System-Updates und Abhängigkeiten
|
||||
|
||||
```bash
|
||||
sudo apt update && sudo apt upgrade -y
|
||||
sudo apt install python3 python3-pip python3-venv nginx sqlite3 -y
|
||||
```
|
||||
|
||||
### 2. Benutzer und Verzeichnisse erstellen
|
||||
|
||||
```bash
|
||||
# MYP-Benutzer erstellen
|
||||
sudo useradd --system --group --home /opt/myp myp
|
||||
|
||||
# Verzeichnisse erstellen
|
||||
sudo mkdir -p /opt/myp/{backend,logs}
|
||||
sudo chown -R myp:myp /opt/myp
|
||||
```
|
||||
|
||||
### 3. Anwendung installieren
|
||||
|
||||
```bash
|
||||
# Als myp-Benutzer wechseln
|
||||
sudo -u myp bash
|
||||
|
||||
# In das Backend-Verzeichnis wechseln
|
||||
cd /opt/myp/backend
|
||||
|
||||
# Repository klonen (oder Dateien kopieren)
|
||||
# git clone https://github.com/your-org/myp.git .
|
||||
|
||||
# Virtual Environment erstellen
|
||||
python3 -m venv /opt/myp/venv
|
||||
source /opt/myp/venv/bin/activate
|
||||
|
||||
# Dependencies installieren
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
### 4. Konfiguration
|
||||
|
||||
```bash
|
||||
# Umgebungsvariablen konfigurieren
|
||||
cp env.example .env
|
||||
nano .env
|
||||
|
||||
# Wichtige Konfigurationen:
|
||||
# - SECRET_KEY: Sicheren Schlüssel generieren
|
||||
# - TAPO_USERNAME/TAPO_PASSWORD: Tapo-Anmeldedaten
|
||||
# - PRINTERS: Drucker-Konfiguration als JSON
|
||||
```
|
||||
|
||||
#### Beispiel für sichere SECRET_KEY-Generierung:
|
||||
|
||||
```bash
|
||||
python3 -c "import secrets; print(secrets.token_hex(32))"
|
||||
```
|
||||
|
||||
### 5. Datenbank initialisieren
|
||||
|
||||
```bash
|
||||
# Datenbank-Verzeichnis erstellen
|
||||
mkdir -p instance
|
||||
|
||||
# Flask-Anwendung starten, um Datenbank zu initialisieren
|
||||
python3 app.py
|
||||
# Ctrl+C nach erfolgreicher Initialisierung
|
||||
```
|
||||
|
||||
### 6. Systemd Service einrichten
|
||||
|
||||
```bash
|
||||
# Als root-Benutzer
|
||||
sudo cp myp-backend.service /etc/systemd/system/
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable myp-backend
|
||||
sudo systemctl start myp-backend
|
||||
```
|
||||
|
||||
### 7. Service-Status überprüfen
|
||||
|
||||
```bash
|
||||
sudo systemctl status myp-backend
|
||||
sudo journalctl -u myp-backend -f
|
||||
```
|
||||
|
||||
## Nginx Reverse Proxy (Optional)
|
||||
|
||||
### Nginx-Konfiguration erstellen
|
||||
|
||||
```bash
|
||||
sudo nano /etc/nginx/sites-available/myp-backend
|
||||
```
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name your-domain.com;
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:5000;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_connect_timeout 30;
|
||||
proxy_send_timeout 30;
|
||||
proxy_read_timeout 30;
|
||||
}
|
||||
|
||||
# Statische Dateien direkt ausliefern
|
||||
location /static/ {
|
||||
alias /opt/myp/backend/static/;
|
||||
expires 1h;
|
||||
add_header Cache-Control "public, immutable";
|
||||
}
|
||||
|
||||
# Health Check
|
||||
location /monitoring/health/simple {
|
||||
access_log off;
|
||||
proxy_pass http://127.0.0.1:5000;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Nginx aktivieren
|
||||
|
||||
```bash
|
||||
sudo ln -s /etc/nginx/sites-available/myp-backend /etc/nginx/sites-enabled/
|
||||
sudo nginx -t
|
||||
sudo systemctl reload nginx
|
||||
```
|
||||
|
||||
## SSL/HTTPS Setup (Empfohlen)
|
||||
|
||||
### Mit Let's Encrypt (Certbot)
|
||||
|
||||
```bash
|
||||
sudo apt install certbot python3-certbot-nginx -y
|
||||
sudo certbot --nginx -d your-domain.com
|
||||
```
|
||||
|
||||
### Umgebungsvariable für HTTPS setzen
|
||||
|
||||
```bash
|
||||
# In .env-Datei
|
||||
FORCE_HTTPS=true
|
||||
```
|
||||
|
||||
## Monitoring und Logs
|
||||
|
||||
### Log-Dateien
|
||||
|
||||
- Anwendungs-Logs: `/opt/myp/backend/logs/myp.log`
|
||||
- Error-Logs: `/opt/myp/backend/logs/myp-errors.log`
|
||||
- Security-Logs: `/opt/myp/backend/logs/security.log`
|
||||
- Systemd-Logs: `journalctl -u myp-backend`
|
||||
|
||||
### Health Check-Endpunkte
|
||||
|
||||
- Einfacher Health Check: `http://your-domain.com/monitoring/health/simple`
|
||||
- Detaillierter Health Check: `http://your-domain.com/monitoring/health`
|
||||
- System-Metriken: `http://your-domain.com/monitoring/metrics`
|
||||
- Anwendungsinfo: `http://your-domain.com/monitoring/info`
|
||||
|
||||
## Wartung und Updates
|
||||
|
||||
### Service neustarten
|
||||
|
||||
```bash
|
||||
sudo systemctl restart myp-backend
|
||||
```
|
||||
|
||||
### Logs rotieren
|
||||
|
||||
Die Log-Rotation ist automatisch konfiguriert. Bei Bedarf manuell:
|
||||
|
||||
```bash
|
||||
sudo logrotate -f /etc/logrotate.d/myp-backend
|
||||
```
|
||||
|
||||
### Updates installieren
|
||||
|
||||
```bash
|
||||
sudo -u myp bash
|
||||
cd /opt/myp/backend
|
||||
source /opt/myp/venv/bin/activate
|
||||
|
||||
# Code aktualisieren
|
||||
git pull
|
||||
|
||||
# Dependencies aktualisieren
|
||||
pip install -r requirements.txt --upgrade
|
||||
|
||||
# Service neustarten
|
||||
sudo systemctl restart myp-backend
|
||||
```
|
||||
|
||||
### Datenbank-Backup
|
||||
|
||||
```bash
|
||||
# Backup erstellen
|
||||
sudo -u myp sqlite3 /opt/myp/backend/instance/myp.db ".backup /opt/myp/backup/myp_$(date +%Y%m%d_%H%M%S).db"
|
||||
|
||||
# Automatisches Backup via Cron
|
||||
sudo -u myp crontab -e
|
||||
# Füge hinzu: 0 2 * * * sqlite3 /opt/myp/backend/instance/myp.db ".backup /opt/myp/backup/myp_$(date +\%Y\%m\%d_\%H\%M\%S).db"
|
||||
```
|
||||
|
||||
## Sicherheit
|
||||
|
||||
### Firewall konfigurieren
|
||||
|
||||
```bash
|
||||
sudo ufw allow ssh
|
||||
sudo ufw allow 'Nginx Full'
|
||||
sudo ufw --force enable
|
||||
```
|
||||
|
||||
### Fail2Ban für zusätzlichen Schutz
|
||||
|
||||
```bash
|
||||
sudo apt install fail2ban -y
|
||||
sudo nano /etc/fail2ban/jail.local
|
||||
```
|
||||
|
||||
```ini
|
||||
[DEFAULT]
|
||||
bantime = 3600
|
||||
findtime = 600
|
||||
maxretry = 5
|
||||
|
||||
[nginx-http-auth]
|
||||
enabled = true
|
||||
port = http,https
|
||||
logpath = /var/log/nginx/error.log
|
||||
```
|
||||
|
||||
### Regelmäßige Security-Updates
|
||||
|
||||
```bash
|
||||
sudo apt install unattended-upgrades -y
|
||||
sudo dpkg-reconfigure -plow unattended-upgrades
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Service startet nicht
|
||||
|
||||
```bash
|
||||
# Logs überprüfen
|
||||
sudo journalctl -u myp-backend --no-pager
|
||||
sudo -u myp cat /opt/myp/backend/logs/myp-errors.log
|
||||
```
|
||||
|
||||
### Hohe Speichernutzung
|
||||
|
||||
```bash
|
||||
# Memory-Statistiken
|
||||
sudo systemctl status myp-backend
|
||||
sudo ps aux | grep gunicorn
|
||||
```
|
||||
|
||||
### Datenbankprobleme
|
||||
|
||||
```bash
|
||||
# Datenbank-Integrität prüfen
|
||||
sudo -u myp sqlite3 /opt/myp/backend/instance/myp.db "PRAGMA integrity_check;"
|
||||
```
|
||||
|
||||
### Netzwerk-Konnektivität
|
||||
|
||||
```bash
|
||||
# Port-Verfügbarkeit prüfen
|
||||
sudo netstat -tlnp | grep :5000
|
||||
curl -I http://localhost:5000/monitoring/health/simple
|
||||
```
|
||||
|
||||
## Performance-Optimierung
|
||||
|
||||
### Gunicorn Worker anpassen
|
||||
|
||||
```bash
|
||||
# In .env oder Service-Datei
|
||||
WORKERS=8 # 2 * CPU-Kerne + 1
|
||||
```
|
||||
|
||||
### Nginx-Caching aktivieren
|
||||
|
||||
```nginx
|
||||
# In Nginx-Konfiguration
|
||||
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
|
||||
expires 1y;
|
||||
add_header Cache-Control "public, immutable";
|
||||
}
|
||||
```
|
||||
|
||||
### SQLite-Optimierung
|
||||
|
||||
```sql
|
||||
-- Für bessere Performance (einmalig ausführen)
|
||||
PRAGMA journal_mode=WAL;
|
||||
PRAGMA synchronous=NORMAL;
|
||||
PRAGMA cache_size=10000;
|
||||
PRAGMA temp_store=memory;
|
||||
```
|
||||
|
||||
## Support
|
||||
|
||||
Bei Problemen oder Fragen:
|
||||
|
||||
1. Überprüfen Sie die Log-Dateien
|
||||
2. Testen Sie die Health Check-Endpunkte
|
||||
3. Konsultieren Sie die Systemd-Logs
|
||||
4. Kontaktieren Sie das Entwicklungsteam
|
Reference in New Issue
Block a user