From f362def9dcdbd9c929a519f0dd1909ce32e3dd08 Mon Sep 17 00:00:00 2001 From: mohamed menacer <mohamed.menacer@etu.univ-amu.fr> Date: Thu, 27 Feb 2025 11:33:17 +0100 Subject: [PATCH] 28 edition --- README.md | 1 - app/Dockerfile | 12 --------- app/index.php | 18 ------------- backend/Dockerfile | 17 ++++++++++++ backend/src/dashboard.php | 30 ++++++++++++++++++++++ backend/src/db.php | 14 ++++++++++ backend/src/index.php | 36 ++++++++++++++++++++++++++ backend/src/logout.php | 6 +++++ backend/src/styles.css | 15 +++++++++++ database/init.sql | 28 ++++++++++++++++++++ docker-compose.yml | 54 ++++++++++++++++++++++++++------------- nginx/Dockerfile | 5 ++++ nginx/default.conf | 17 ++++++++++++ 13 files changed, 204 insertions(+), 49 deletions(-) delete mode 100644 README.md delete mode 100644 app/Dockerfile delete mode 100644 app/index.php create mode 100644 backend/Dockerfile create mode 100755 backend/src/dashboard.php create mode 100755 backend/src/db.php create mode 100755 backend/src/index.php create mode 100755 backend/src/logout.php create mode 100644 backend/src/styles.css create mode 100644 database/init.sql create mode 100644 nginx/Dockerfile create mode 100755 nginx/default.conf diff --git a/README.md b/README.md deleted file mode 100644 index 632e4fe..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -Bonjour diff --git a/app/Dockerfile b/app/Dockerfile deleted file mode 100644 index ec7094c..0000000 --- a/app/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -# Utiliser une image PHP avec Apache -FROM php:8.0-apache - -# Installer l'extension MySQLi -RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli - -# Copier les fichiers du projet -COPY . /var/www/html/ - -# Exposer le port 80 -EXPOSE 80 - diff --git a/app/index.php b/app/index.php deleted file mode 100644 index b0c22f6..0000000 --- a/app/index.php +++ /dev/null @@ -1,18 +0,0 @@ -<?php -$servername = "db"; -$username = "user"; -$password = "password"; -$database = "test_db"; - -// Connexion à MySQL -$conn = new mysqli($servername, $username, $password, $database); - -// Vérifier la connexion -if ($conn->connect_error) { - die("Échec de la connexion : " . $conn->connect_error); -} -echo "Connexion réussie à MySQL ! 🚀"; - -// Fermer la connexion -$conn->close(); -?> diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..6f42717 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,17 @@ +FROM php:7.4-apache + +# Installer les extensions PHP nécessaires +RUN docker-php-ext-install mysqli pdo pdo_mysql + +# Activer les modules Apache +RUN a2enmod rewrite + +# Copier les fichiers du projet +COPY src/ /var/www/html/ + +# Donner les permissions à Apache +RUN chown -R www-data:www-data /var/www/html + +EXPOSE 80 +CMD ["apache2-foreground"] + diff --git a/backend/src/dashboard.php b/backend/src/dashboard.php new file mode 100755 index 0000000..531ac22 --- /dev/null +++ b/backend/src/dashboard.php @@ -0,0 +1,30 @@ +<?php +session_start(); +if (!isset($_SESSION["loggedin"])) { + header("Location: index.php"); + exit; +} + +include "db.php"; +$query = "SELECT name FROM cars"; +$result = $conn->query($query); +?> + +<!DOCTYPE html> +<html> +<head> + <title>Dashboard</title> + <link rel="stylesheet" href="styles.css"> +</head> +<body> + <h2>Bienvenue, Admin !</h2> + <h3>Liste des voitures :</h3> + <ul> + <?php while ($row = $result->fetch_assoc()): ?> + <li><?php echo $row["name"]; ?></li> + <?php endwhile; ?> + </ul> + <a href="logout.php">Déconnexion</a> +</body> +</html> + diff --git a/backend/src/db.php b/backend/src/db.php new file mode 100755 index 0000000..8a07555 --- /dev/null +++ b/backend/src/db.php @@ -0,0 +1,14 @@ +<?php +$host = "db"; +$user = "root"; +$password = "rootpassword"; +$database = "car_database"; + +// Connexion à la base de données +$conn = new mysqli($host, $user, $password, $database); + +// Vérifier la connexion +if ($conn->connect_error) { + die("Erreur de connexion: " . $conn->connect_error); +} +?> diff --git a/backend/src/index.php b/backend/src/index.php new file mode 100755 index 0000000..f220ee1 --- /dev/null +++ b/backend/src/index.php @@ -0,0 +1,36 @@ +<?php +session_start(); +include 'db.php'; // Vérifie qu'il n'affiche rien + +if ($_SERVER["REQUEST_METHOD"] == "POST") { + $username = $_POST['username']; + $password = $_POST['password']; + + if ($username == "admin" && $password == "admin123") { + $_SESSION['loggedin'] = true; + header("Location: dashboard.php"); + exit(); // Toujours ajouter `exit()` après `header()` + } else { + $error = "Identifiants incorrects"; + } +} +?> + +<!DOCTYPE html> +<html lang="fr"> +<head> + <meta charset="UTF-8"> + <title>Connexion</title> +</head> +<body> + <h2>Connexion</h2> + <form method="post"> + <label>Nom d'utilisateur :</label> + <input type="text" name="username" required><br> + <label>Mot de passe :</label> + <input type="password" name="password" required><br> + <button type="submit">Se connecter</button> + </form> + <?php if (isset($error)) echo "<p style='color:red;'>$error</p>"; ?> +</body> +</html> diff --git a/backend/src/logout.php b/backend/src/logout.php new file mode 100755 index 0000000..95697c4 --- /dev/null +++ b/backend/src/logout.php @@ -0,0 +1,6 @@ +<?php +session_start(); +session_destroy(); +header("Location: index.php"); +exit; +?> diff --git a/backend/src/styles.css b/backend/src/styles.css new file mode 100644 index 0000000..bc0a909 --- /dev/null +++ b/backend/src/styles.css @@ -0,0 +1,15 @@ +body { + font-family: Arial, sans-serif; + text-align: center; +} + +form { + margin-top: 50px; +} + +input, button { + display: block; + margin: 10px auto; + padding: 10px; +} + diff --git a/database/init.sql b/database/init.sql new file mode 100644 index 0000000..71214ff --- /dev/null +++ b/database/init.sql @@ -0,0 +1,28 @@ +CREATE DATABASE IF NOT EXISTS car_database; +USE car_database; + +CREATE TABLE users ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(50) NOT NULL, + password VARCHAR(255) NOT NULL +); + +CREATE TABLE cars ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(100) NOT NULL +); + +INSERT INTO users (username, password) VALUES ('admin', MD5('admin123')); + +INSERT INTO cars (name) VALUES +('Toyota Corolla'), +('Honda Civic'), +('Ford Mustang'), +('Chevrolet Camaro'), +('Tesla Model 3'), +('BMW M3'), +('Audi R8'), +('Mercedes-Benz C-Class'), +('Volkswagen Golf'), +('Nissan GT-R'); + diff --git a/docker-compose.yml b/docker-compose.yml index 2ce505f..7281f7b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,30 +1,48 @@ +version: '3.8' + services: - web: - build: ./app # Assure-toi que ce chemin est correct ! - container_name: php_app - image: php:7.4-apache # Utilise une image avec Apache pour exposer un serveur web + backend: + build: ./backend + container_name: backend_app + environment: + - DB_HOST=db + - DB_USER=root + - DB_PASSWORD=rootpassword + - DB_NAME=car_database + volumes: + - ./backend/src:/var/www/html ports: - - "8000:80" # Expose le port 80 du conteneur sur le port 8080 de l'hôte + - "8000:80" depends_on: - - db # Assure-toi que le conteneur `db` est prêt avant de démarrer `web` + - db networks: - - dev_network # Réseau personnalisé + - app_network + + nginx: + build: ./nginx + container_name: nginx + ports: + - "80:80" + volumes: + - ./backend/src:/var/www/html + - ./nginx/default.conf:/etc/nginx/conf.d/default.conf + depends_on: + - backend + networks: + - app_network db: image: mysql:5.7 - container_name: mysql_db_dev - restart: always + container_name: db environment: - MYSQL_ROOT_PASSWORD: root - MYSQL_DATABASE: test_db - MYSQL_USER: user - MYSQL_PASSWORD: password - ports: - - "3306:3306" # Expose le port 3306 pour MySQL + MYSQL_ROOT_PASSWORD: rootpassword + MYSQL_DATABASE: car_database + volumes: + - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql networks: - - dev_network # Réseau personnalisé + - app_network networks: - dev_network: - driver: bridge # Utiliser un réseau `bridge` personnalisé + app_network: + driver: bridge diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..892e541 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,5 @@ +FROM nginx:latest + +# Copier la configuration Nginx +COPY default.conf /etc/nginx/conf.d/default.conf + diff --git a/nginx/default.conf b/nginx/default.conf new file mode 100755 index 0000000..6564045 --- /dev/null +++ b/nginx/default.conf @@ -0,0 +1,17 @@ +server { + listen 80; + + root /var/www/html; + index index.php; + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~ \.php$ { + include fastcgi_params; + fastcgi_pass backend_app:80; + fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; + } +} + -- GitLab