Project 3 ‐ Web Application and Database Redundancy - benjigifford/SEC-440 GitHub Wiki

Web Application and Database Redundancy

Part 1: Database Redundancy

u1,2,3 Setup

sudo hostnamectl set-hostname db0x-benji
  • Connect each box to LAN network adapter
  • Set hostnames to db01-benji, db02-benji, & db03-benji, respectively

Networking for new db Boxes

  • On each db0x Box-
sudo nano /etc/netplan/00-installer-config.yaml
sudo netplan apply

image

Scheme:

  • db01-benji - 10.0.5.201

  • db02-benji - 10.0.5.202

  • db03-benji - 10.0.5.203

  • On each box:

sudo apt clean
sudo apt install mariadb-server
sudo apt install galera-4
sudo nano /etc/mysql/mariadb.conf.d/60-galera.cnf

image

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
  • Comment out #bind-address = 127.0.0.1
  • Exit and save

image

  • Once galera cluster is working, on u1 run mariadb-secure-installation to secure your database and set a root password (can allow root remote logins). It should synchronize across u2 and u3. Test by accessing mysql sudo mysql -u root -p with your new password on each server

image

  • Stop MariaDB on all 3 boxes to make new cluster
systemctl stop mariadb
  • On u1:
sudo galera_new_cluster
sudo systemctl status mariadb
  • On u2 and u3:
sudo systemctl start mariadb

image

Configure haproxy on ha1, ha2 to be the SQL load balancer for u1, u2, u3

  • On ha1&2:
sudo nano /etc/haproxy/haproxy.cfg
  • Add this on to the end

image

On both ha1&2

sudo systemctl restart haproxy

Part 2: Tiered Web Application

Step 1: Prep web servers (web01/02) with PHP with MySQL support

  • On web01&02:
setsebool -P httpd_can_network_connect_db on
sudo yum install php php-mysql
sudo systemctl restart httpd.service
sudo mysql_secure_installation
  • Create a new PHP file under the /var/www/html directory (on 1 of the 2 boxes):
sudo nano /var/www/html/info.php
  • When the file opens, type in the following code:
<?php
phpinfo();
    ?>

image

To verify it worked, type this URL in your browser on xubuntu-wan: http://10.0.5.100/info.php

Step 2: Create a MySQL/Maria DB

I made a shark database for this project.

On u1:

sudo mysql
CREATE DATABASE shark_species;
USE shark_species;

CREATE TABLE sharks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    species VARCHAR(100) NOT NULL,
    scientific_name VARCHAR(255) NOT NULL,
    average_size VARCHAR(50),
    habitat VARCHAR(100),
    description TEXT
);

Insert data

INSERT INTO sharks (species, scientific_name, average_size, habitat, description)
VALUES
    ('Great White Shark', 'Carcharodon carcharias', 'Up to 6 meters', 'Oceans worldwide', 'The great white shark is notable for its size, with mature individuals growing up to 6.4 m in length and 3,324 kg in weight.'),
    ('Hammerhead Shark', 'Sphyrnidae', 'Up to 6 meters', 'Tropical and warm temperate waters worldwide', 'Hammerhead sharks are known for their distinctive, flattened heads, which are shaped like a double-sided mallet, called a cephalofoil.'),
    ('Whale Shark', 'Rhincodon typus', 'Up to 18 meters', 'Tropical and warm oceans', 'The whale shark is the largest known extant fish species, reaching up to 12.65 m (41.5 ft) in length. Despite its size, the whale shark feeds only on plankton and small fish through filter feeding.');

Step 3: PHP and MySQL Integration

On both Web01 and Web02, create a PHP script to interact with the MySQL database and display the shark species information.

<?php
$servername = "10.0.5.201"; 
$username = "benji";
$password = "password";
$dbname = "shark_species";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Fetch data from the database
$sql = "SELECT * FROM sharks";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Species: " . $row["species"]. "<br>";
        echo "Scientific Name: " . $row["scientific_name"]. "<br>";
        echo "Average Size: " . $row["average_size"]. "<br>";
        echo "Habitat: " . $row["habitat"]. "<br>";
        echo "Description: " . $row["description"]. "<br><br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

  • Name the script 'sharks.php' and place it in '/var/www/html/'
  • On u1:
GRANT ALL PRIVILEGES ON *.* TO 'benji'@'10.0.5.100' IDENTIFIED BY 'password' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'benji'@'10.0.5.101' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;

image

Make it multi-layered:

  • On web01 and web02, replace index.html with this:
<form action="search.php" method="post">
    <label for="species">Select a shark species:</label>
    <select name="species" id="species">
        <option value="Great White Shark">Great White Shark</option>
        <option value="Hammerhead Shark">Hammerhead Shark</option>
        <option value="Whale Shark">Whale Shark</option>
    </select>
    <button type="submit">Search</button>
</form>

Write /var/www/html/search.php:

<?php
// Connect to the MySQL database
$servername = "localhost"; // Replace with your MySQL server hostname or IP address
$username = "your_mysql_username";
$password = "your_mysql_password";
$dbname = "shark_species"; // Replace with your database name
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get the selected shark species from the form
$species = $_POST['species'];

// Prepare and execute the SQL query
$stmt = $conn->prepare("SELECT * FROM sharks WHERE species = ?");
$stmt->bind_param("s", $species);
$stmt->execute();
$result = $stmt->get_result();

// Display the search results
if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    echo "<h2>" . $row["species"] . "</h2>";
    echo "<p><strong>Scientific Name:</strong> " . $row["scientific_name"] . "</p>";
    echo "<p><strong>Average Size:</strong> " . $row["average_size"] . "</p>";
    echo "<p><strong>Habitat:</strong> " . $row["habitat"] . "</p>";
    echo "<p><strong>Description:</strong> " . $row["description"] . "</p>";
} else {
    echo "No results found for the selected species.";
}

// Close the database connection
$conn->close();
?>

I couldn't get the above stuff to work, so I resorted to just making a simple button that displays the info when you press it.

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Shark Information</title>
</head>
<body>
    <h1>Welcome to Sharkipedia</h1>
    <p>Click the button below to get shark information:</p>
    <form action="sharks.php">
        <button type="submit">Click here to get shark info</button>
    </form>
</body>
</html>

image

image

⚠️ **GitHub.com Fallback** ⚠️