Skip to main content

Installing Tomcat

Hands-On Lab

 

Photo of

Training Architect

Length

00:45:00

Difficulty

Beginner

Welcome to this hands-on lab for installing Tomcat 9 onto Red Hat Enterprise 8.

Apache's Tomcat is a Java based application platform built around deploying and managing Java Web Applications.

Tomcat is cross-platform, working on anything that supports Java, but this hands-on lab focuses on a Red Hat 8 deployment. In it we will configure the system and install Tomcat version 9.

Once we have Tomcat installed, we will configure it with a user and then configure it so we can access the management web console via the internet.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Installing Tomcat

Introduction

Welcome to this hands-on lab for installing Tomcat 9 onto Red Hat Enterprise 8.

Apache's Tomcat is a Java based application platform built around deploying and managing Java Web Applications.

Tomcat is cross-platform, working on anything that supports Java, but this hands-on lab focuses on a Red Hat 8 deployment. In it we will configure the system and install Tomcat version 9.

Once we have Tomcat installed, we will configure it with a user and then configure it so we can access the management web console via the internet.

The Scenario

Your company is exploring using Java based Web applications and has tasked you with setting up Tomcat 9 using a Red Hat Enterprise 8 server.

Your company already uses Red Hat and many in the Development team are familiar with Tomcat, but would like to test running it on Red Hat Enterprise 8.

Your task is to deploy Tomcat 9 on the provided server. The Java (OpenJDK) will also need to be installed prior to the Tomcat setup.

NOTE: It may take a few minutes after the hands-on lab has started before you are able to log into the server. This is fine and will not affect the performance of the tasks for the hands-on lab.

Logging In

Use the credentials provided on the hands-on lab overview page, and log in as cloud_user.

Install Java 11 OpenJDK

You need to install the java-11-openjdk-devel package:

sudo dnf install java-11-openjdk-devel

Once it's installed, check the version of Java to ensure it has installed properly and is set as the default for the system.

Obtain Tomcat version 9 from the Apache Tomcat Website

Go to the webpage to get the latest version of Tomcat 9:

https://tomcat.apache.org/download-90.cgi

Go to the Binary Distributions section.
Under the Core section, right click on the tar.gz, then select Copy Link Location.

Now use wget to download the file:

wget  USE-THE-URL-YOU-COPIED-EARLIER

Install and Configure Tomcat, Create the User and Service, Then Start It and Ensure It Starts on a System Reboot

You need to expand the compressed Tomcat files into the location it will be put.

Change to the root user (or add 'sudo' at the beginning of the commands that follow)

sudo su -

Change to the location we will use for the tomcat files.

cd /usr/local

Extract the downloaded file to the current location. Note: replace THE-TOMCAT-FILE-YOU-COPIED-EARLIER with the correct file that was downloaded.

tar -xvf /home/cloud_user/THE-TOMCAT-FILE-YOU-COPIED-COPIED-EARLIER

Change the name of the Tomcat folder to tomcat9. Note the version you get may be newer than this one. Change the name as needed:

mv apache-tomcat-9.0.31 tomcat9

Add the tomcat user as a system account:

useradd -r tomcat

Change the permissions of the tomcat9 folder so the tomcat user can use it:

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

Create the tomcat service. Use an editor (like Vim) to create a file /etc/systemd/system/tomcat.service:

vim /etc/systemd/system/tomcat.service

Add the following contents (and note that if you copy and paste whats below, ensure the lines in your file do not have leading spaces):

[Unit]
Description=Apache Tomcat
After=syslog.target network.target

[Service]
Type=forking
User=tomcat
Group=tomcat

Environment=CATALINA_PID=/usr/local/tomcat9/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat9
Environment=CATALINA_BASE=/usr/local/tomcat9

ExecStart=/usr/local/tomcat9/bin/catalina.sh start
ExecStop=/usr/local/tomcat9/bin/catalina.sh stop

RestartSec=12
Restart=always
[Install]
WantedBy=multi-user.target

Next we need to have the system recognize there is a new service. We can use the daemon-reload option for systemctl:

systemctl daemon-reload

Now we can start the service, set it to restart on system boot, and check if it's currently running:

systemctl start tomcat.service
systemctl enable tomcat.service
systemctl status tomcat.service

Check that Tomcat works by going to your server's public IP address, on port 8080, in a web browser:

http://YOUR_SERVER_IP:8080

Add an admin User for Tomcat

To add an admin user for Tomcat, you need to edit the tomcat-users.xml file:

cd /usr/local/tomcat9/
vim conf/tomcat-users.xml

Go to the bottom of the file, and put the following just before the </tomcat-users> end block. Don't forget to change the YOURPASSWORDHERE to be your own password, whatever you like:

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

Modify Tomcat so You Can Log in Remotely

The next step is to allow access to the management web pages from the internet.

Edit the webapps/manager/META-INF/context.xml file:

vim webapps/manager/META-INF/context.xml

Get to the following line:

allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1" />

Change it to this:

allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1|.*" />

Restart Tomcat and Check Remote Connectivity

The next task is to restart the Tomcat and ensure it is accessible, then try logging in:

systemctl restart tomcat

Go back to the browser you had open to the public IP address of the lab server on port 8080:

http://YOURSERVERIP:8080

Click on Server Status, then log in with the username and password you completed on Step 5: Add an admin User for Tomcat

You should be able to log in and manage the Tomcat installation.

Conclusion

If you get in, then you're done. You have a working Tomcat install. Congratulations!