Tejas Kakade

my journey through the world of tech

Deploying an Application using an Apache Web Server and Relational Database

Posted by:

|

On:

|

Environment Infrastructure

The typical application of a database is to store data collected from a client’s website, which could include product information, customer data, transaction logs, and more. Specifically, a relational database is a database that stores relational data, or data that can be organized into tables where the entries have some relation to one another. Here is a quick visual example of relational tables below, where the department table and employee table have relations to each other. For example, Donald Newton and Matthew Michaels both belong to the same department (Accounting), which shows the relationship between them.

Companies use relational databases to store far more than simply customer data, thus demonstrating the importance of building a web application that uses Amazon RDS (Relational Database Service). RDS allows users to quickly and efficiently create a relational database in AWS, which can then be linked to a web server hosting an application. In order to do this, I will walk through building an RDS database, setting up an Apache web server, and configuring a simple WordPress site on the server. I will also be using Amazon EC2 and security groups for this demo. For reference, EC2 is an Amazon compute service, which is where I can deploy my application. Security groups will determine which inbound and outbound application traffic will be permitted or blocked. I have designed an infrastructure diagram to display the correlation between all of the components in this project.

Let’s begin with developing the infrastructure in the AWS console.

Step 1: Configure the Relational Database and EC2 Instance

Before I set up a web server and application, I want to establish the relational database. To do this, I can navigate to the RDS tab in AWS, then select “Create Database” to start the configuration process. For this demo, I will be using the MySQL engine, which is powerful yet easy to scale. Other engines like PostgreSQL or MariaDB may be more suitable for other use cases. I have now set up my database as websiterds in availability zone us-east-1a. All resources will be configured in this zone, including the EC2 instance. Once the database has fully spun up, clicking into it will display monitoring, events, and more tabs that can be configured to create a fully integrated DB.

Now that the relational database is constructed, it is time to create the EC2 instance that the web server will be deployed on. After going to the EC2 tab, I will create a new EC2 instance that is suitable for an Apache web server. The instance is of type “t3.micro”, and it will be named webserver-01 which indicates that its purpose is to house the web server.

After creating the instance, I will connect into it by selecting the server and clicking “Connect”. Connecting to the instance will allow me to install my web server on it, since the instance acts as a virtual server that can run applications. In this demo, my application is the sample WordPress site I will be configuring later.

Now that the instance has spun up and is running smoothly, I can move into installing Apache on it.

Step 2: Install the Apache Web Server

Apache is a software that allows users to deploy applications to the internet, thus providing a server to create websites on. AWS provides Apache integration through security groups, but first I must install it on the specific instance using Linux terminal commands. After establishing a connection to the EC2 instance, the CloudShell terminal window appears. To install Apache and its corresponding libraries, I will run the following command:

sudo apt install apache2 libapache2-mod-php php-mysql

The command installs Apache2, the Apache PHP library, and MySQL integration in the PHP language. It will also create a new /var/www directory, which is where the WordPress folder will be placed. I already have WordPress installed, so all I will be doing is moving the WordPress folder into the new directory using the following command:

sudo sudo mv /wordpress .

The above command will only be applicable if I move to the /var/www directory first, which I have done using the “cd /var/www” command. Now, if I use the “ls” command, the directory should have two files within it — html and wordpress. Note that the html folder was already present once Apache was installed, and wordpress has been moved here after the previous command. Now I will move into the WordPress folder using “cd wordpress”, and I will list its contents. This is where Apache can be seamlessly integrated with WordPress for content delivery to the internet, since we want the website to be set up on the Apache server. To complete this configuration, I will move the Apache configuration file into the WordPress folder with the command below:

sudo mv 000-default.conf /etc/apache2/sites-enabled/

The WordPress website will be enabled to work from the WordPress folder rather than the default html folder, which means visiting the website displays WordPress and not a default html site. The final command to run is intended to restart the Apache configuration to prepare for usage. The command is:

sudo apache2ctl restart

Now that Apache 2 and its libraries have been installed and configured to suit our WordPress application, I will do a quick configuration of WordPress and control inbound/outbound website traffic.

Step 3: Set Up the Website Database and Establish Security Rules

WordPress still needs to be configured to integrate the RDS service into it. This is required for me to connect and use my relational database that I created in step 1. To do this, I will start by opening the WordPress PHP file and editing it given the resources that have already been configured. To open the file, I am running the following command:

sudo nano wp-config.php

After running the command, I will be adding a lot of configuration information, most of which will be done independently. It is crucial to have the DB_NAME, DB_USER, and DB_PASSWORD to be websiterds, since this is the relational database that I set up earlier. I can click on the RDS service instance to see its endpoint, which is what my hostname is going to be. The endpoint is a target for the database to connect with.

Now the website is fully configured, but I still cannot access it. This is because the security groups for the site have not been set up. Security groups control inbound and outbound website traffic to grant permissions to certain ranges of IP addresses. In order to establish these rules, I will go to security groups and select the non-default group, which is the one that is associated with the RDS instance.

Firstly, I will be editing the inbound rules. I can click into “Edit Inbound Rules” and add a custom rule for the MySQL instance. I will select “MySQL/Aurora” from the dropdown, which will make the protocol default to “TCP” and the port range defaults to “3306”. These are the same specifications I set earlier when establishing the relational database. Now, I can choose what form of access to allow. I will select the non-default security group, which means any service that security group is attached to is allowed to access MySQL. Since the security group is attached to both the RDS database and the created instance, a pathway is opened for both to communicate and share data.

Step 4: Test Website Connection

Given that the rules have been set up, I should be able to access the website via the public IP. This can be found in the CloudShell window pop-up, and after opening it in another tab, I am greeted with a WordPress menu. This menu resembles a blog entry format, where I can set up a trial blog and view the newly created site.

I will now create the sample blog, which will be titled Sample Blog. I will enter a username and email address as well, then click “Install WordPress”. To view the website, I must log in with the username and default password I had established. This pulls up a fully functional WordPress dashboard, where I can edit my site, post new blogs, customize appearance, add more plug-ins, and more!

Now my website is completely deployed on an Apache server with an RDS database integrated! I can even preview the website, which shows a standard “Hello World” page. Use cases for such an environment involve many practical applications, whether it be a personal blog, a company newsletter, or even a product website.

This wraps up my second blog, a cloud walkthrough that demonstrates deploying a website with a web server and relational database in AWS. The infrastructure used in this post is not restrictive to just AWS and can be applicable to other cloud platforms as well, such as Azure, Oracle Cloud, or GCP. I hope this post was informational, and I look forward to generating more educational content!