Zum Hauptinhalt springen
Zurück zur Kategorie

Nginx und Apache Installation: Virtual Host Konfiguration

Nginx und Apache Webserver installieren und konfigurieren, Stärken vergleichen, Virtual Hosts einrichten, SSL/TLS konfigurieren und Webdienste unter Linux verwalten.

Lesezeit: 15 min Serververwaltung
nginxapachewebservervirtual hostssllinuxhttphttps

Inhaltsverzeichnis

Nginx und Apache Installation: Virtual Host Konfiguration

Nginx und Apache sind die zwei meistgenutzten Webserver weltweit. Beide können Webanwendungen, statische Dateien bereitstellen und als Reverse Proxy auf Linux-Servern fungieren. Dieser Leitfaden behandelt Installation, Grundkonfiguration und Virtual Host-Einrichtung für beide.

Nginx vs Apache Vergleich

MerkmalNginxApache
ArchitekturAsynchron, event-drivenProzess/Thread-basiert
Statische DateienSehr schnellLangsamer
Dynamischer InhaltReverse Proxy erforderlichEingebaute Module
SpeichernutzungGeringHöher
KonfigurationEinfacher.htaccess-Unterstützung
Beste VerwendungHochfrequentierte SeitenPHP-Anwendungen

Wählen Sie Nginx für hochfrequentierte statische Inhalte oder Reverse Proxy; wählen Sie Apache für PHP-basierte Anwendungen (WordPress, Laravel). Beide können auch zusammen verwendet werden: Nginx als Front-End Reverse Proxy, Apache als Back-End Anwendungsserver.

Nginx installieren

Debian/Ubuntu

hljs bash
# Paketliste aktualisieren
sudo apt update

# Nginx installieren
sudo apt install nginx -y

# Dienst starten und aktivieren
sudo systemctl start nginx
sudo systemctl enable nginx

# Status prüfen
sudo systemctl status nginx

RHEL/CentOS/AlmaLinux

hljs bash
# EPEL-Repository hinzufügen
sudo dnf install epel-release -y

# Nginx installieren
sudo dnf install nginx -y

# Dienst starten und aktivieren
sudo systemctl start nginx
sudo systemctl enable nginx

Grundlegende Nginx-Befehle

hljs bash
# Konfiguration testen
sudo nginx -t

# Konfiguration neu laden (kein Ausfall)
sudo systemctl reload nginx

# Dienst neu starten
sudo systemctl restart nginx

# Nginx-Version anzeigen
nginx -v

Nginx Virtual Host Konfiguration

In Nginx wird für jede Website eine separate Konfigurationsdatei unter /etc/nginx/sites-available/ erstellt.

Grundlegender Virtual Host

hljs bash
# Konfigurationsdatei erstellen
sudo nano /etc/nginx/sites-available/example.com
hljs nginx
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    # Für PHP-FPM
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }

    # Zugriff auf versteckte Dateien blockieren
    location ~ /\.ht {
        deny all;
    }

    # Log-Dateien
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}
hljs bash
# Website aktivieren
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

# Web-Verzeichnis erstellen
sudo mkdir -p /var/www/example.com/html
sudo chown -R www-data:www-data /var/www/example.com

# Testen und neu laden
sudo nginx -t && sudo systemctl reload nginx

Nginx Reverse Proxy Konfiguration

hljs nginx
server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        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_cache_bypass $http_upgrade;
    }
}

Apache installieren

Debian/Ubuntu

hljs bash
# Apache installieren
sudo apt update
sudo apt install apache2 -y

# Dienst starten und aktivieren
sudo systemctl start apache2
sudo systemctl enable apache2

# Status prüfen
sudo systemctl status apache2

RHEL/CentOS/AlmaLinux

hljs bash
# Apache (httpd) installieren
sudo dnf install httpd -y

# Dienst starten und aktivieren
sudo systemctl start httpd
sudo systemctl enable httpd

Grundlegende Apache-Befehle

hljs bash
# Konfiguration testen
sudo apachectl configtest

# Konfiguration neu laden
sudo systemctl reload apache2

# Module aktivieren
sudo a2enmod rewrite
sudo a2enmod ssl

# Sites aktivieren/deaktivieren
sudo a2ensite example.com
sudo a2dissite 000-default

Apache Virtual Host Konfiguration

hljs bash
# Konfigurationsdatei erstellen
sudo nano /etc/apache2/sites-available/example.com.conf
hljs apache
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/html

    <Directory /var/www/example.com/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
hljs bash
# Web-Verzeichnis erstellen
sudo mkdir -p /var/www/example.com/html
sudo chown -R www-data:www-data /var/www/example.com

# Site aktivieren
sudo a2ensite example.com.conf

# Testen und neu laden
sudo apachectl configtest && sudo systemctl reload apache2

SSL/TLS Konfiguration (Let's Encrypt)

Verwenden Sie Certbot für ein kostenloses SSL-Zertifikat:

hljs bash
# Certbot installieren
sudo apt install certbot -y

# Für Nginx
sudo apt install python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com

# Für Apache
sudo apt install python3-certbot-apache -y
sudo certbot --apache -d example.com -d www.example.com

# Automatische Erneuerung testen
sudo certbot renew --dry-run

Manuelle SSL-Konfiguration (Nginx)

hljs nginx
server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    root /var/www/example.com/html;
    index index.html;
}

# HTTP zu HTTPS umleiten
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}

Firewall-Einstellungen

hljs bash
# HTTP und HTTPS mit ufw erlauben
sudo ufw allow 'Nginx Full'
# oder
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# Für Apache
sudo ufw allow 'Apache Full'

Vergessen Sie auf REXE-Servern nicht, HTTP (80) und HTTPS (443) Ports auch in der Path Panel Firewall zu öffnen.

Fazit

Nginx und Apache sind leistungsstarke Webserver für unterschiedliche Anwendungsfälle. Nginx ist ideal für hochleistungsfähige statische Inhalte und Reverse Proxy, während Apache für PHP-Anwendungen und .htaccess-basierte Konfigurationen bevorzugt wird. Mit Virtual Host-Konfiguration auf beiden Servern können mehrere Domains auf demselben Server gehostet werden.