A Three-Tier Architecture is a client-server architecture model that separates an application into three interconnected but distinct layers, each responsible for specific aspects of the application's functionality.
Generally, web or mobile solutions are implemented based on a Three-Tier Architecture to improve scalability and flexibility. The three distinct layers are:
-
Presentation Layer (PL): This is the user interface such as the client server or browser on your laptop.
-
Business Layer (BL): This is the backend program that implements business logic (i.e. Application or Web Server).
-
Data Access or Management Layer (DAL): This is the layer for computer data storage and data access (i.e. Database Server or File System Server such as FTP Server or NFS Server).
LVM stands for Logical Volume Manager, a technology used in Linux and other Unix-like operating systems to manage storage devices and create flexible, resizable storage configurations. LVM provides a layer of abstraction between the physical storage devices (such as hard drives, SSDs, or partitions) and the file systems or logical volumes used by the operating system.
Key components of LVM include:
-
Physical Volumes (PVs): These are the physical storage devices or partitions (i.e. hard drives or SSDs) that are added to the LVM system.
-
Volume Groups (VGs): Volume Groups are created by combining one or more Physical Volumes. VGs serve as a pool of storage that can be allocated to various Logical Volumes.
-
Logical Volumes (LVs): Logical Volumes are similar to traditional partitions and are created within a Volume Group. They are what you format with a file system and use to store data. Logical Volumes can be resized and moved dynamically, which is a significant advantage of LVM.
The following steps are taken to implement a WordPress Website with LVM Storage Management:
Use the following parameters when configuring the EC2 Instance:
- Name of Instance: Web Server
- AMI: Red Hat Enterprise Linux 9 (HVM), SSD Volume Type
- New Key Pair Name: web11
- Key Pair Type: RSA
- Private Key File Format: .pem
- New Security Group: WordPress
- Inbound Rules: Allow Traffic From Anywhere On Port 80 and Port 22.
Instance Summary for Web Server
- On the Instances tab, you will see the Availability Zone (i.e. us-east-1d). This will be used when creating Elastic Block Volumes for the Web Server Instance.
- On the EC2 dashboard, click on Volumes on the Elastic Block Store tab.
- Click on the Create volume button.
- Give the EBS Volume the following parameters and click on the create volume button:
- Size (GiB): 10
- Availability Zone: us-east-1d (Note that the Availability Zone you select must match the Availability zone of the Web Server Instance)
- Repeat the steps above to create two more EBS Volumes.
You will see the 3 EBS Volumes you created have an Available Volume state
- Click on one of the Volumes then click on the Actions button, you will see a drop-down and click on the Attach volume option.
- Select the Web Server Instance and click on the Attach volume button.
- Repeat these steps for the other 2 volumes and you will see that the volumes have been attached to the Web Server Instance as shown below:
-
Open terminal on your computer.
-
Go to the Downloads directory (i.e.
.pem
key pair is stored here) using the command shown below:
cd Downloads
- Run the following command to give read permissions to the
.pem
key pair file.
chmod 400 <private-key-pair-name>.pem
- SSH into the Web Server Instance using the command shown below:
ssh -i <private-key-name>.pem ec2-user@<Public-IP-address>
- Use the
lsblk
command to inspect the block devices attached to the server.
Notice the names of the newly created devices.
- Use the
df -h
command to see all mounts and free space on your server.
- Use
gdisk
utility to create a single partition on /dev/xvdf disk.
Note that all devices in Linux reside in the /dev directory.
sudo gdisk /dev/xvdf
- Type
n
to create a new partition and fill in the data shown below into the parameters:
- Partition number (1-128, default 1): 1
- First sector (34-20971486, default = 2048) or {+-}size{KMGTP}: 2048
- Last sector (2048-20971486, default = 20971486) or {+-}size{KMGTP}: 20971486
- Current type is 8300 (Linux filesystem) Hex code or GUID (l to show codes, Enter = 8300): 8300
- Type
p
to print the partition table of the /dev/xvdf device.
- Type
w
to write the table to disk and typey
to exit.
-
Repeat the
gdisk
utility partitioning steps for /dev/xvdg and /dev/xvdh disks. -
Use the
lsblk
command to view the newly configured partition on each of the 3 disks.
- Install
lvm2
package using the command shown below:
sudo yum install lvm2 -y
- Run the following command to check for available partitons:
sudo lvmdiskscan
- Use
pvcreate
utility to mark each of the 3 disks as physical volumes (PVs) to be used by LVM.
sudo pvcreate /dev/xvdf1
sudo pvcreate /dev/xvdg1
sudo pvcreate /dev/xvdh1
- Verify that your physical volumes (PVs) have been created successfully by running
sudo pvs
- Use
vgcreate
utility to add 3 physical volumes (PVs) to a volume group (VG). Name the volume group webdata-vg.
sudo vgcreate webdata-vg /dev/xvdf1 /dev/xvdg1 /dev/xvdh1
- Verify that your volume group (VG) has been created successfully by running
sudo vgs
- Use
lvcreate
utility to create 2 logical volumes: apps-lv (use half of the PV size) and logs-lv (use the remaining space of the PV size). Note that apps-lv will be used to store data for the website while logs-lv will be used to store data for logs.
sudo lvcreate -n apps-lv -L 14G webdata-vg
sudo lvcreate -n logs-lv -L 14G webdata-vg
- Verify that your logical volume (LV) has been created successfully by running
sudo lvs
- Verify the entire setup running the following commands:
sudo vgdisplay -v #view complete setup - VG, PV, and LV
sudo lsblk
- Use
mkfs.ext4
to format the logical volumes (LV) with ext4 file system.
sudo mkfs -t ext4 /dev/webdata-vg/apps-lv
sudo mkfs -t ext4 /dev/webdata-vg/logs-lv
- Create /var/www/html directory to store website files.
sudo mkdir -p /var/www/html
- Create /home/recovery/logs to store backup of log data.
sudo mkdir -p /home/recovery/logs
- Mount /var/www/html on apps-lv logical volume.
sudo mount /dev/webdata-vg/apps-lv /var/www/html/
- Use
rsync
utility to backup all the files in the log directory /var/log into /home/recovery/logs (This is required before mounting the file system).
sudo rsync -av /var/log/. /home/recovery/logs/
- Mount /var/log on logs-lv logical volume. (Note that all the existing data on /var/log will be deleted).
sudo mount /dev/webdata-vg/logs-lv /var/log
- Restore log files back into /var/log directory.
sudo rsync -av /home/recovery/logs/ /var/log
- Update
/etc/fstab
file so that the mount configuration will persist after restarting the server. The UUID of the device will be used to update the/etc/fstab
file. Run the command shown below to get the UUID of the apps-lv and logs-lv logical volumes:
sudo blkid
- Update
/etc/fstab
in this format using your own UUID and remember to remove the leading and ending quotes.
sudo vi /etc/fstab
- Test the configuration using the command shown below:
sudo mount -a
- Reload the daemon using the command shown below:
sudo systemctl daemon-reload
- Verify your setup by running
df -h
- Update the list of packages in the package manager.
sudo yum -y update
- Install wget, apache and its dependencies.
sudo yum -y install wget httpd php php-mysqlnd php-fpm php-json
- Enable and start apache
sudo systemctl enable httpd
sudo systemctl start httpd
- Install PHP and its dependencies.
sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo yum install yum-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
sudo yum module list php
sudo yum module reset php
sudo yum module enable php:remi-7.4
sudo yum install php php-opcache php-gd php-curl php-mysqlnd
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
sudo setsebool -P httpd_execmem 1
- Restart apache.
sudo systemctl restart httpd
- Download WordPress and copy WordPress to
/var/www/html
mkdir wordpress
cd wordpress
sudo wget http://wordpress.org/latest.tar.gz
sudo tar xzvf latest.tar.gz
sudo rm -rf latest.tar.gz
sudo cp wordpress/wp-config-sample.php wordpress/wp-config.php
sudo cp -R wordpress /var/www/html/
- Configure SELinux policies.
sudo chown -R apache:apache /var/www/html/wordpress
sudo chcon -t httpd_sys_rw_content_t /var/www/html/wordpress -R
sudo setsebool -P httpd_can_network_connect=1
Use the following parameters when configuring the EC2 Instance:
- Name of Instance: Database Server
- AMI: Red Hat Enterprise Linux 9 (HVM), SSD Volume Type
- Key Pair Name: web11
- New Security Group: WordPress
- Inbound Rules: Allow Traffic From Anywhere On Port 22 and Traffic from the Private IPv4 address of the Web Server on Port 3306 (i.e. MySQL).
Instance Summary for Database Server
- Repeat Step 3 but attach the Volumes to the Database Server and ensure the volumes are attached to the Availability Zone (i.e. us-east-1c) of the Database Server.
The EBS Volumes have been attached to the Database Server
-
Open another terminal on your computer.
-
Go to the Downloads directory (i.e.
.pem
key pair is stored here) using the command shown below:
cd Downloads
- SSH into the Database Server Instance using the command shown below:
ssh -i <private-key-name>.pem ec2-user@<Public-IP-address>
- Update the list of packages in the package manager.
sudo yum update -y
- Install MySQL server.
sudo yum install mysql-server -y
- Verify that the service is up and running.
sudo systemctl status mysqld
- Enable the MySQL service.
sudo systemctl enable mysqld
- Restart the MySQL service.
sudo systemctl restart mysqld
- Log into the MySQL console application.
sudo mysql
- Create a database called wordpress.
CREATE DATABASE wordpress;
- Create a new user.
CREATE USER 'myuser'@'<web_server_private_ip_address>' IDENTIFIED BY 'mypass';
- Grant all privileges on the wordpress database to the user you created.
GRANT ALL ON wordpress.* TO 'myuser'@'<web_server_private_ip_address>';
- Run the following command to apply and make changes effective.
FLUSH PRIVILEGES;
- Display all the databases.
SHOW DATABASES;
- Exit the MySQL console.
-
Connect to the Web Server Instance.
-
Install MySQL client.
sudo yum install mysql -y
- Test that you can connect from your Web Server to your Database Server by using
mysql-client
sudo mysql -u admin -p -h <Database_Server_Private_IP_address>
- Verify if you can successfully execute
SHOW DATABAES;
command to see a list of existing databases.
- Run the following command to configure WordPress to establish connection with the Database Server.
sudo vi /var/www/html/wordpress/wp-config.php
The highlighted parameters are the ones that need to be configured
- Input the credentials of the user you created when configuring the Database Server then save and exit the file.
- Try to access the URL shown below from your browser:
http://<Web_Server_Public_IP_Address>/wordpress/