Apache: zweite Domain mit eigenem document-root

Ziel dieses Tutorial ist eine weiter Domain oder Subdomain (dev.example.com) mit eigenem DocumentRoot dem Webserver Apache hinzufügen.

01. Backup erstellen
Mach ein Backup von /etc/apache2

# cp -r /etc/apache2 /var/backups/apache2_original

Falls nachträglich etwas schief geht, kannst du das Backup unter /var/backups/apache2_original jederzeit wieder einspielen!

02. DocumentRoot erstellen
Normalerweise befindet sich DocumentRoot auf /var/www/html falls keine eigenen Regeln vergeben wurden. Wir erstellen nun einen Ordner unter /var/www/html, welcher auf die neue Webseite zeigt:

# mkdir dev.example.com

03. Seiten unter /etc/apache2 definieren
Unter /etc/apache2/sites-available erstellen wir nun zwei weitere Dokumente, welche auf die neue Domain oder Subdomain verweisen. Eine Datei verweist dabei auf VirtualHost *:80 und eine auf VirtualHost *:443. Aktiviere nach dem Erstellen der beiden Dateien die virtuellen Hosts mit a2ensite dev.example.com. Dieser Befehl macht nicht anderes, als die beiden Dateien nach /etc/apache2/sites-enabled zu kopieren. Dies kann auch manuell erledigt werden. Am Ende den Webserver noch neu starten:

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/dev.example.com

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

RewriteEngine on
 RewriteCond %{SERVER_NAME} = dev.example.com
 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/dev.example.com

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

ServerName dev.example.com
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/dev.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/dev.example.com/privkey.pem

</VirtualHost>
<IfModule>
Dieses File benötigen wir einmal für den VirtualHost:443 und einmal für VirtualHost:80 unter sites-available und sites-enabled – also insgesamt vier Mal. Es reicht jedoch, die Dokumente unter sites-available zu erstellen und danach mittels cp nach sites-enabled zu kopieren.

04. example.com und dev.example.com haben nun separate DocumenRoot

hier am Beispiel mit meiner Testdomain jirah.ch und dev.jirah.ch. Falls du ein SSL-Zertifikat benötigst, müssen die DNS Einträge auf den Server (IP) zeigen.

Let’s encrypt (certbot) um eine Domain erweitern

Let’s encrypt bietet kostenlose Zertifikate zur Verschlüsselung an. In diesem Beispiel erweitern wir unser bestehendes Zertifikat um die Domain newexample.com. Das Vorgehen für eine Subdomain ist dasselbe. Mittels:

# certbot certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
  Certificate Name: example.com
    Serial Number: 1234567890acb1234567890abc1234567890
    Key Type: ECDSA
    Domains: example.com test.example.com example2.com example3.com
    Expiry Date: 2023-07-28 17:09:45+00:00 (VALID: 87 days)
    Certificate Path: /etc/letsencrypt/live/nyx7.ch/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/nyx7.ch/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

wird uns der Zertifikatsname mit allen zugehörenden Domains angezeigt. Möchten wir nun das bestehende Zertikikat um eine Domain oder Subdomain (newexample.com) erweitern, müssen wir das Zertifikat example.com für alle Domains und Subdomains erweitern.

Die Erweiterung des Zertifikates per expand:

# certbot --expand -d example.com,test.example.com,example2.com,example3.com,newexample.com
# certbot --expand -d example.com,test.example.com,example2.com,example3.com,newexample.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Renewing an existing certificate for example.com and 4 more domains

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/example.com/privkey.pem
This certificate expires on 2023-07-30.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for example.com to /etc/apache2/sites-enabled/000-default-le-ssl.conf
Successfully deployed certificate for test.example.com to /etc/apache2/sites-enabled/000-default-le-ssl.conf
Successfully deployed certificate for example2.com to /etc/apache2/sites-enabled/000-default-le-ssl.conf
Successfully deployed certificate for example3.com to /etc/apache2/sites-enabled/000-default-le-ssl.conf

We were unable to find a vhost with a ServerName or Address of newexample.com.
Which virtual host would you like to choose?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: 000-default-le-ssl.conf        | Multiple Names        | HTTPS | Enabled
2: 000-default.conf               |                       |       | Enabled
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1
Successfully deployed certificate for newexample.com to /etc/apache2/sites-enabled/000-default-le-ssl.conf
Your existing certificate has been successfully renewed, and the new certificate has been installed.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Um jedoch ein neues Zertifikat für die Domain zu bekommen, tippen wir:

# certbot certonly -d newexample.com

ein. Mehrere Domain und Subdomain werden durch ein Komma getrennt.

Wildcard Zertifikat

Um ein Wildecard zu bekommen, geben wir:

certbot certonly --manual --preferred-challenges=dns --email mail@example.com --server https://acme-v02.api.letsencrypt.org/directory --agree-tos -d example.com -d *.example.com

ein. Danach fordert uns die Installationsroutine auf, einen TXT Eintrag in der Form:

_acme-challenge.example.com   TXT  uDCIo-hQfzzIAl0N--H1-F75WF92yt14OKNc-JT6Q_U

zu machen. Erst nach diesem Eintrag kann das Zertifikat bezogen werden.

Falls bei dir Apache läuft, ist der virtuelle Host noch zu ergänzen.

(...)
ServerName example.com
ServerAlias *.example.com
(...)

gesamte Domain weiterleiten per .htaccess

Wer seine gesamte Webseite umgezogen hat, setzt per .htaccess ein Weiterleitung von der alten Domain (example1.com) zur neuen Domain (example2.com)
Damit werden sämtliche Anfrage von der alten Domain auf die neue Domain permanent umgeleitet:

# Weiterleitung von example1.com nach example2.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^https://example1.com [NC]
RewriteRule ^(.*)$ https://example2.com/$1 [R=301,L]
</IfModule>

nachfolgende Weiterleitung behält alle indexierten URLs bei und leitet alles auf die neue Webseite example.com weiter. Hat eine Suchmaschine somit https://mydomain.com indexiert, zeigt diese Weiterleitung automatisch dann auf https://example.com und weist die Suchmaschine über die permanente Weiterleitung mittels 301 hin:

<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://example.com/$1 [R=301,L]
</IfModule>

Webserver (mit XAMPP)

XAMPP ist eine leicht zu installierende Apache-Distribution, die MariaDB, PHP und Perl enthält.
Gehe auf http://www.apachefriends.org/de/download.html

Dieses Tutorial bezieht sich auf die Installation von XAMPP unter Debian GNU/Linux und wurde unter Bullseye getestet.

Fährst Du mit der Maus über eine Prüfsumme (md5 oder sha1), wird dir diese angezeigt. Nach erfolgreichem Download kann die Prüfsumme ausgelesen werden:

$ sha1sum xampp-linux-*-installer.run
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN  xampp-linux-*-installer.run

Ist die Prüfsumme korrekt, kann mit der Installation begonnen werden. Die Datei wird zuerst ausführbar gemacht:

$ chmod 755 xampp-linux-*-installer.run

und anschliessend die Installationsroutine gestartet:

# ./xampp-linux-*-installer.run

Der XAMPP-Installer verlangt root-Rechte. Tippe dazu su – ein:

su -
Passwort: 
./xampp-linux-x64-8.1.1-2-installer.run
----------------------------------------------------------------------------
Welcome to the XAMPP Setup Wizard.

----------------------------------------------------------------------------
Select the components you want to install; clear the components you do not want 
to install. Click Next when you are ready to continue.

XAMPP Core Files : Y (Cannot be edited)

XAMPP Developer Files [Y/n] : Y

Is the selection above correct? [Y/n]: Y

----------------------------------------------------------------------------
Installation Directory

XAMPP will be installed to /opt/lampp
Press [Enter] to continue:

----------------------------------------------------------------------------
Setup is now ready to begin installing XAMPP on your computer.

Do you want to continue? [Y/n]: Y

----------------------------------------------------------------------------
Please wait while Setup installs XAMPP on your computer.

 Installing
 0% ______________ 50% ______________ 100%
 #########################################

----------------------------------------------------------------------------
Setup has finished installing XAMPP on your computer.

XAMPP kann nun via Brower aufgerufen werden. Tippe dazu in die Adresszeile localhost ein.

Die Dienste müssen vorab jedoch gestartet werden. Xampp heisst unter Linux korrekterweise lampp:

# /opt/lampp/lampp start
Starting XAMPP for Linux 8.1.1-2...
XAMPP: Starting Apache.../opt/lampp/share/xampp/xampplib: Zeile 22: netstat: Kommando nicht gefunden.
/opt/lampp/share/xampp/xampplib: Zeile 22: netstat: Kommando nicht gefunden.
ok.
XAMPP: Starting MySQL.../opt/lampp/share/xampp/xampplib: Zeile 22: netstat: Kommando nicht gefunden.
ok.
XAMPP: Starting ProFTPD.../opt/lampp/share/xampp/xampplib: Zeile 22: netstat: Kommando nicht gefunden.
ok.

Die Meldung von netstat kann getrost ignoriert werden oder mit dem Paket net-tools behoben werden.

# apt install net-tools

jetzt startet XAMPP «fehlerfrei»

# /opt/lampp/lampp start
Starting XAMPP for Linux 8.1.1-2...
XAMPP: Starting Apache...ok.
XAMPP: Starting MySQL...ok.
XAMPP: Starting ProFTPD...ok.

Beendet wird XAMPP mit:

# /opt/lampp/lampp stop
Stopping XAMPP for Linux 8.1.1-2...
XAMPP: Stopping Apache...ok.
XAMPP: Stopping MySQL...ok.
XAMPP: Stopping ProFTPD...ok.

>>> HINWEIS <<<
Das Verzeichnis von XAMPP befindet sich unter
/opt/lampp/htdocs
Um dauerhaft Besitzer dieses Verzeichnis zu werden, können die Besitzerrechte mit chown geändert werden. Tippe dazu:

# chown -cR BENUTZER /opt/lampp/htdocs

ein.