Einfach Anwendung mit Mariadb, Apache und PHP: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Die Seite wurde neu angelegt: „__TOC__ == Einleitung == Dieses Tutorial beschreibt die Einrichtung eines minimalen Monitoring-Tools auf Basis von '''PHP''', '''MySQL''' und '''Apache2'''. D…“ |
Keine Bearbeitungszusammenfassung |
||
| Zeile 2: | Zeile 2: | ||
== Einleitung == | == Einleitung == | ||
Minimales Monitoring-Tool für Debian 13 (Trixie). Prüft IP-Adressen via ICMP (Ping) und speichert die Hosts in einer MariaDB-Datenbank. | |||
== | == System-Setup == | ||
== | ; Apache & PHP | ||
: Installation der Web-Umgebung und des Datenbank-Treibers. | |||
* <code>sudo apt update && sudo apt install apache2 php php-mysqli libapache2-mod-php -y</code> | |||
; Datenbank (MariaDB) | |||
: Installation und Absicherung des Datenbank-Servers. | |||
* <code>sudo apt install mariadb-server -y</code> | |||
* <code>sudo mysql_secure_installation</code> | |||
; PHP-Berechtigungen | |||
: Die Funktion <code>exec()</code> darf nicht in der <code>disable_functions</code> der <code>/etc/php/8.3/apache2/php.ini</code> stehen. | |||
* <code>sudo systemctl restart apache2</code> | |||
== Datenbank-Schema == | |||
Lege die Struktur in der MariaDB-Konsole an: | |||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
CREATE TABLE | CREATE DATABASE monitoring; | ||
USE monitoring; | |||
CREATE TABLE hosts ( | |||
id INT AUTO_INCREMENT PRIMARY KEY, | |||
hostname VARCHAR(255), | |||
ip_address VARCHAR(45), | |||
last_check TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | |||
); | ); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Programmierung (index.php) == | ||
Erstelle die Datei unter <code>/var/www/html/index.php</code>. | |||
<blockquote> | |||
'''Hinweis für Debian 13:''' Der www-data User benötigt oft explizite Rechte für Raw Sockets, um zu pingen: | |||
* <code>sudo setcap cap_net_raw+p /usr/bin/ping</code> | |||
</blockquote> | |||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
<?php | <?php | ||
$db = new mysqli("localhost", "root", "DEIN_PASSWORT", "monitoring"); | |||
$db = new mysqli("localhost", " | |||
echo '<meta http-equiv="refresh" content="30">'; | echo '<meta http-equiv="refresh" content="30">'; | ||
echo "<h1>System Monitor</h1>"; | |||
function | function pingHost($ip) { | ||
// | // Absoluter Pfad ist unter Debian 13 sicherer | ||
exec("ping -c 1 -W 1 " . escapeshellarg($ip), $output, $result); | exec("/usr/bin/ping -c 1 -W 1 " . escapeshellarg($ip), $output, $result); | ||
return ($result === 0) | return ($result === 0); | ||
} | } | ||
$ | $res = $db->query("SELECT * FROM hosts"); | ||
echo "<table border='1'><tr><th>Host</th><th>IP</th><th>Status</th></tr>"; | |||
while($row = $res->fetch_assoc()) { | |||
$is_online = pingHost($row['ip_address']); | |||
$status = $is_online ? "<b style='color:green'>ONLINE</b>" : "<b style='color:red'>OFFLINE</b>"; | |||
echo "<tr><td>{$row['hostname']}</td><td>{$row['ip_address']}</td><td>$status</td></tr>"; | |||
} | |||
echo "</table>"; | |||
?> | ?> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Sicherheit & Performance == | ||
* '''Validierung:''' IPs immer mit <code>filter_var($ip, FILTER_VALIDATE_IP)</code> prüfen. | |||
* '''Skalierung:''' Bei mehr als 10 Hosts sollte der Ping per '''Cronjob''' in die Datenbank schreiben, statt beim Laden der Seite zu pingen. | |||
* ''' | |||
* ''' | |||
[[Kategorie: | [[Kategorie:Admin-Dokumentation]] | ||
[[Kategorie: | [[Kategorie:Debian 13]] | ||
Aktuelle Version vom 16. März 2026, 18:31 Uhr
Einleitung
Minimales Monitoring-Tool für Debian 13 (Trixie). Prüft IP-Adressen via ICMP (Ping) und speichert die Hosts in einer MariaDB-Datenbank.
System-Setup
- Apache & PHP
- Installation der Web-Umgebung und des Datenbank-Treibers.
sudo apt update && sudo apt install apache2 php php-mysqli libapache2-mod-php -y
- Datenbank (MariaDB)
- Installation und Absicherung des Datenbank-Servers.
sudo apt install mariadb-server -ysudo mysql_secure_installation
- PHP-Berechtigungen
- Die Funktion
exec()darf nicht in derdisable_functionsder/etc/php/8.3/apache2/php.inistehen.
sudo systemctl restart apache2
Datenbank-Schema
Lege die Struktur in der MariaDB-Konsole an:
CREATE DATABASE monitoring;
USE monitoring;
CREATE TABLE hosts (
id INT AUTO_INCREMENT PRIMARY KEY,
hostname VARCHAR(255),
ip_address VARCHAR(45),
last_check TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Programmierung (index.php)
Erstelle die Datei unter /var/www/html/index.php.
Hinweis für Debian 13: Der www-data User benötigt oft explizite Rechte für Raw Sockets, um zu pingen:
sudo setcap cap_net_raw+p /usr/bin/ping
<?php
$db = new mysqli("localhost", "root", "DEIN_PASSWORT", "monitoring");
echo '<meta http-equiv="refresh" content="30">';
echo "<h1>System Monitor</h1>";
function pingHost($ip) {
// Absoluter Pfad ist unter Debian 13 sicherer
exec("/usr/bin/ping -c 1 -W 1 " . escapeshellarg($ip), $output, $result);
return ($result === 0);
}
$res = $db->query("SELECT * FROM hosts");
echo "<table border='1'><tr><th>Host</th><th>IP</th><th>Status</th></tr>";
while($row = $res->fetch_assoc()) {
$is_online = pingHost($row['ip_address']);
$status = $is_online ? "<b style='color:green'>ONLINE</b>" : "<b style='color:red'>OFFLINE</b>";
echo "<tr><td>{$row['hostname']}</td><td>{$row['ip_address']}</td><td>$status</td></tr>";
}
echo "</table>";
?>
Sicherheit & Performance
- Validierung: IPs immer mit
filter_var($ip, FILTER_VALIDATE_IP)prüfen. - Skalierung: Bei mehr als 10 Hosts sollte der Ping per Cronjob in die Datenbank schreiben, statt beim Laden der Seite zu pingen.