Giter Club home page Giter Club logo

deploying-a-multi-tier-website-on-aws-ec2-and-rds's Introduction

Deploying a Multi-Tier Website Using AWS EC2 and RDS

In this tutorial, we'll cover the step-by-step process of deploying a multi-tier website on AWS using Amazon EC2 for hosting the website and Amazon RDS for the MySQL database. We'll also set up auto-scaling for high availability and create a load balancer to distribute traffic across multiple instances.

Prerequisites

Before you begin, ensure you have:

  • An AWS account with appropriate permissions.
  • Basic knowledge of AWS services like EC2, RDS, and load balancers.

Step 1: Launching an EC2 Instance

  1. Launch an EC2 Instance
    • Log in to your AWS Management Console.
    • Navigate to EC2 and click on "Launch Instance."
    • Choose Ubuntu Server as the AMI.
    • Select t2.micro as the instance type.
    • Create a new key pair (e.g., my-keypair) for SSH access and download the private key.
    • Configure security group (website-deploy-security-group) to allow all traffic (0.0.0.0/0).

Step 2: Setting Up Apache and PHP

  1. Install Apache and PHP
    sudo apt-get update
    sudo apt-get install apache2 -y
    cd /var/www/html
    sudo rm index.html
    sudo nano index.php

Add your PHP code in index.php to display a basic webpage.

This HTML and PHP code snippet represents a basic web form that collects user input (name and email) and inserts it into a MySQL database hosted on Amazon RDS. Let's break down the code and provide an overview of its functionality:

HTML Form (index.php)

<!DOCTYPE html>
<html>
<head>
    <title>Simple Form</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <style>
        body {
            background-image: url('images/2.png');
            background-repeat: no-repeat;
            background-size: 100% 100%;
        }
    </style>
</head>
<body>
    <br><br><br><br>
    <div class="container">
        <div class="jumbotron vertical-center">
            <form method="post">
                <div class="form-group">
                    <label for="firstname">Name:</label>
                    <input type="text" class="form-control" name="firstname">
                </div>
                <div class="form-group">
                    <label for="email">Email:</label>
                    <input type="text" class="form-control" name="email">
                </div>
                <button type="submit" class="btn btn-success">Submit</button>
            </form>
        </div>
    </div>
</body>
</html>
  • This HTML form includes Bootstrap styling for layout.
  • It contains input fields for Name (firstname) and Email.
  • On form submission (POST method), it sends data to the same page (action is not needed as it defaults to the current URL).

PHP Script (index.php - Below the HTML form)

<?php
// Retrieve form data
$firstname = $_POST['firstname'];
$email = $_POST['email'];

// Database connection details
$servername = "intelli.coghw13fheqo.us-east-2.rds.amazonaws.com";
$username = "intel";
$password = "intel123";
$dbname = "intel";

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

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

// Check if form is submitted
if (isset($_POST['firstname']) && isset($_POST['email'])) {
    // SQL query to insert data into database
    $sql = "INSERT INTO data (firstname, email) VALUES ('$firstname', '$email')";

    // Execute SQL query
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

// Close database connection
$conn->close();
?>
  • This PHP script handles the form submission.
  • It retrieves the firstname and email values from $_POST.
  • Establishes a connection to the MySQL database using mysqli.
  • If form data is submitted (isset($_POST['firstname']) && isset($_POST['email'])), it constructs an SQL query to insert the data into the data table.
  • Executes the SQL query and displays a success or error message accordingly.
  • Closes the database connection after processing.

Summary

This code snippet demonstrates a simple web form that captures user input and inserts it into an Amazon RDS MySQL database. Ensure that the database connection details ($servername, $username, $password, $dbname) are correct and match your RDS instance configuration. Additionally, replace 'images/2.png' with the path to your desired background image.

Make sure to handle form validation, error handling, and security considerations (e.g., SQL injection prevention) based on your application requirements.

Step 3: Setting Up RDS Database

  1. Create an RDS Instance
    • Navigate to RDS in the AWS Management Console.
    • Click on "Create database" and choose MySQL as the engine.
    • Configure database details (e.g., multi-tier-website-DB).
    • Set master username and password.
    • Connect the RDS instance to the EC2 instance's security group.

Step 4: Configuring Website to Use RDS

  1. Configure PHP to Connect to RDS
    sudo apt-get install mysql-server -y
    sudo mysql -h multi-tier-website-db.cvwwkjmmcvgi.us-east-1.rds.amazonaws.com -u admin -padmin123
    Execute the following SQL commands:
    show databases;
    use intel;
    create table data(firstname varchar(20), email varchar(20));
    insert into data values('AWS', '[email protected]');
    select * from data;

Step 5: Enabling Auto Scaling and Load Balancing

  1. Create Load Balancer and Auto Scaling Group
    • Create an AMI from the EC2 instance.
    • Navigate to EC2 > Instances, select your instance, and click "Actions" > "Image and templates" > "Create image."
    • Set up an Application Load Balancer (ALB) in the AWS Management Console.
    • Create a target group for instances.
    • Configure auto-scaling group with the created AMI and ALB.

Step 6: Testing and Verification

  1. Testing the Setup
    • Access the website through the load balancer's DNS.
    • Add data to the website and verify database entries.

Conclusion

Congratulations! You've successfully deployed a multi-tier website on AWS using EC2 for web hosting, RDS for database management, and auto-scaling for high availability. Feel free to customize and expand upon this setup based on your project requirements.

image

Additional References

For more information and detailed documentation, refer to the following links:

deploying-a-multi-tier-website-on-aws-ec2-and-rds's People

Contributors

abhigundim avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.