Logo
Card image cap
>subDomain.md
Toni Sun

Subdomains für egosanto.de auf VServer Debian11-bullseye, NginX, MySQL, PHP7.1, PHP-7.4, PHP-8.1, Certbot


@authors: René Fürst und Toni Sun
@Datum: 25.07.2022


www.egosanto.de

dev icon dev.egosanto.de

jup icon jup.egosanto.de

java icon java.egosanto.de

org icon org.egosanto.de

app icon app.egosanto.de

dbs icon dbs.egosanto.de

lemp icon lemp.egosanto.de

  • Einlogen

  • Domains->Domain auswählen(egosanto.de)->DNS Domains

  • Host = 'dev'

  • Type = A

  • Destination \<Your VServer IP>

  • dev.egosanto.de

  • DNS Records speichern

  • OK

1. Installiere Image "Debian 11 bullseye" auf VServer''

  • root Passwörter notieren.
  • Mit Putty unter root auf VServer anmelden.

  • OS aktualisieren

apt update && apt upgrade -y


  • Nötige Packete instalieren
apt-get install software-properties-common dirmngr ca-certificates apt-transport-https nano wget curl -y



2. Installieren von NginX mit LEMP-Stack

  • Option 1: Mainline-Repository importieren:
curl -sSL https://packages.sury.org/nginx-mainline/README.txt | sudo bash -x

oder

  • Option 2: Stabiles Repository importieren:
curl -sSL https://packages.sury.org/nginx/README.txt | sudo bash -x


  • Aktualisiere dein Repository, um die neue Änderung widerzuspiegeln:
apt update


  • Nachdem die Nginx-Repository installiert und aktiviert wurde, installieren wir Nginx mit dem folgenden:
apt install nginx-core nginx-common nginx nginx-full


  • Überprüfe ob die installierte Version die neueste ist
apt-cache policy nginx


  • Brotli-Support installieren
nano /etc/nginx/nginx.conf


  • Fügen nun die zusätzlichen Zeilen im HTTP{} Abschnitt hinzu:
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types application/atom+xml application/javascript application/json application/rss+xml
application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype
application/x-font-ttf application/x-javascript application/xhtml+xml application/xml
font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon
image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;


  • Teste als Nächstes, ob die Änderungen korrekt funktionieren, bevor du sie live schaltest:
nginx -t


  • Ist die Konfiguration in Ordnung, können wir NGINX nun neustarten
systemctl restart nginx
  • oder so
nginx -s reload


  • Aktivieren von NGINX beim Systemstart
systemctl enable nginx


  • Überprüfen des NGINX Status
systemctl status nginx



3. Installation von MySQL / MariaDB-Server

  • Option 1. MariaDB 10.6-Repository importieren:
curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash -s -- --mariadb-server-version=10.6 --skip-maxscale --skip-tools
  • oder

  • Option 2. MariaDB 10.7-Repository importieren:

curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash -s -- --mariadb-server-version=10.7 --skip-maxscale --skip-tools


  • Repository Listen Updaten
apt update


  • MariaDB installieren
apt-get install -y mariadb-server mariadb-client


  • Überprüfen der Version
mariadb --version


  • MariaDB Server Status überprüfen
systemctl status mariadb


  • MariaDB Server Starten
systemctl start mariadb


  • MariaDB Server Stoppen
systemctl stop mariadb


  • MariaDB Server beim Systemstart automatisch starten
systemctl enable mariadb


  • MariaDB Server neustarten
systemctl restart mariadb


MariaDB hat noch kein root PASSWORT

  • Die installierst du mit
mysql_secure_installation


  • Test mit
mysql -uroot -p
  • passwort eingeben
mysql> show databases;
...
exit



4. PHP Installation

  • Ondřej Surý PHP Repository importieren
curl -sSL https://packages.sury.org/php/README.txt | sudo bash -x


Installieren von PHP 7.4, 8.0 und 8.1 FPM

  • So installierst du PHP-FPM 7.4
apt install php7.4 php7.4-fpm php7.4-cli php7.4-curl php7.4-zip php7.4-mysql -y


  • So installierst du PHP-FPM 8.0
apt install php8.0 php8.0-fpm php8.0-cli php8.0-curl php8.0-zip php8.0-mysql -y


  • So installierst du PHP-FPM 8.1
apt install php8.1 php8.1-fpm php8.1-cli php8.1-curl php8.1-zip php8.1-mysql -y


Nach der Installation wird der PHP-FPM-Dienst automatisch gestartet und du kannst den Status überprüfen, um sicherzustellen, dass er in Ordnung ist.

systemctl status php7.4-fpm


systemctl status php8.0-fpm


systemctl status php8.1-fpm


  • Überprüfe welche PHP Version aktiv ist
php --version


  • PHP Testseite
nano /var/www/html/info.php


  • Inhalt der Datei:
<?php
  phpinfo();
?>


  • Datei wieder löschen
rm -f /var/www/html/info.php



5. Erstelle einen Nginx-Serverblock

  • Erstellen der Verzeichnisse falls die nicht da sind
mkdir /etc/nginx/sites-available && mkdir /etc/nginx/sites-enabled


  • Bearbeite die Nginx-Konfigurationsdatei
nano /etc/nginx/nginx.conf


  • Füge dann im HTTP-Abschnitt der (nginx.conf) Konfigurationsdatei:
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;


  • Konfigurationsdateien für jedes Domain, Subdomain erstellen und da drin je ein Serverblock erstellen.
nano /etc/nginx/sites-available/dev.egosanto.de.conf
nano /etc/nginx/sites-available/lemp.egosanto.de.conf
nano /etc/nginx/sites-available/org.egosanto.de.conf
nano /etc/nginx/sites-available/www.egosanto.de.conf
nano /etc/nginx/sites-available/app.egosanto.de.conf
nano /etc/nginx/sites-available/jup.egosanto.de.conf
nano /etc/nginx/sites-available/java.egosanto.de.conf
nano /etc/nginx/sites-available/dbs.egosanto.de.conf


  • Die Konfigurationsdatei mit einem Serverblock
    • ein funktionierenden Beispiel mit aktiviertem PHP-FPM einrichten.
server {
  listen 80;
  listen [::]:80;
  server_name dev.egosanto.de www.dev.egosanto.de;
  root /var/www/dev.egosanto.de/;
  index index.php index.html index.htm;

  location / {
    try_files $uri $uri/ /index.php;
  }

  location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
  }

  #Along browser cache lifetime can speed up repeat visits to your page
  location ~* .(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
    access_log off;
    log_not_found off;
    expires 360d;
  }

  #disable access to hidden files
  location ~ /.ht {
    access_log off;
    log_not_found off;
    deny all;
  }
}


  • OMG jeder schreibt etwas rein? Isch och ! ;-)
server {
  server_name org.egosanto.de www.org.egosanto.de;
  root /var/www/org.egosanto.de/;
  index index.php index.html index.htm;

  location / {
    try_files $uri $uri/ /index.php;
  }

  # MarkDown files parser Parsedown.php -> see https://github.com/erusev/parsedown
  location ~ .md$ {
    include fastcgi.conf;
    fastcgi_param SCRIPT_FILENAME $document_root/vendors/parsedown/parseMD.php;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
  }

  location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
  }

  # A long browser cache lifetime can speed up repeat visits to your page
  location ~* .(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
    access_log off;
    log_not_found off;
    expires 360d;
  }

  # disable access to hidden files
    location ~ /.ht {
    access_log off;
    log_not_found off;
    deny all;
  }

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/org.egosanto.de/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/org.egosanto.de/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


    add_header Strict-Transport-Security "max-age=31536000" always; # managed by Certbot


    ssl_trusted_certificate /etc/letsencrypt/live/org.egosanto.de/chain.pem; # managed by Certbot
    ssl_stapling on; # managed by Certbot
    ssl_stapling_verify on; # managed by Certbot

}

server {
    if ($host = org.egosanto.de) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  listen 80;
  listen [::]:80;
  server_name org.egosanto.de www.org.egosanto.de;
  root /var/www/org.egosanto.de/;
  index index.php index.html index.htm;

  location / {
    try_files $uri $uri/ /index.php;
  }

  location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
  }

  # A long browser cache lifetime can speed up repeat visits to your page
  location ~* .(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
    access_log off;
    log_not_found off;
    expires 360d;
  }

  #disable access to hidden files
    location ~ /.ht {
    access_log off;
    log_not_found off;
    deny all;
  }
}


  • Test-Landingpage erstellen
mkdir -p /var/www/dev.egosanto.de/
mkdir -p /var/www/lemp.egosanto.de/
mkdir -p /var/www/org.egosanto.de/
mkdir -p /var/www/www.egosanto.de/
mkdir -p /var/www/app.egosanto.de/
mkdir -p /var/www/java.egosanto.de/
mkdir -p /var/www/jup.egosanto.de/
mkdir -p /var/www/dbs.egosanto.de/


  • Du musst die Berechtigungen konfigurieren und kannst als aktuell angemeldeter Benutzer mit dem $USER Variable:
chown -R $USER:$USER /var/www/html/example.com/


  • Die letzte Berechtigungseinstellung besteht darin, der Öffentlichkeit das Lesen des Webverzeichnisses (Zugriff auf Ihre Site) mit der Berechtigung chmod 755 zu ermöglichen:
chmod -R 755 /var/www


  • Als Nächstes erstelle die index.html Datei:
nano /var/www/lemp.renefuerst.eu/index.php


Nginx-Serverblock aktivieren

ln -s /etc/nginx/sites-available/www.egosanto.de.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/dev.egosanto.de.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/org.egosanto.de.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/lemp.egosanto.de.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/app.egosanto.de.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/java.egosanto.de.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/jup.egosanto.de.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/dbs.egosanto.de.conf /etc/nginx/sites-enabled/


  • Nginx-Serverblock testen
nginx -t


Öffne als Nächstes den Internetbrowser und gib die Domain-Adresse ein (egosanto.de) um zu testen, ob der Server erreichbar ist.



6. Nginx mit dem kostenlosen SSL-Zertifikat von Let's Encrypt

  • Installiere zuerst das certbot-Paket wie folgt:
apt install python3-certbot-nginx -y
  • Führe nach der Installation den folgenden Befehl aus, um die Erstellung des Zertifikats zu starten:
certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email toni.z@gmx.de -d www.egosanto.de
certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email toni.z@gmx.de -d dev.egosanto.de
certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email toni.z@gmx.de -d lemp.egosanto.de
certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email toni.z@gmx.de -d org.egosanto.de
certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email toni.z@gmx.de -d java.egosanto.de
certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email toni.z@gmx.de -d app.egosanto.de
certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email toni.z@gmx.de -d jup.egosanto.de
certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email toni.z@gmx.de -d dbs.egosanto.de


  • Ein ideales Setup umfasst eine erzwungene HTTPS 301-Weiterleitungen, einen Strict-Transport-Security-Header und OCSP-Stapling. Passe einfach die E-Mail und den Domainnamen an deine Anforderungen an.

  • Jetzt lautet deine URL HTTPS://lemp.renefuerst.eu statt HTTP://lemp.renefuerst.eu.

  • Wenn du das alte HTTP-URL verwendest, wird es automatisch umgeleitet zu HTTPS.

ENDE