Tomcat

Tomcat

What is tomcat and why do we need tomcat?

Tomcat is an application server where we can deploy our war( web archive ) file, with maven we can generate war, jar, and ear but Tomcat supports war files only, we can run jar with the help of,

java -jar jarfilename

So with Tomcat, we can only run war files, tomcat is an application server we can use to host dynamic websites, first let’s see what the difference is between a web server and an app server.

Web Server :

It is used to host static web content like HTML, CSS, JS, XML, and Images and we mainly use web servers for load-balancing purposes., nginx is an example of a web server. We will define reverse proxy rules in the nginx like these - if the request comes to port 8080 we can use it to route to a specific server.

How Reverse Proxy Works in NGINX

  • Port Mapping: In NGINX, you can define reverse proxy rules to route incoming requests to the appropriate backend server. For instance:

    • If a request hits port 8080 with the context /hello, it can be routed to the application's backend server.

    • To the end-user, only port 80 or 443 (for HTTPS) is exposed, masking internal server details.

Apache HTTP is also a web server. We need to install httpd in our server and start it and we can have an index.html file in /var/www/html folder and we can access it with the help of public IP.,

In an Ubuntu machine, we use - sudo apt install apache2 -y, to install HTTP in our server to check the status we will use the command - service apache2 status

To access we use the public IP of our server, this is the default page that it will display.

To host our own webpage we can create index.html and put it in /var/www/html directory

Like this, we can have our content on the index.html page. In case we have hello.html then we need to access the context also., 52.91.204.137/hello.html

Application Server :

It is used to host dynamic websites and it is suitable for complex applications. Tomcat is an example of an application server.

Tomcat Directory Structure :

Once we install Tomcat we will have these directories,

bin - it contains binary files like startup.sh, startup.bat, catalina.sh, catalina.bat, version.sh, version.bat

conf - it contains server.xml and tomcat-users.xml

webapps - applications are stored and deployed into this directory. 5 default apps are present here.

logs - contains logs, By default it will be empty, after starting tomcat server it will generate. Manager.log, Hostmanager.log, Catalina.log, and localhost.log

work - after deploying the applications in the webapps directory, those apps generate some files and get stored in the work directory, these files are used by the applications.

temp - contains temporary files.

To install the tomcat we need Java to be installed first so we will install Java and then we will install tomcat. After installing tomcat we need to come to bin directory and execute sh startup.sh., rather than coming to the bin directory we can create a link and run from any path. Give execute permissions with the help of chmod command.

After starting the tomcat server we need to access it with the help of public IP and port 8080,

On the homepage we can see Manager App and Host Manager., to access it we need to create a username and password but before that, we need to make a change in the manager directory present in the webapps.

In the context.xml we need to comment out the valve tag and we were able to access the manager

Similarly, we need to make changes to the host-manager directory also. Now we will create a user in tomcat-users.xml file.,

We have given roles as manager-gui which is used to access server status and manager application, and admin-gui which is used to access host manager.

So we can deploy our war file here and we can access the application with the application context path which is the package name.

http://< public-ip >:8080/< context-path >

We have options such as start - to start the application, stop - to stop the application, and Reload - In case we made any modifications to our application we can simply reload.

That’s all about the Tomcat application server.