# Apache HTTP Server — Webserver konfigurieren und betreiben

> Praxis-Guide zum Apache HTTP Server — Dienststeuerung, configtest, VirtualHosts, Module, TLS und Directives auf Debian/Ubuntu und RHEL.

Source: https://www.jpkc.com/db/cheatsheets/web-servers/apache/

<!-- PROSE:intro -->
Der Apache HTTP Server – oft schlicht httpd genannt – gehört seit Jahrzehnten zu den meistgenutzten Webservern im Internet. Sein modularer Aufbau lässt dich Funktionen wie mod_rewrite, SSL oder Reverse-Proxy gezielt zuschalten, und über VirtualHosts bedienst du beliebig viele Domains aus einer einzigen Instanz. Dieses Cheat-Sheet bündelt die Befehle, die du im Alltag brauchst: Dienststeuerung, Konfigurationstests, Module, VirtualHosts, TLS und Performance-Tuning. Prüfe vor jedem Reload die Syntax mit `apachectl configtest` – und nutze `graceful` statt eines harten Neustarts, um laufende Verbindungen nicht abzuwürgen.
<!-- PROSE:intro:end -->

## Dienststeuerung (systemctl)

`systemctl start apache2` — Startet den Apache-Webserver (Debian/Ubuntu). Auf RHEL/CentOS httpd verwenden.

```bash
systemctl start apache2
```

`systemctl stop apache2` — Stoppt den Apache-Webserver geordnet.

```bash
systemctl stop apache2
```

`systemctl restart apache2` — Startet Apache neu (stoppt und startet). Trennt kurzzeitig alle aktiven Verbindungen.

```bash
systemctl restart apache2
```

`systemctl reload apache2` — Lädt die Apache-Konfiguration neu, ohne aktive Verbindungen zu trennen (graceful).

```bash
systemctl reload apache2
```

`systemctl status apache2` — Zeigt Dienststatus, PID und die letzten Logausgaben von Apache.

```bash
systemctl status apache2
```

`systemctl enable apache2` — Aktiviert den automatischen Start von Apache beim Systemstart.

```bash
systemctl enable apache2
```

`systemctl disable apache2` — Deaktiviert den automatischen Start von Apache beim Booten.

```bash
systemctl disable apache2
```

## apachectl-Kommandos

`apachectl configtest` — Prüft die Apache-Konfiguration auf Syntaxfehler. Gibt „Syntax OK" oder einen Fehler zurück.

```bash
apachectl configtest
```

`apachectl -t` — Kurzform von configtest. Prüft die Konfigurationssyntax.

```bash
apachectl -t
```

`apachectl -t -D DUMP_VHOSTS` — Zeigt eine Übersicht aller konfigurierten VirtualHosts mit ServerName und DocumentRoot.

```bash
apachectl -t -D DUMP_VHOSTS
```

`apachectl -t -D DUMP_MODULES` — Listet alle geladenen Apache-Module auf.

```bash
apachectl -t -D DUMP_MODULES
```

`apachectl -t -D DUMP_RUN_CFG` — Zeigt die aufgelöste Laufzeit-Konfiguration (MPM-Einstellungen, ServerName usw.).

```bash
apachectl -t -D DUMP_RUN_CFG
```

`apachectl -v` — Zeigt die Apache-Version und das Build-Datum.

```bash
apachectl -v
```

`apachectl -V` — Zeigt Version, Build-Flags und einkompilierte Standardwerte von Apache.

```bash
apachectl -V
```

`apachectl graceful` — Lädt die Apache-Konfiguration geordnet neu (ohne Verbindungen zu trennen).

```bash
apachectl graceful
```

`apachectl graceful-stop` — Stoppt Apache geordnet, nachdem die laufenden Requests abgeschlossen sind.

```bash
apachectl graceful-stop
```

## Module (Debian/Ubuntu)

`a2enmod <module>` — Aktiviert ein Apache-Modul. Legt Symlinks in mods-enabled/ an. Erfordert Reload/Restart.

```bash
a2enmod rewrite
```

`a2dismod <module>` — Deaktiviert ein Apache-Modul. Entfernt Symlinks aus mods-enabled/. Erfordert Reload.

```bash
a2dismod status
```

`a2enmod rewrite && systemctl reload apache2` — Aktiviert mod_rewrite und lädt Apache in einem Schritt neu.

```bash
a2enmod rewrite && systemctl reload apache2
```

`a2enmod ssl headers deflate expires` — Aktiviert mehrere Module auf einmal.

```bash
a2enmod ssl headers deflate expires
```

`apache2ctl -M` — Listet alle aktuell geladenen (statischen und dynamischen) Apache-Module auf.

```bash
apache2ctl -M
```

`apache2ctl -M | grep <module>` — Prüft, ob ein bestimmtes Modul aktuell geladen ist.

```bash
apache2ctl -M | grep rewrite
```

## VirtualHosts (Debian/Ubuntu)

`a2ensite <config>` — Aktiviert eine VirtualHost-Konfigurationsdatei aus sites-available/. Legt einen Symlink in sites-enabled/ an.

```bash
a2ensite example.com.conf
```

`a2dissite <config>` — Deaktiviert eine VirtualHost-Konfigurationsdatei. Entfernt den Symlink aus sites-enabled/.

```bash
a2dissite 000-default.conf
```

`a2ensite example.com.conf && systemctl reload apache2` — Aktiviert einen VirtualHost und lädt Apache in einem Schritt neu.

```bash
a2ensite example.com.conf && systemctl reload apache2
```

`ls /etc/apache2/sites-enabled/` — Listet alle aktuell aktivierten VirtualHost-Konfigurationen auf.

```bash
ls /etc/apache2/sites-enabled/
```

`ls /etc/apache2/sites-available/` — Listet alle verfügbaren VirtualHost-Konfigurationsdateien auf.

```bash
ls /etc/apache2/sites-available/
```

`apachectl -t -D DUMP_VHOSTS 2>&1 | grep -A3 'example.com'` — Findet heraus, welche VirtualHost-Konfiguration eine bestimmte Domain bedient.

```bash
apachectl -t -D DUMP_VHOSTS 2>&1 | grep -A3 'example.com'
```

## Logs

`tail -f /var/log/apache2/access.log` — Verfolgt das Apache-Access-Log in Echtzeit (Debian/Ubuntu).

```bash
tail -f /var/log/apache2/access.log
```

`tail -f /var/log/apache2/error.log` — Verfolgt das Apache-Error-Log in Echtzeit.

```bash
tail -f /var/log/apache2/error.log
```

`tail -f /var/log/httpd/access_log` — Verfolgt das Apache-Access-Log auf RHEL/CentOS.

```bash
tail -f /var/log/httpd/access_log
```

`grep 'error\|crit\|alert\|emerg' /var/log/apache2/error.log` — Durchsucht das Error-Log nach Meldungen der Stufen Critical und Error.

```bash
grep 'error\|crit\|alert\|emerg' /var/log/apache2/error.log
```

`apache2ctl status` — Zeigt eine Live-Serverstatus-Seite im Terminal an (erfordert mod_status).

```bash
apache2ctl status
```

`apachectl -e debug -k start` — Startet Apache mit Error-Logging auf Debug-Stufe zur Fehlersuche.

```bash
apachectl -e debug -k start
```

## SSL / TLS

`a2enmod ssl && a2ensite default-ssl.conf && systemctl reload apache2` — Aktiviert das SSL-Modul und den Standard-SSL-VirtualHost (Debian/Ubuntu).

```bash
a2enmod ssl && a2ensite default-ssl.conf && systemctl reload apache2
```

`openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt` — Erzeugt ein selbstsigniertes SSL-Zertifikat mit 365 Tagen Gültigkeit.

```bash
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt
```

`openssl s_client -connect <host>:443 -servername <host>` — Testet eine SSL-Verbindung und prüft die Zertifikatskette.

```bash
openssl s_client -connect example.com:443 -servername example.com
```

`openssl x509 -in server.crt -text -noout` — Zeigt die Details einer SSL-Zertifikatsdatei an (Ablauf, Aussteller, SANs).

```bash
openssl x509 -in server.crt -text -noout
```

`certbot --apache -d <domain>` — Bezieht und installiert automatisch ein Let's-Encrypt-SSL-Zertifikat für Apache.

```bash
certbot --apache -d example.com -d www.example.com
```

`certbot renew --dry-run` — Testet den automatischen Erneuerungsprozess für Let's-Encrypt-Zertifikate.

```bash
certbot renew --dry-run
```

## Konfigurationsverzeichnisse

`/etc/apache2/apache2.conf` — Haupt-Konfigurationsdatei von Apache auf Debian/Ubuntu.

```bash
nano /etc/apache2/apache2.conf
```

`/etc/httpd/conf/httpd.conf` — Haupt-Konfigurationsdatei von Apache auf RHEL/CentOS/Fedora.

```bash
nano /etc/httpd/conf/httpd.conf
```

`/etc/apache2/sites-available/` — Verzeichnis mit allen VirtualHost-Definitionsdateien (Debian/Ubuntu).

```bash
ls /etc/apache2/sites-available/
```

`/etc/apache2/conf-available/` — Verzeichnis für zusätzliche Konfigurations-Snippets (Debian/Ubuntu).

```bash
ls /etc/apache2/conf-available/
```

`/etc/apache2/mods-available/` — Verzeichnis mit allen verfügbaren Modul-Konfigurationsdateien.

```bash
ls /etc/apache2/mods-available/
```

`a2enconf <config> && systemctl reload apache2` — Aktiviert ein Konfigurations-Snippet aus conf-available/.

```bash
a2enconf php8.2-fpm && systemctl reload apache2
```

`a2disconf <config>` — Deaktiviert ein Konfigurations-Snippet aus conf-enabled/.

```bash
a2disconf security
```

## VirtualHost-Konfiguration

Einfacher HTTP-VirtualHost-Block mit ServerName, DocumentRoot und separaten Logdateien.

```bash
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example
    ErrorLog ${APACHE_LOG_DIR}/example-error.log
    CustomLog ${APACHE_LOG_DIR}/example-access.log combined
</VirtualHost>
```

HTTPS-VirtualHost-Block mit SSL-Zertifikatskonfiguration.

```bash
<VirtualHost *:443>
    ServerName example.com
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/example.crt
    SSLCertificateKeyFile /etc/ssl/private/example.key
    DocumentRoot /var/www/html/example
</VirtualHost>
```

Directory-Block, der Zugriff gewährt und .htaccess-Overrides aktiviert (AllowOverride All).

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

Blendet Apache-Version und Betriebssystem aus HTTP-Headern und Fehlerseiten aus (in apache2.conf eintragen).

```bash
ServerTokens Prod
ServerSignature Off
```

Reverse-Proxy: leitet /api/-Requests an eine Backend-App auf Port 3000 weiter (erfordert mod_proxy).

```bash
ProxyPass /api/ http://localhost:3000/
ProxyPassReverse /api/ http://localhost:3000/
```

## Performance & Tuning

`apache2ctl -V | grep 'MPM'` — Prüft, welches MPM (Multi-Processing Module) aktiv ist: prefork, worker oder event.

```bash
apache2ctl -V | grep 'MPM'
```

`a2dismod mpm_prefork && a2enmod mpm_event && systemctl restart apache2` — Wechselt von prefork zum performanten event-MPM (für HTTP/2 erforderlich).

```bash
a2dismod mpm_prefork && a2enmod mpm_event && systemctl restart apache2
```

`a2enmod http2 && systemctl reload apache2` — Aktiviert HTTP/2-Unterstützung (erfordert mpm_event und SSL).

```bash
a2enmod http2 && systemctl reload apache2
```

`ab -n 1000 -c 10 https://example.com/` — Apache Bench: sendet 1000 Requests mit 10 gleichzeitigen Verbindungen, um eine URL zu benchmarken.

```bash
ab -n 1000 -c 10 https://example.com/
```

`ab -n 500 -c 20 -H "Accept-Encoding: gzip" http://example.com/` — Benchmark mit gzip-Encoding-Header, um die Komprimierungs-Performance zu testen.

```bash
ab -n 500 -c 20 -H "Accept-Encoding: gzip" http://example.com/
```

`curl -I -H 'Accept-Encoding: gzip' http://example.com/` — Prüft, ob gzip-Komprimierung (mod_deflate) für eine URL aktiv ist.

```bash
curl -I -H 'Accept-Encoding: gzip' http://example.com/
```

<!-- PROSE:outro -->
## Fazit

Apache bleibt die robuste, breit unterstützte Wahl für klassisches Webhosting – besonders dort, wo `.htaccess`-Overrides, ausgereifte Module und jahrzehntelange Dokumentation zählen. Prüfe Konfigurationsänderungen immer erst mit `apachectl configtest` und rolle sie per `graceful`-Reload aus, dann bleibt dein Server auch im laufenden Betrieb stabil. Wer maximale Performance braucht, kombiniert das event-MPM mit HTTP/2 und mod_deflate.

## Weiterführende Links

- [Apache HTTP Server – offizielle Dokumentation](https://httpd.apache.org/docs/) – Referenz und Handbuch (englisch)
- [Apache HTTP Server – Wikipedia](https://de.wikipedia.org/wiki/Apache_HTTP_Server) – Hintergrund und Geschichte
<!-- PROSE:outro:end -->

## Verwandte Kommandos

- [caddy](https://www.jpkc.com/db/cheatsheets/web-servers/caddy/) – moderner Webserver mit automatischem HTTPS
- [certbot](https://www.jpkc.com/db/cheatsheets/web-servers/certbot/) – Let's-Encrypt-Zertifikate beziehen und erneuern
- [ferron](https://www.jpkc.com/db/cheatsheets/web-servers/ferron/) – schlanker, in Rust geschriebener Webserver

