How To Install Apache Tomcat on Rocky Linux 9

This guide will teach you How To Install and Configure Apache Tomcat on Rocky Linux 9.

Apache Tomcat it’s an open-source Java servlet and Java Server Page container that lets developers implement an array of enterprise Java applications. Tomcat also runs an HTTP web server environment in which Java code can run.

Steps To Install and Configure Apache Tomcat on Rocky Linux 9

To complete this guide, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide on Initial Server Setup with Rocky Linux 9.

Install Java on Rocky Linux 9

To install tomcat, you need to have Java installed on your server.

First, update your local package index with the following command:

sudo dnf update

Then, use the command below to install Java on Rocky Linux 9:

sudo dnf install java -y

You can verify your Java installation by checking its version:

java --version

In your output you will see the following:

Output
openjdk 11.0.17 2022-10-18 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.17.0.8-2.el9_0) (build 11.0.17+8-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.17.0.8-2.el9_0) (build 11.0.17+8-LTS, mixed mode, sharing)

Install Tomcat 10 on Rocky Linux 9

At this point, to install the latest Tomcat on your server, you need to visit the Tomcat downloads page.

Download Tomcat

Now check for the latest release and download it to your server by using the wget command:

sudo wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.1.1/bin/apache-tomcat-10.1.1.tar.gz

When your download is completed, extract it by using the command below:

sudo tar -xvf apache-tomcat-10*.tar.gz

Next, move your file to the /usr/local/tomcat directory:

sudo mv apache-tomcat-10.1.1 /usr/local/tomcat

Create Tomcat User

At this point, you need to run the tomcat service as a normal user. To do this, create a Tomcat user and group by using the commands below:

# sudo groupadd tomcat 
# sudo useradd -d /usr/local/tomcat -r -s /bin/false -g tomcat tomcat

Then, set the correct ownership of the directory to the tomcat user by using the following command:

sudo chown -R tomcat:tomcat /usr/local/tomcat/

You can verify the change of ownership using the following command:

sudo ls -l /usr/local/tomcat
Output
drwxr-x--- 2 tomcat tomcat  4096 Nov  3 04:38 bin
-rw-r----- 1 tomcat tomcat 20123 Oct  3 08:42 BUILDING.txt
drwx------ 2 tomcat tomcat  4096 Oct  3 08:42 conf
-rw-r----- 1 tomcat tomcat  6210 Oct  3 08:42 CONTRIBUTING.md
drwxr-x--- 2 tomcat tomcat  4096 Nov  3 04:38 lib
-rw-r----- 1 tomcat tomcat 60393 Oct  3 08:42 LICENSE
drwxr-x--- 2 tomcat tomcat  4096 Oct  3 08:42 logs
-rw-r----- 1 tomcat tomcat  2333 Oct  3 08:42 NOTICE
-rw-r----- 1 tomcat tomcat  3398 Oct  3 08:42 README.md
-rw-r----- 1 tomcat tomcat  6775 Oct  3 08:42 RELEASE-NOTES
-rw-r----- 1 tomcat tomcat 16073 Oct  3 08:42 RUNNING.txt
drwxr-x--- 2 tomcat tomcat  4096 Nov  3 04:38 temp
drwxr-x--- 7 tomcat tomcat  4096 Oct  3 08:42 webapps
drwxr-x--- 2 tomcat tomcat  4096 Oct  3 08:42 work

Configure Apache Tomcat on Rocky Linux 9

At this point, you need to configure the CATALINA_HOME Environment variable required to run the tomcat server.

To do this, run the following commands:

# echo "export CATALINA_HOME="/usr/local/tomcat"" >> ~/.bashrc 
# source ~/.bashrc

In order to access Tomcat Web Management Interface, you need to create a user to access the manager-gui and admin-gui.

Edit Tomcat Users File

Here you need to open the tomcat user file on Rocky Linux 9 by using your favorite text editor, here we use vi:

sudo vi /usr/local/tomcat/conf/tomcat-users.xml

Add the following lines just above the </tomcat-users> tag at the end of the file.

<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="admin-pass" roles="manager-gui,admin-gui"/>

Remember to update the ‘password’ field with your preferred one.

Save and exit the file, when you are done.

At this point, you need to allow the tomcat manager and host manager to be accessible.

Edit Tomcat Host Manager File

Open the host manager file with your favorite text editor, here we use vi:

sudo vi /usr/local/tomcat/webapps/host-manager/META-INF/context.xml

Comment on the following line as shown below:

<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

When you are done, save and close the file.

Edit Tomcat Manager App

Open the tomcat manager app:

sudo vi /usr/local/tomcat/webapps/manager/META-INF/context.xml

Comment on the following line as shown below:

<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

When you are done, save and close the file.

Run Tomcat as a service on Rocky Linux 9

At this point, you can use the following command to start your tomcat service:

# cd /usr/local/tomcat 
# sudo ./bin/startup.sh

You will get the following output:

Output
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.

Configure Firewall for Tomcat

At this point, we assumed that you have enabled firewalld. So you need to allow port 8080 for Tomcat through the firewall:

sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent

Reload the firewall to apply the new rules:

sudo firewall-cmd --reload

Access the Apache Tomcat web Interface

In your web browser type your server’s IP address followed by 8080:

http://server-IP-adress:8080

You will see:

Apache tomcat screen

To access the Manager GUI click on Manager App. Then you will be presented will a login prompt. Enter the user and password that you have provided before.

Then, you will see your Manager App screen:

Tomcat web application manager

For the Host manager GUI, you follow the same process and the page is as shown below.

tomcat virtual host manager

That’s it, you are done.

Conclusion

At this point, you have learned to Install and Configure Apache Tomcat on Rocky Linux 9.

Hope you enjoy it.

You may be like these articles:

How To Install XRDP on Rocky Linux 9

How To Install XAMPP on Rocky Linux 9

Install and Configure Caddy on AlmaLinux 8

Install and Configure CSF Firewall on AlmaLinux 8

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!