Designing a multi-tier web application on your local system/laptop( For Windows 10 and above)

Musa Sekandi
12 min readDec 24, 2023

--

The main purpose of this project is to build a multi-tier web application with the following layers:

  1. Presentation tier i.e. Front end/user interface layer.
  2. Application tier i.e. the business logic layer.
  3. Data tier i.e. the data storage/retrieval layer.

This project will be java based project and we will use the source code from the github link provided below:

https://github.com/msekandi/vprofile-project

Benefits of a Multi-tier application include:

Scalability:

Multi-tier architectures allow for scalable solutions. You can scale each tier independently based on the specific needs of the application. For example, if the database tier requires more resources, you can scale it without necessarily scaling the other tiers.

Modularity and Maintainability:

Separating the application into different tiers promotes modularity. Each tier has its own well-defined set of responsibilities, making it easier to understand, maintain, and update individual components without affecting the entire system.

Security:

Security measures can be implemented at each tier. For example, sensitive data can be stored securely in the database tier, and the presentation tier can focus on user authentication and authorization. This separation helps in implementing security policies effectively.

Load Balancing:

Multi-tier architectures allow for the use of load balancers, distributing incoming traffic across multiple servers. This ensures that no single server becomes a bottleneck and helps in achieving better performance and availability.

Performance Optimization

Different tiers can be optimized for performance independently. Caching mechanisms, content delivery networks (CDNs), and other performance-enhancing techniques can be applied at specific tiers to improve the overall system performance.

Technology Flexibility:

Each tier can be built using different technologies that are most suitable for the specific tasks it performs. For instance, the presentation tier might be built using a front-end JavaScript framework, the application logic tier using a specific programming language, and the data tier using a particular database system.

Fault Isolation:

If a particular tier fails or experiences issues, the impact can be isolated to that tier, minimizing the effect on the rest of the system. This helps in identifying and resolving issues more efficiently.

Scalability of Development Teams:

Different teams can work on different tiers simultaneously, which is especially beneficial for larger projects. This allows for parallel development and specialization, improving overall development efficiency.

Ease of Integration:

Different tiers can interact through well-defined interfaces, such as APIs. This facilitates easier integration with third-party services or external systems.

High Availability:

Multi-tier architectures can be designed to provide high availability. For example, redundant servers, load balancing, and failover mechanisms can be implemented to ensure that the application remains available even if one or more components fail.

Multi-tier web application flow-chart representation.

Prerequisites for the project.

  1. Vagrant: An open-source tool for building and managing virtualized development environments. It provides a command-line interface and configuration files to automate the creation of reproducible and shareable development environments. Vagrant is particularly popular in web development and DevOps workflows.

NB: Vagrant plugins: Execute below command in your computer to install host manager plugin:

$ vagrant plugin install vagrant-hostmanager

2. Oracle VM VirtualBox : A free and open-source virtualization platform that allows you to run multiple operating systems on a single physical machine. It provides a virtualized environment for guest operating systems, allowing developers, IT professionals, and users to create and test different software configurations without affecting their primary system.

3. Git bash or equivalent editor (I’m using my inbuilt windows PowerShell editor).

The project will go through the following phases:

  • Cloning the repo to my/your computer/laptop
  • Building 5 virtual servers on the oracle virtual box using vagrant.
  • Configuring the 5 servers to meet the application requirements.
  • Testing out the application functionality.

As per the chart seen above we will build 5 machines/servers namely:

  1. Tomcat : With CentOs as the linux flavor, This will work as the server to host our application. Apache Tomcat is an open-source application server developed by the Apache Software Foundation. It is a widely used web server and servlet container for deploying and running Java-based web applications. Tomcat implements the Java Servlet, JavaServer Pages (JSP), and Java Expression Language (EL) technologies, providing a platform for developers to build and deploy dynamic web applications.
  2. RabbitMQ with CentOs linux flavor: This will work as our message queue server. RabbitMQ is an open-source message broker software that facilitates message-oriented middleware (MOM) and is designed to support various messaging patterns, including point-to-point, publish/subscribe, and request/reply. It provides a reliable and scalable platform for building distributed and decoupled applications.
  3. Memcached with CentOs flavor: This will be use for database caching. Memcached is a widely used open-source, high-performance, distributed memory object caching system. It is designed to speed up dynamic web applications by alleviating database load and reducing response times. Memcached stores key-value pairs in memory, allowing fast access to frequently used data.
  4. MySQL with CentOs flavor: This will be used as our database server. MySQL is an open-source relational database management system (RDBMS) that is widely used for building and managing various types of databases. It is part of the LAMP stack (Linux, Apache, MySQL, PHP/Python/Perl) and is known for its reliability, scalability, and ease of use. MySQL is owned by Oracle Corporation.
  5. Nginx with Ubuntu flavor: This will be used as our webserver, load balancing and reverse proxy. Nginx is a high-performance open-source web server and reverse proxy server. It is widely used to serve static content, handle SSL/TLS termination, and act as a load balancer for distributing incoming web traffic across multiple servers. Nginx is known for its efficiency, scalability, and low resource consumption.

Detailed overview of the stages in the project.

1. Cloning the repo to my/your computer/laptop

Cloning the project to my laptop

2. Building 5 virtual servers on the oracle virtual box using vagrant.

After doing the clone operation above change your directory to the vprofile-project as seen below:

listed files in the vprofile-project folder

Change the directory to the vagrant file and you should see the all the files listed inside:

listed files in the vagrant folder

Change the directory to the folder Manual_provisioning_WinMacIntel and you should see the vagrant file.

Vagrantfile listed above

From this location run the Vagrant up command and you should see the VMs starting to be created.

Virtual machines getting created on Oracle Virtual box

I encountered the error below and every time it comes up I run the vagrant up command again for the rest of the 4 machines until the complete setup is done:

Error encountered

After all the VMs have been provisioned with vagrant run the vagrant status and you will see all the VMs listed like below:

Listed VMs created on Oracle virtual box

3. Configuring the 5 servers to meet the application requirements.

While configuring the server it good practice to start from the back end server all the way to the front end that we way we can validate as we do the configuration

We will follow the order below:

  • MySQL(Database SVC)
  • Memcached(Database caching SVC)
  • RabbitMQ(Broker/Queue SVC)
  • Tomcat(Application SVC)
  • Nginx(Web server)

The set up:

Configuring MySql(Mariadb)

  1. Log in to db01 the Database server with the command vagrant ssh db01 and switch to the root user:
logging in db01

2. Verify Hosts entry, if entries missing update the it with IP and hostnames run the command : cat /etc/hosts

list of all servers in the /etc/hosts file

3. Update OS with latest patches run the command : yum update -y

updating OS with the latest packages

4.Set Repository run the command : yum install epel-release -y

setting repositories

3. Install Maria DB Package run the command: yum install git mariadb-server -y

Installing mariadb

4. Starting & enabling mariadb-server run the commands below;

  • systemctl start mariadb

After setting up the database server we can test it out :

a) RUN MySQL secure installation script runthe command below;

* mysql_secure_installation

NOTE: Set database root password, I will be using admin123 as password

Mariadb setup

b) Set DB name and users. Run the command below :

mysql -u root -padmin123
create database accounts;
grant all privileges on accounts.* TO 'admin'@'%' identified by 'admin123';
FLUSH PRIVILEGES;
exit;
mariadb setup complete

c) Download Source code & Initialize Database. Run the commands below:

git clone -b main https://github.com/msekandi/vprofile-project
cd vprofile-project
mysql -u root -padmin123 accounts < src/main/resources/db_backup.sql
mysql -u root -padmin123 accounts show tables;
exit;
source code downloaded and database initialized

d) Restart mariadb-server run:

systemctl restart mariadb

e) Starting the firewall and allowing the mariadb to access from port no. 3306 run the following commands.

systemctl start firewalld 
systemctl enable firewalld
firewall-cmd - get-active-zones
firewall-cmd - zone=public - add-port=3306/tcp - permanent
firewall-cmd - reload
systemctl restart mariadb
firewall started allowing mariadb access to port 3306

Configuring Memcache

  1. Login to the Memcache vm and switch to root user run the command:

vagrant ssh mc01

ssh in the mc01

2. Verify Hosts entry, if entries missing update the it with IP and hostnames run the command :

cat /etc/hosts

list of hosts

3. Update OS with latest patches run the following command:

yum update -y

updated OS with latest patches

4. Install, start & enable memcache on port 11211 run the following commands:

sudo dnf install epel-release -y
sudo dnf install memcached -y
sudo systemctl start memcached
sudo systemctl enable memcached
sudo systemctl status memcached
sed -i 's/127.0.0.1/0.0.0.0/g' /etc/sysconfig/memcached
sudo systemctl restart memcached

5. Starting the firewall and allowing the port 11211 to access memcache

firewall-cmd - add-port=11211/tcp 
firewall-cmd - runtime-to-permanent
firewall-cmd - add-port=11111/udp
firewall-cmd - runtime-to-permanent
sudo memcached -p 11211 -U 11111 -u memcached -d
firewall enabled and port 11211 enabled

Configuring RabbitMq

  1. Log in to rabbitmq VM and switch to the root user run the following command

vagrant ssh rmq01

ssh to rmq01

2. Verify Hosts entry, if entries missing update the it with IP and hostnames run the command :

cat /etc/hosts

list of hosts

3. Update OS with latest patches run the command:

yum update -y

updated OS and patches

4. Set EPEL Repository run the command below:

yum install epel-release -y

epel repository installed

5. Install Dependencies run the commands below:

sudo yum install wget -y
cd /tmp/
dnf -y install centos-release-rabbitmq-38
dnf - enablerepo=centos-rabbitmq-38 -y install rabbitmq-server
systemctl enable - now rabbitmq-server

6. Setup access to user test and make it admin run commands below:

 sudo sh -c 'echo "[{rabbit, [{loopback_users, []}]}]." > /etc/rabbitmq/rabbitmq.config'
sudo rabbitmqctl add_user test test
sudo rabbitmqctl set_user_tags test administrator
sudo systemctl restart rabbitmq-server

7. Starting the firewall and allowing the port 5672 to access rabbitmq run the commands below:

firewall-cmd --add-port=5672/tcp
firewall-cmd --runtime-to-permanent
sudo systemctl start rabbitmq-server
sudo systemctl enable rabbitmq-server
sudo systemctl status rabbitmq-server

Configuring Tomcat Server.

  1. Login to the tomcat VM and switch to root user run the command:

vagrant ssh app01

Tomcat server login

2. Verify Hosts entry, if entries missing update the it with IP and hostnames run the command :

cat /etc/hosts

list of hosts

3. Update OS with latest patches run the command:

yum update -y

Update OS with latest patches

4.Set Repository run the command:

yum install epel-release -y

repositories set

5. Install Dependencies run the commands:

dnf -y install java-11-openjdk java-11-openjdk-devel
dnf install git maven wget -y

6. Change dir to /tmp run the command:

cd /tmp/

7. Download and Tar Tomcat Package run the commands below:

wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.75/bin/apache-tomcat-9.0.75.tar.gz
tar xzvf apache-tomcat-9.0.75.tar.gz

7. Add tomcat user run the command:

useradd — home-dir /usr/local/tomcat — shell /sbin/nologin tomcat

8. Copy data to tomcat home directory run:

cp -r /tmp/apache-tomcat-9.0.75/* /usr/local/tomcat/

9 .Make tomcat user owner of tomcat home directory:

chown -R tomcat.tomcat /usr/local/tomcat

10. Setup systemctl command for tomcat start by Creating tomcat service file run:

vi /etc/systemd/system/tomcat.service

Update the file with below content:

systemctl file updated

11.Reload systemd files run the command:

systemctl daemon-reload

12. Start & Enable service run the commands:

systemctl start tomcat
systemctl enable tomcat

13. Enabling the firewall and allowing port 8080 to access the tomcat

 systemctl start firewalld
systemctl enable firewalld
firewall-cmd --get-active-zones
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload
fierwall enabled and port 8080 allowed

14. CODE BUILD & DEPLOY (app01)

Download Source code run command:

git clone -b main https://github.com/msekandi/vprofile-project

15. Update configuration run the commands:

cd vprofile-project
vi src/main/resources/application.properties

Update the file with the back end servers.

16. Build code run below command inside the repository (vprofile-project):

mvn install

Build starting
Build success

17. Deploy artifact run the commands:

systemctl stop tomcat
rm -rf /usr/local/tomcat/webapps/ROOT*
cp target/vprofile-v2.war /usr/local/tomcat/webapps/ROOT.war
systemctl start tomcat
chown tomcat.tomcat /usr/local/tomcat/webapps -R
systemctl restart tomcat
systemctl status tomcat
Deploying artifact.

Configuring and Setting up Nginx server.

  1. Login to the Nginx VM and switch ro root user run the commands below:

vagrant ssh web01

sudo -i

ssh in web01 and switch to root user

2. Verify Hosts entry, if entries missing update the it with IP and hostnames run the commands:

cat /etc/hosts

list of hosts

3. Update OS with latest patches

apt update
apt upgrade
OS updated with the latest Patches

4. Install nginx runthe command:

apt install nginx -y

5. Create Nginx conf file run the commands:

vi /etc/nginx/sites-available/vproapp

vproapp file created.

6.Remove default nginx conf run the command below:

rm -rf /etc/nginx/sites-enabled/default

7. Create link to activate website run the command:

ln -s /etc/nginx/sites-available/vproapp /etc/nginx/sites-enabled/vproapp

8.Restart Nginx run:

systemctl restart nginx

soft link to start the website

--

--

Musa Sekandi

DevOps Engineer/Cloud Engineer AWS CERTIFIED and LINUX(RHEL) Certified