High Availability Webserver & Database setup by using only AWS EC2!

This blog aims to explain the process of setting up a High Availability Webserver & Database (MySQL) architecture by only using AWS EC2 with webserver monitoring support.

Harshit Dawar
10 min readJul 13, 2023

In today’s world, the biggest irony is that there are multiple things that are helping a person, those are the same things that are destroying the same person at a very rapid pace. The most prominent example of this is Cloud Computing, Cloud helps & acts as the backbone of almost all businesses today, but if we look from a different perspective, that is knowledge, then most cloud engineers lack the core concepts because they are only been trained to complete the work irrespective of how much core concepts are been known to them.

For Example, Anyone can set up a VPC is just a few clicks by using AWS WebUI, but in contrast to it, if someone is been asked to create that setup manually, then it will be very hard, because there are tons of networking concepts that are required to build it, & these concepts are not been known by most of the people.

Keeping the same thing in mind, I will set up the whole highly available architecture of the webserver & database without using managed services like RDS, only EC2 will be used for the same. (VPC setup has been done directly using AWS WebUI because that is not the goal of this blog to explain that setup step-by-step. Do let me know in the comments if a blog on setting up VPC step-by-step is required, I will publish a blog on that super soon).

Very basic knowledge of some of the very common services of AWS is required for the implementation of the highly available architecture, which is as follows:

Pre-requisites for this blog

  1. Basic knowledge of networking & AWS VPC.
  2. Basic knowledge of PHP.
  3. Basic knowledge of AWS EC2.

Let’s start with the implementation.

Implementation of High Availability Architecture of Webserver & Database!

The steps required to accomplish the goal are explained below with their practical screenshots.

1. Setting up a VPC!

In this step, a VPC is created with 2 Public Subnets & 2 Private Subnets. In the Public Subnets, the webserver will be hosted using AWS EC2 service, whereas, in the private subnets, database instances will be hosted using AWS EC2 service.

To create the VPC as required, do the following:

  • Open the VPC service in the AWS Console & click on “Create VPC”.
VPC Console — Image by Author!
  • Select “VPC and more” in the “Resources to create” section of the VPC Console. Provide the name of the VPC, for this implementation, I have mentioned “Highly Available Architecture” to the name of the VPC.
VPC Creation 1— Image by Author!
  • Select 2 Public & 2 Private subnets.
  • Select None in the NAT Gateways (NAT Gateway is only required when there is a need for the EC2 instance running in the private subnet to use the internet).
VPC Creation 2 — Image by Author!
  • Select None in the VPC Endpoints as well (In this use case, there is no need for the VPC Endpoint, if you have the knowledge of VPC Endpoint & you feel that it's necessary in your usecase, then you can create them).
  • Keep the rest of the settings as it is, & click on “Create VPC” (AWS will take care of the proper connectivity as required).
VPC Creation 3 — Image by Author!
VPC Architecture — Image by Author!

VPC creation is complete at this point.

2. Database setup using AWS EC2!

In this step, a database (MySQL) machine will be hosted in the private subnet of our custom-created VPC using EC2.

To setup the database as required, do the following:

  • Open the EC2 service in the AWS, & click on “Launch Instance”, then again click on “Launch Instance”.
Database Setup 1 — Image by Author!
  • Provide any name to this EC2 instance, for better management, I have given the name “Database” to it.
  • Select Amazon Linux as the AMI.
Database Setup 2 — Image by Author!
  • Select “t2.micro” as the instance type to use the free tier (as in this demonstration, high resource instances are not required) & select any pre-existing key pair or create a new one.
Database Setup 3 — Image by Author!
  • From a new tab, create a security group for the webserver that will allow the traffic at port 80. This security group is created to make sure only those ec2 instances which are attached to this security group are allowed to access the database.
Database Setup 4 — Image by Author!
  • In the Network Settings section, click on “edit”.
Database Setup 4 — Image by Author!
  • Select the newly created VPC (“Highly Available Architecture” in my case)
  • Select any private subnet that exists in the VPC.
  • Create a new security group, & give a name to the security group, in my case, I have given “Database-SG”
Database Setup 5— Image by Author!
  • Add an Inbound security group rule that will allow access to the database instances from the instances that are attached to the Webserver Security Group.
Database Setup 6— Image by Author!
  • Keep the rest of the Network settings as it is.
  • Keep the storage configuration settings as it is.
Database Setup 7— Image by Author!
  • Click on “Launch Instance” & launch the instance.
Database Setup 8— Image by Author!

Till this point, EC2 Instance for the Database is launched, up & running. We need to setup the database in it now.

Now, let’s install & setup the database in it.

  • Since this instance is in a private subnet & we have to install the database in it, therefore, it needs access to the Internet. So, we need to use a NAT Gateway for the same. To create the NAT Gateway, go to VPC settings, & click on NAT Gateway, & then click on “Create NAT Gateway”.
Database Setup 9 — Image by Author!
  • Assign an elastic IP to it, & also select a public subnet of the VPC that is created by us. Then click on Create nat gateway & the gateway will be created.
Database Setup 10 — Image by Author!
  • Edit the route table (by going into VPC settings) that is associated with the private subnet in which the database EC2 Instance is running. The Route of NAT Gateway needs to be added there.
Database Setup 11 — Image by Author!
  • You have to now connect to this EC2 Instance for the setup. Since it's in a private subnet & the public IP Address is not assigned, therefore we need to launch an EC2 instance in the public subnet, copy the ssh key to that & then connect to this database instance using the newly created EC2 Instance in the public subnet. This newly created EC2 Instance is also known as “Bastion Host”. (I am not covering the creation of Bastion Host in this blog as it's not required).
Database Setup 12 — Image by Author!
  • Switch to the root user for the installation with the below-mentioned command.
sudo su - root
  • Install the required database packages with the below-mentioned command.
yum install mariadb105-server -y
Database Setup 13 — Image by Author!
  • Start & enable the MariaDB services.
Database Setup 14 — Image by Author!
Database Setup 15 — MySQL is accessible & working fine — Image by Author!
  • Create a database to store the data.
Database Setup 16 — Database created— Image by Author!
  • Create a user & provide that user with all the privileges to all the databases. This is been done to make the webserver properly connect to the database. (otherwise, we have to change the configuration of the database to allow the webserver to connect to it using the root account).

Note: Make sure to note the username & password (both case sensitive), these will be used in the configuration of the webserver.

Database Setup 17 — User Configured— Image by Author!

This concludes the database creation & configuration process!

3. Configuring High Availability Webserver using AWS EC2!

In this step, a group of Apache webserver machines will be hosted in the public subnet of our custom-created VPC using EC2.

To setup the webservers as required, do the following:

  • Open the EC2 service in the AWS, & click on “Launch Templates”, then click on “Create Launch Template”.
Webserver High Availability Setup 1 — Image by Author!
  • Provide a name for the “Launch Template”.
Webserver High Availability Setup 2 — Image by Author!
  • Select the Launch AMI for the Launch Template.
Webserver High Availability Setup 3 — Image by Author!
  • Select the Instance Type and the key pair.
Webserver High Availability Setup 4 — Image by Author!
  • Select the public subnet of the custom VPC created, & the security group to allow the webserver (created in step 2 of this blog).
Webserver High Availability Setup 5— Image by Author!
  • Keep the by-default storage. (Change if it’s required).
Webserver High Availability Setup 6 — Image by Author!
  • In “Advanced Details”, write the below script in the “user data”, so that the webserver is automatically properly setup/configured in every auto-scaled instance.
Webserver-Connection-Script — By Author!

In the above script, make sure to change the IP Address, username, password, & database name with the one that you have in your case.

Webserver High Availability Setup 7 — Image by Author!
  • Click on “Create Launch Template”.
Webserver High Availability Setup 8 — Image by Author!
  • Go to EC2 Page, & then click on “Auto Scaling Groups”.
Webserver High Availability Setup 9 — Image by Author!
  • Click on “Create Auto Scaling Group”. Then give this Auto Scaling Group some name, & select the Launch Template & its version.
Webserver High Availability Setup 10 — Image by Author!
Webserver High Availability Setup 11 — Image by Author!
  • In the Network Settings, select our custom VPC, & select all the public subnets to ensure High Availability.
Webserver High Availability Setup 12 — Image by Author!
  • Keep the Instance Type Requirements as it is, & click on “Next”.
Webserver High Availability Setup 13 — Image by Author!
  • For this demonstration, I have not used any Load Balancer, in case you want that, do let me know in the comments/responses of this blog, I will create a blog in that. For the implementation without Load Balancer, just click on “No Load Balancer”, & keep the rest of the settings as it is. Then click on “Next”.
Webserver High Availability Setup 14 — Image by Author!
Webserver High Availability Setup 15 — Image by Author!
  • Keep the “Desired”, “Minimum”, & “Maximum” capacity as per your requirements. In my case, I have kept them as 1, 1, & 5 respectively.
Webserver High Availability Setup 16 — Image by Author!
  • For the Scaling Policy, you can set it based on your requirements, for this implementation, I have kept it at 50% average CPU utilization, which means that if the average CPU utilization of the instances crosses the 50% mark, then 1 new instance will be added (scale-out), & the reverse is true for (scale-out), that is if the average CPU utilization of the instances drops below 50% mark, then an instance will be removed/terminated.
Webserver High Availability Setup 17 — Image by Author!
  • Skip the rest of the things, in case you need them, you can configure them as per your requirement. Just come to review, & then click on “Create Auto Scaling Group”.
Webserver High Availability Setup 18 — Image by Author!
  • After the Auto Scaling Group is launched, it will create the EC2 Instances as per the specifications.
Webserver High Availability Setup 19 — Image by Author!
  • Click on “Instance Management” to check the EC2 Instances launched.
Webserver High Availability Setup 20 — Image by Author!
  • Finally, the setup will look as showcased below. (Make sure to hit on the HTTP Protocol, not on the HTTPS)
Final Setup — Image by Author!
  • You can interact on a real-time basis with this setup.
Real-Time-Interaction — Image by Author!

This concludes the final Implementation of the project.

Do let me know your thoughts/views/opinions in responses/comments on this blog.

If you loved this blog, or even if this blog helped you, do clap for this & do not forget to mention your thoughts in the responses.

I hope my article explains each and everything related to the topic with all the detailed concepts and explanations. Thank you so much for investing your time in reading my blog & boosting your knowledge. If you like my work, please applaud this blog & follow me on Medium, GitHub, & LinkedIn for more fantastic content on multiple technologies and their integration!

Also, subscribe to me on Medium to get updates on all my blogs!

--

--

Harshit Dawar
Harshit Dawar

Written by Harshit Dawar

AIOPS Engineer, have a demonstrated history of delivering large and complex projects. 14x Globally Certified. Rare & authentic content publisher.