NGINX Web Server
In diesem Tutorial lernst du, wie du den NGINX Web Server auf einem Linux-System installierst, Server Blocks konfigurierst und deine erste Website damit hostest.
In this tutorial you will learn how to install NGINX on a Linux system, configure server blocks, and host your first website.
NGINX installieren
Install NGINX
Aktualisiere die Paketliste und installiere NGINX:
Update the package list and install NGINX:
sudo apt update
sudo apt install nginx -yNGINX starten und aktivieren
Start and enable NGINX
Starte den NGINX-Dienst und aktiviere ihn für den automatischen Start beim Booten:
Start the NGINX service and enable it for automatic startup on boot:
sudo systemctl start nginx
sudo systemctl enable nginxInstallation überprüfen
Verify the installation
Prüfe den Status von NGINX und teste die Standardseite:
Check the NGINX status and test the default page:
sudo systemctl status nginx
curl localhostDu solltest die NGINX-Willkommensseite sehen. Du kannst auch http://localhost oder http://DEINE-IP im Browser aufrufen.
You should see the NGINX welcome page. You can also visit http://localhost or http://YOUR-IP in your browser.
Verzeichnisstruktur verstehen
Understand the directory structure
NGINX verwendet zwei wichtige Verzeichnisse für die Konfiguration von Websites:
NGINX uses two important directories for website configuration:
/etc/nginx/sites-available/ – Hier werden alle Konfigurationsdateien (Server Blocks) gespeichert.
/etc/nginx/sites-available/ – All configuration files (server blocks) are stored here.
/etc/nginx/sites-enabled/ – Hier befinden sich Symlinks zu den aktiven Konfigurationen.
/etc/nginx/sites-enabled/ – Symlinks to active configurations are placed here.
Das Standard-Webverzeichnis ist /var/www/html.
The default web directory is /var/www/html.
ls -la /etc/nginx/sites-available/
ls -la /etc/nginx/sites-enabled/Server Block erstellen
Create a server block
Erstelle ein Verzeichnis für deine Website und eine Beispiel-HTML-Datei:
Create a directory for your website and a sample HTML file:
sudo mkdir -p /var/www/meine-seite
sudo chown -R $USER:$USER /var/www/meine-seite
echo '<h1>Meine NGINX Website läuft!</h1>' > /var/www/meine-seite/index.htmlErstelle nun eine Server Block Konfigurationsdatei:
Now create a server block configuration file:
sudo nano /etc/nginx/sites-available/meine-seiteFüge folgenden Inhalt ein:
Paste the following content:
server {
listen 80;
listen [::]:80;
server_name meine-seite.local;
root /var/www/meine-seite;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/meine-seite.access.log;
error_log /var/log/nginx/meine-seite.error.log;
}Seite aktivieren mit Symlink
Enable the site with a symlink
Aktiviere den Server Block, indem du einen symbolischen Link von sites-available nach sites-enabled erstellst:
Enable the server block by creating a symbolic link from sites-available to sites-enabled:
sudo ln -s /etc/nginx/sites-available/meine-seite /etc/nginx/sites-enabled/Optional: Entferne den Standard-Server-Block, damit er nicht mit deiner Seite kollidiert:
Optional: Remove the default server block so it doesn't conflict with your site:
sudo rm /etc/nginx/sites-enabled/defaultKonfiguration testen mit nginx -t
Test configuration with nginx -t
Bevor du NGINX neu lädst, solltest du immer die Konfiguration auf Fehler prüfen:
Before reloading NGINX, you should always check the configuration for errors:
sudo nginx -tDie Ausgabe sollte syntax is ok und test is successful enthalten. Falls Fehler auftreten, überprüfe deine Konfigurationsdatei auf Tippfehler.
The output should contain syntax is ok and test is successful. If errors occur, check your configuration file for typos.
NGINX neu laden
Reload NGINX
Lade NGINX neu, um die neue Konfiguration zu übernehmen:
Reload NGINX to apply the new configuration:
sudo systemctl reload nginxDer Unterschied zwischen reload und restart: Bei reload werden die Konfigurationsdateien neu geladen, ohne den Dienst zu stoppen – aktive Verbindungen bleiben bestehen. Bei restart wird der gesamte Dienst neu gestartet.
The difference between reload and restart: With reload, configuration files are reloaded without stopping the service – active connections are preserved. With restart, the entire service is restarted.
🎉 Fertig! Dein NGINX Webserver ist jetzt eingerichtet. Öffne http://DEINE-IP im Browser, um deine Website zu sehen.
🎉 Done! Your NGINX web server is now set up. Open http://YOUR-IP in your browser to see your website.