Project: Securely Storing Login Information - AWS-RDS

Project: Securely Storing Login Information - AWS-RDS

Steps:

  1. Create EC2-Instance - Myinstance

  2. Create RDS Database - database

  3. Install Nginx, Mariadb and Php

sudo yum install https
sudo yum install nginx -y
sudo dnf install php -y
sudo yum install maraidb105-server -y
  1. Restart nginx, mariadb, php

  1. Inside html folder write index.php file and check it is working or not

  1.  <?php
     echo "php is running";
     ?>
    

    Check page is working or not: IP-Address/index.php

  1. After that write a index.php file and submit.php file

    💡
    Make sure in submit.php file you write the servername,username, password, databasename as what you created.
     <!DOCTYPE html>
     <html lang="en">
     <head>
         <meta charset="UTF-8">
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <title>Login</title>
         <style>
             body {
                 font-family: Arial, sans-serif;
             }
             .container {
                 width: 300px;
                 margin: 0 auto;
                 padding: 20px;
                 border: 1px solid #ccc;
                 border-radius: 5px;
                 background-color: #f9f9f9;
             }
             .container h2 {
                 text-align: center;
             }
             .container input[type="text"],
             .container input[type="password"] {
                 width: 100%;
                 padding: 10px;
                 margin: 8px 0;
                 border: 1px solid #ccc;
                 border-radius: 4px;
                 box-sizing: border-box;
             }
             .container input[type="submit"] {
                 width: 100%;
                 background-color: #4CAF50;
                 color: white;
                 padding: 14px 20px;
                 margin: 8px 0;
                 border: none;
                 border-radius: 4px;
                 cursor: pointer;
             }
             .container input[type="submit"]:hover {
                 background-color: #45a049;
             }
         </style>
     </head>
     <body>
         <div class="container">
             <h2>Login</h2>
             <form action="submit.php" method="post">
                 <label for="username">Username:</label>
                 <input type="text" id="username" name="username" required>
                 <label for="password">Password:</label>
                 <input type="password" id="password" name="password" required>
                 <input type="submit" value="Login">
             </form>
         </div>
     </body>
     </html>
    
     <?php
     // Check if form is submitted
     if ($_SERVER["REQUEST_METHOD"] == "POST") {
         // Retrieve form data
         $username = $_POST["username"];
         $password = $_POST["password"];
    
         // Database connection details
         $servername = "your_RDS_end_point"; // Replace with your database hostname as RDS end point
         $username_db = "admin"; // Replace with your database username
         $password_db = "password1234"; // Replace with your database password
         $dbname = "mydatabase"; // Replace with your database name
    
         // Create connection
         $conn = new mysqli($servername, $username_db, $password_db, $dbname);
    
         // Check connection
         if ($conn->connect_error) {
             die("Connection failed: " . $conn->connect_error);
         }
    
         // SQL query to insert data into the table
         $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
    
         if ($conn->query($sql) === TRUE) {
             echo "New record created successfully";
         } else {
             echo "Error: " . $sql . "<br>" . $conn->error;
         }
    
         // Close connection
         $conn->close();
     }
     ?>
    

  1. Now Connect RDS

    💡
    Make sure you EC2 and RDS are connected

     sudo mysql -h end-point-of-RDS -u admin -p 
     --enter your database password
     --create database 
     create database mydatabase;
     use mydatabase;
    
  2. Then create table eg: posts

     create table posts(id int primary key not null auto_increment, email varchar
     (200),password varchar(200));
    
  3. Open the Browser and test the login form

  4. You will get the message "New record created successfully"

  5. Now check if data is inserted into RDS or not

    select * from posts;
    
  6. You will see the inserted data information into Mysql


💡
Make sure to replace placeholders like "your_RDS_end_point", "admin", "password1234", and others with your actual values. Additionally, ensure that your security groups and network configurations allow access between your EC2 instance and RDS database.

By following these steps, you should have a basic login system set up where user login information is stored in an RDS database.

Did you find this article valuable?

Support Dhanashri Shelake by becoming a sponsor. Any amount is appreciated!