Apache Tomcat

How to install and configure Apache Tomcat on Fedora Linux

Apache Tomcat is one of the most widely used web application servers in the world. It is an open-source project of Apache Software Foundation. It is written in Java. It is used for implementing servlet containers and Java Server Pages(JSP) in Java.

Earlier, Tomcat required a high level of expertise for configuring and administering its services, as only advanced users and developers were able to work it out. With Tomcat’s GUI installer, it has become just a matter of a few commands to administer the server as a system service.

What will we cover

This tutorial will show you how to install apache Tomcat and use it to deploy a basic JSP program. Tomcat requires JRE (Java Runtime Environment) for running java web applications. In case if you are developing a Java application, you will need a full JDK application installed. For this, we will cover the guide only with the JRE only.

Prerequisites

You need to be familiar with the Java and basic Linux command to understand this tutorial better. We assume that you have already installed the JRE (Java Runtime Environment) on your system. You also need to have root privileges for installing Apache Tomcat.

Downloading Tomcat

1. To download the Apache Tomcat, visit the Apache Tomcat home page, where you will see different available versions. Alternatively, you can also use the wget command to get the file. For this guide, we are using Tomcat 9.

# wget https://mirrors.estointernet.in/apache/tomcat/tomcat-9/v9.0.43/bin/apache-tomcat-9.0.43.tar.gz

2. If you prefer, you can download Tomcat from the homepage. This is shown below:

Extracting The Binary Archive

1. Once the archive binary file is downloaded, you need to copy it to the directory where you want to install the Tomcat server and extract the file there. For example, we will extract the Tomcat tar file into /opt/tomcat. For this, we first need to create a directory ‘tomcat’ inside /opt. Use the following command to create a directory.

# mkdir /opt/tomcat
# tar xzf apache-tomcat-9.0.43.tar.gz -C /opt/tomcat

Creating a user and group for Tomcat

We will create a non-root user and group for running the Apache Tomcat server. Use the command below for creating the user and group.

# useradd -r tomcat

The above command will also add a ‘tomcat’ group.

Now we will change the ownership of the tomcat directory to the Tomcat user with the command:

# chown -R tomcat:tomcat /opt/tomcat

Setting Environment Variables

Tomcat requires certain environment variables to be set for running the startup scripts. Let’s see those variables:

a. CATALINA_HOME: The location of this environment variable is the root directory of Tomcat’s “binary” distribution. In our case, this root directory is /opt/tomcat/apache-tomcat-9.0.43

b. JRE_HOME or JAVA_HOME: These environment variables specify the location of Java Runtime Environment and a JDK location, respectively. If you are specifying both JRE_HOME and JAVA_HOME, then JRE_HOME will be used by default.

To set these variables, open the following file:

# vi /etc/profile

Now insert the following lines at the end of this file:

export JRE_HOME=/usr/java/jre1.8.0_281-amd64/bin/java
export CATALINA_HOME=/opt/tomcat/apache-tomcat-9.0.43

Now save the file and run the below command to apply these changes:

# . /etc/profile

To check if these variables are correctly set, check if the output of the below command is the same as the value for JRE_HOME and CATALINA_HOME:

# echo $JRE_HOME
# echo $CATALINA_HOME

See the below pictures for reference:

Creating Tomcat service

Now we will create a simple systemd unit file to define our Tomcat service. Create the service with the following instructions:

1. Create a file tomcat.service:

# vim /etc/systemd/system/tomcat.service

Now put the following content inside it:

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

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

Environment=CATALINA_PID=/opt/tomcat/apache-tomcat-9.0.43/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat/apache-tomcat-9.0.43
Environment=CATALINA_BASE=/opt/tomcat/apache-tomcat-9.0.43

ExecStart=/opt/tomcat/apache-tomcat-9.0.43/bin/catalina.sh start
ExecStop=/opt/tomcat/apache-tomcat-9.0.43/bin/catalina.sh stop

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

Note: Please replace the bolded text with the path of your Tomcat installation.

Now save the file and reload the systemd configuration with the following command
to apply the changes

# systemctl daemon-reload

We are now ready to use the tomcat service. Start the service and enable it to persist the reboot.

# systemctl start tomcat.service
# systemctl enable tomcat.service

Check the status of service; it should show an active running status:

# systemctl status tomcat.service

All the above steps are shown below:

Accessing Tomcat in Browser

Now we are ready to test if our tomcat server is correctly installed or not. To check this, open your web browser and browse the addresses:

http://localohost:8080
or
http://system_IP_addr:8080 (To see your system IP, use the ip addr command.)

You would see the default homepage of Apache Tomcat. The following screenshot shows the tomcat homepage:

Deploying a simple JSP application

Now we will deploy a basic JSP application with a Tomcat server.

1. Create a basic JSP application called ‘test.jsp’ inside the directory “/opt/tomcat/apache-tomcat-9.0.43/webapps/ROOT/”:

# nano /opt/tomcat/apache-tomcat-9.0.43/webapps/ROOT/test.jsp

Note: Again, replace the bolded text with the path of your Tomcat installation.

2. Put the following content inside it:

<html>
<head><title> JSP Page</title></head>
<body>
This is a JSP Page from LinuxHint!<br/>
<%
out.println("Your System IP address is: " + request.getRemoteAddr());
%>
</body>
</html>

3. Now again, open the web browser and browse the following address:

http://localhost:8080/test.jsp

This time you should see the following web page:

Conclusion

This tutorial shows how we can install Apache Tomcat from an archive binary file on Fedora Linux. We have learned to install a JSP application with tomcat.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.