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.
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.
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.
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:
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:
Now insert the following lines at the end of this file:
export CATALINA_HOME=/opt/tomcat/apache-tomcat-9.0.43
Now save the file and run the below command to apply these changes:
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 $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:
Now put the following content inside it:
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
We are now ready to use the tomcat service. Start the service and enable it to persist the reboot.
# systemctl enable tomcat.service
Check the status of service; it should show an active running status:
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/”:
Note: Again, replace the bolded text with the path of your Tomcat installation.
2. Put the following content inside it:
<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.