SV01 - Serveur web : Apache, PHP, MariaDB (pile LAMP)
Objectif
Installer un serveur web Apache avec PHP, héberger plusieurs sites (VirtualHosts), lire les journaux, passer en HTTPS, et savoir où se trouve quoi sur Debian et sur Arch.
1. Comment fonctionne une requête web
sequenceDiagram participant N as Navigateur participant A as Apache :80/:443 participant P as PHP participant M as MariaDB :3306 N->>A: GET /index.php (Host: monsite.local) A->>A: Choisit le VirtualHost selon le nom (Host) A->>P: Exécute index.php P->>M: SELECT … (identifiants de wp-config.php) M-->>P: Lignes de résultat P-->>A: Page HTML générée A-->>N: HTTP 200 + HTML
LAMP = Linux + Apache + MariaDB/MySQL + PHP. Variante LEMP avec Nginx (« Engine-X »).
2. Installation
Script fourni : scripts/web/install_lamp.sh (Debian/Ubuntu).
sudo apt install apache2 mariadb-server php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-zip php-intl
sudo systemctl enable --now apache2 mariadb
curl -I http://localhost # doit répondre "HTTP/1.1 200 OK"| Debian / Ubuntu | Arch | |
|---|---|---|
| Paquet / service | apache2 | apache → service httpd |
| Config principale | /etc/apache2/apache2.conf | /etc/httpd/conf/httpd.conf |
| Sites | /etc/apache2/sites-available/ + a2ensite | Include dans httpd.conf |
| Racine web par défaut | /var/www/html | /srv/http |
| Journaux | /var/log/apache2/ | /var/log/httpd/ |
| Utilisateur du serveur | www-data | http |
| Ports | /etc/apache2/ports.conf | Listen dans httpd.conf |
Commandes Debian spécifiques
| Commande | Effet |
|---|---|
sudo a2ensite monsite.conf / a2dissite | Activer / désactiver un site |
sudo a2enmod rewrite ssl headers / a2dismod | Activer / désactiver un module |
sudo apache2ctl configtest | Tester la configuration (« Syntax OK ») |
sudo apache2ctl -S | Lister les VirtualHosts actifs |
sudo systemctl reload apache2 | Recharger sans couper |
3. Un VirtualHost (un site par nom de domaine)
sudo mkdir -p /var/www/intranet
echo "<h1>Intranet SIO</h1>" | sudo tee /var/www/intranet/index.html
sudo chown -R www-data:www-data /var/www/intranet
sudo nano /etc/apache2/sites-available/intranet.conf<VirtualHost *:80>
ServerName intranet.sio.lan
ServerAlias www.intranet.sio.lan
DocumentRoot /var/www/intranet
<Directory /var/www/intranet>
# Pas de listing du contenu du dossier
Options -Indexes
# Autorise les fichiers .htaccess (WordPress, GLPI)
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/intranet_error.log
CustomLog ${APACHE_LOG_DIR}/intranet_access.log combined
</VirtualHost>sudo a2ensite intranet.conf
sudo apache2ctl configtest && sudo systemctl reload apache2
# Sur le poste client (sans DNS) :
echo "192.168.56.20 intranet.sio.lan" | sudo tee -a /etc/hosts
curl http://intranet.sio.lanChanger le port (cas du projet transversal : Apache écoutait sur 8080)
sudo sed -i 's/Listen 8080/Listen 80/' /etc/apache2/ports.conf
sudo sed -i 's/:8080>/:80>/' /etc/apache2/sites-enabled/000-default.conf
sudo systemctl restart apache2
sudo ss -tlnp | grep apache4. HTTPS
Certificat auto-signé (labo)
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/ssl/private/intranet.key -out /etc/ssl/certs/intranet.crt \
-subj "/CN=intranet.sio.lan"
sudo a2enmod ssl<VirtualHost *:443>
ServerName intranet.sio.lan
DocumentRoot /var/www/intranet
SSLEngine on
SSLCertificateFile /etc/ssl/certs/intranet.crt
SSLCertificateKeyFile /etc/ssl/private/intranet.key
</VirtualHost>
<VirtualHost *:80>
ServerName intranet.sio.lan
Redirect permanent / https://intranet.sio.lan/
</VirtualHost>Le navigateur affichera un avertissement (certificat non signé par une autorité reconnue) - normal en labo.
Let’s Encrypt (serveur public avec un vrai nom de domaine)
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d monsite.fr -d www.monsite.fr # renouvellement automatique installé5. Codes HTTP à connaître
| Code | Sens | Cause fréquente |
|---|---|---|
| 200 | OK | |
| 301 / 302 | Redirection | HTTP → HTTPS |
| 403 | Interdit | Droits des fichiers, Require, proxy qui bloque |
| 404 | Introuvable | Mauvais DocumentRoot, .htaccess/rewrite absent |
| 500 | Erreur serveur | Erreur PHP → lire error.log |
| 502 / 503 | Mauvaise passerelle / indisponible | Backend arrêté derrière un reverse proxy (HAProxy) |
sudo tail -f /var/log/apache2/error.log # LE réflexe en cas de page blanche ou d'erreur 5006. PHP
php -v # version
php -m # modules chargés (mysqli, pdo_mysql, intl…)
echo "<?php phpinfo();" | sudo tee /var/www/html/info.php # test, puis supprimer !Réglages dans /etc/php/8.x/apache2/php.ini : upload_max_filesize, post_max_size, memory_limit, date.timezone = Europe/Paris.
7. Et XAMPP ?
XAMPP (Apache + MariaDB + PHP + phpMyAdmin dans un seul installeur, dans /opt/lampp) est pratique pour un test rapide, mais la version Linux n’est plus mise à jour (bloquée en 8.2.12, fin 2023). Pour apprendre l’administration système, installe les vrais paquets (ci-dessus) ou utilise Docker (V05 - Docker). Commandes si besoin : sudo /opt/lampp/lampp start | stop | status. Ne jamais l’exposer sur un réseau.