WeSeong Log in
← Back to posts
WEB

3-tier architecture, using Nginx (for web serving) and Tomcat (as a Web Application Server)

3-tier architecture, using Nginx (for web serving) and Tomcat (as a Web Application Server)

Preview of the following text... 🫠 To achieve the full potential of a 3-tier architecture, I plan to integrate a database server with both the existing Nginx and Tomcat servers.


I. Overview

Let's explore the 3-tier architecture. Typically, a 3-tier architecture consists of a web server, a web application server (WAS), and a database server. While this basic structure remains consistent when providing services, the specific configuration and environment settings can vary depending on the architecture. As a practical exercise, we will set up and operate an Nginx server. Once the Nginx server is functioning correctly, we will proceed with installing the WAS server based on Tomcat. Finally, we will verify that the WAS server is connected when accessing the web server.



II. 3-Tier Architecture


https://docs.aws.amazon.com/ko_kr/whitepapers/latest/serverless-multi-tier-architectures-api-gateway-lambda/three-tier-architecture-overview.html

3-Tier architectureAs you can see in the image above, when providing a single service A system with three serversThis refers to the front-end development work that is directly presented to the client. Web serverWhen a client uses the service, it can be programmed to perform specific actions or to connect to a database and display the desired information to the client. WAS serverThere is a WAS server. The WAS server is Database server The database can be queried, data can be inserted, modified, or deleted, and other operations can be performed based on this foundation. The database server is the system that stores the data. While this fundamental structure remains unchanged when providing services, ArchitectureThe specific details of which policy will be implemented may vary depending on the circumstances.


ArchitectureArchitecture refers to the process of defining and designing a system, including its components, their interactions, and their relationship with the external environment. It also involves specifying the level of support provided for designing and implementing the system's components. To put it simply, when creating a website, architecture involves deciding on the design, determining what personal information to collect during registration, specifying the level of access for different user groups (e.g., regular members, non-members) on specific forums, and determining who can leave reviews and whether non-members can place orders. In essence, architecture is about how the overall service is designed and presented to achieve the specific goals of a particular website. There are many different types of architecture, and it's a vast field that requires extensive study. If you're interested in learning more, I recommend searching for relevant information.


In other words, when a user enters a Naver address and accesses it, the web server will… Access via IP address(As we simply www.naver.com Even if the user is prompted to enter "이라고" (meaning "said"), this happens in a hidden area. DNS serverThis involves accessing the Neerber web server through the provided interface. Since the web server acts as an intermediary, connecting to the WAS server, it provides Various servicesWhenever we request them, the WAS server provides the necessary services. For example, when we log in, the WAS server provides the login-related services it has implemented. Similarly, when we access news, shopping, or sports sections on Naver, the WEB server retrieves the corresponding services implemented on the WAS server. Ultimately, the WAS server functions as the actual service provider. However, the extensive data related to user information when logging in, various information, news, and shopping products that appear during searches, are not stored on the WAS server. Database serverIt has this feature, so the WAS server is connected to the database server. Retrieve and obtain the data relevant to the service being provided. For example, when a blog post is created, the WAS server can request the database server to store, modify, and delete the new blog post as data. The database server then performs the actual storage, modification, and deletion, and stores the data. Think of the database server as the place where all the data we encounter on our website is stored.


※ This is a simplified explanation, and there are many more details to consider. For example, how to configure network architecture and firewalls, and how to connect multiple Web Application Server (WAS) and Web Server instances. I plan to learn and add more details on these topics in the future.



Ⅲ. Nginx

Before starting the practical training, it's helpful to familiarize yourself with some basic commands.

Command

Function description

sudo apt install nginx

Install the package with administrator privileges.

sudo apt remove nginx

Delete a package installed with administrator privileges.

sudo systemctl stop nginx (on the server)

Shut down the server running with administrator privileges.

sudo systemctl start nginx

Run the server with administrator privileges.

sudo systemctl restart nginx

Restart the server using administrator privileges.

sudo systemctl enable nginx

As an administrator, register the server as a startup program (similar to Windows' startup programs).

sudo systemctl disable nginx

Remove the server from the startup program using administrator privileges.

These are system commands related to installing, deleting, starting, restarting, and stopping servers. Knowing these commands can be very helpful.

I plan to install Nginx, which is the most commonly used web server, and practice setting up a 3-tier architecture. While Tomcat is also available, each has its own advantages and disadvantages. Briefly, When it comes to handling high traffic, Nginx generally performs better than Tomcat. Tomcat is Due to the numerous sub-projects, it offers significant advantages in terms of functionality and module expansion.


  1. Install the Nginx server.

  • sudo apt update

  • sudo apt install nginx



※ Once the nginx server is installed, it is automatically registered and starts up using the systemctl command, allowing it to be controlled. You can verify this by running "netstat -anp | grep nginx".


2. Trying to access the Nginx server.

When considering the file structure, `/etc` typically contains configuration files, while `/var` usually holds data files or log files. This is also the case with Nginx servers. /etc/nginxIt contains a configuration file. /var/www/htmlThis is where the source code files for the implementation are located.


You can find detailed information about the configuration file by navigating to /sites-available/default.


I plan to review this section again when I need to register the necessary settings for integration with the WAS server later.


/var/www/ html Now index.html I plan to create a file and then actually connect to a web server. After accessing this path, I will use the `vi` editor to modify the `index. html I'll create a template and then fill it in with basic information.


I've written it this way, and if there are any tips for doing so, vim Once the package is installed, you can use the vi editor much more easily. Or

cat > 파일명
cat >> 파일명

Ways to use when viewing file content "cat" Using the command to...> You can create a text file with the specified filename. If the filename already exists, you will be prompted to either overwrite it or choose a different name.>"In this case, the content will be overwritten, followed by the following...">>"In this case, the content is expanded."


Now, it's time to check if the content of the `index.html` file is displayed correctly when accessing the Nginx server. Using the command 'netstat -anp | grep nginx' mentioned earlier, we can verify that the server is LISTEN After confirming the status, if it's correct, I immediately try to log in.


'ifconfig'You can access the website using the IP address provided. Once you're there,



You can clearly see the results!


* Tip: HTTP Response I would like to share some status codes that are relevant. Please note that this is a simplified overview, and I recommend referring to more detailed resources for specific information.

Status code

Status description

100th

The server is currently waiting for a request from the client, which has already sent its request.

200 series

This is displayed when the client and server are communicating successfully.

300 series

The server requests further action (redirection) to complete the request, indicating that additional steps are needed from the client.

400 series

This error occurs when a client is unable to access the requested resource on the server.

500 series

This means that there is an error within the server itself.



Ⅳ. Tomcat

Next, we will install Tomcat. WAS serverWe plan to begin a practical exercise using Tomcat. While setting up Tomcat might seem a bit complicated at first, it's actually not that difficult in practice.

Apache Tomcat® - Apache Tomcat 9 Software Downloads


First, let's start with... Extracting a tar.gz file from a Tomcat web page.You can download the file. The latest version is 9.0.82, but other versions are also available for download on the website. The path to the file you need to install is in the program installation directory. /usr/local Simply download the file using the 'wget' command. Then, navigate to the /usr/local directory and extract the downloaded Tomcat file.


It will be saved in this format. You can either keep the filename as it is after unzipping, or change it freely.


If you were to open it up, you would find something like this inside. /confThis refers to the directory related to settings. /libThe library will house the database server, and a connector for connecting to the database server will be installed in this location. /web applications The directory is where you register files related to web services, such as JSP, CSS, and HTML. Then, you install the Java language.

sudo apt install openjdk-17-jdk

I will install Java 17. Recently, Spring Framework also supports versions after 17, and I believe that most work is being done on version 17 since the support period for version 8 is nearing its end. Now, to check if the Tomcat server is working correctly, I will... /usr/local/apache-tomcat-9.0.82/bin Navigate to the specified path. startup.sh Simply run the script file. Since Tomcat was not installed using the 'apt' command, it will not start automatically.


Server shutdown shutdown.sh Simply run the script file.


The system is functioning normally, and the server is LISTEN This can be verified. For Tomcat, the default port is 8080This is important to know.


Now, verify that the Tomcat server is accessible via its IP address and port 8080, and that it is functioning correctly. Then, proceed to connect it to the web server. Nginx serverOf /etc/nginx/sites-available/default I will modify the configuration file.


By adding `proxy_pass` and `index.jsp`, 'sudo systemctl restart nginx'Once you have the credentials, you can access the system using the web server IP address.



You can clearly see that it is properly connected to the Tomcat (Web Application Server) server!



Review

I found the process of setting up and connecting the web server and WAS server to be relatively straightforward. However, I anticipate that setting up the database server will be more challenging, so I plan to approach it by rebuilding the entire system from scratch, as I did in the previous post. 😄