Apache Tomcat is an open source Java server.
You need Tomcat when you want to deploy and execute a Java application that is written in any of the Java technologies including Java Servlet, JSP, etc.
This tutorial explains how to install the latest Apache Tomcat version 9.x on Linux platform
Pre-req: Install Java 8
For Apache Tomcat 9 to be installed and configured properly, you need to have Java version 8 installed on your system.
To verify the java version on your system, execute the following.
$ java -version java version "1.8.0_131" Java(TM) SE Runtime Environment (build 1.8.0_131-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
If you don’t have Java JRE 8 installed, then follow these steps: How to Install Java 8 JRE on Linux
Create tomcat User
First, as root, create a user called tomcat and assign a password as shown below.
adduser tomcat passwd tomcat
Next, su to this newly created tomcat user.
su - tomcat
Download Apache Tomcat 9
Go to Apache Tomcat 9 Download page.
Under Core, click on “tar.gz” link to download the tar.gz version of the latest tomcat.
In this example, we are downloading this file: apache-tomcat-9.0.0.M21.tar.gz
Install Apache Tomcat
First, untar the tar.gz file as shown below using tar command
tar xvfz apache-tomcat-9.0.0.M21.tar.gz
This will create a directory apache-tomcat with the version number in it. cd to this new directory.
Apache Tomcat 9 Directories
You’ll see the following files and directories under this apache-tomcat directory.
$ ls -altr drwxr-x---. 2 tomcat tomcat 6 May 4 22:42 work drwxr-x---. 2 tomcat tomcat 6 May 4 22:42 logs drwxr-x---. 7 tomcat tomcat 76 May 4 22:43 webapps -rw-r-----. 1 tomcat tomcat 15946 May 4 22:45 RUNNING.txt -rw-r-----. 1 tomcat tomcat 6709 May 4 22:45 RELEASE-NOTES -rw-r-----. 1 tomcat tomcat 1804 May 4 22:45 NOTICE -rw-r-----. 1 tomcat tomcat 57092 May 4 22:45 LICENSE drwx------. 2 tomcat tomcat 4096 May 4 22:45 conf drwxr-x---. 2 tomcat tomcat 29 Jun 1 17:25 temp drwxr-x---. 2 tomcat tomcat 4096 Jun 1 17:25 lib drwxr-x---. 2 tomcat tomcat 4096 Jun 1 17:25 bin
In the above:
- bin – This contains all the binary files and shell scripts required to start the tomcat, set the environment variables, etc.
- conf – This is the tomcat configuration directory where all the config files are located. The main tomcat server config file is server.xml which is located under this directory.
- lib – Contains library files and classes that are required for the tomcat server
- logs – Contains the log and output files of tomcat (i.e catalina.out, etc.)
- webapps – This is where you’ll deploy your java web application. Any application deployed under this directory will be automatically loaded by the Apache tomcat. For example, you can deploy birt-viewer on tomcat.
- work – This is used as a temp working directory for your web apps.
- temp – This is used by the JVM, which will create any required temp files that are specified by java.io.tmpdir.
Set Apache Tomcat CATALINA_HOME
The home environment variable that is used inside the Apache tomcat script is called as CATALINA_HOME.
Catalina refers to Tomcat.
Set this variable to the full directory of the apache tomcat that we extracted earlier as shown below.
Apart from setting it on the command line, make sure you add the above line to tomcat’s bash_profile also as shown below. This will make sure this variable is set every time you login as tomcat user.
$ vi ~/.bash_profile export CATALINA_HOME=/home/tomcat/apache-tomcat-9.0.0.M21
Log-out and login as tomcat user to verify that this environment variable is set properly.
$ set | grep CATALINA CATALINA_HOME=/home/tomcat/apache-tomcat-9.0.0.M21
Startup Apache Tomcat
To start Apache tomcat, we’ll use the script called catalina.sh that is located in the bin directory of CATALINA_HOME.
So, execute catalina.sh start as shown below to start Apache tomcat on your system.
$ $CATALINA_HOME/bin/catalina.sh start
Use grep command to verify whether the tomcat java process is started and running in the background as shown below.
$ ps -ef | grep -i tomcat root 3412 2045 0 17:27 pts/0 00:00:00 su - tomcat tomcat 3413 3412 0 17:27 pts/0 00:00:00 -bash tomcat 3463 1 1 17:30 pts/0 00:00:02 //bin/java -Djava.util.logging.config.file=/home/tomcat/apache-tomcat-9.0.0.M21/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -classpath /home/tomcat/apache-tomcat-9.0.0.M21/bin/bootstrap.jar:/home/tomcat/apache-tomcat-9.0.0.M21/bin/tomcat-juli.jar -Dcatalina.base=/home/tomcat/apache-tomcat-9.0.0.M21 -Dcatalina.home=/home/tomcat/apache-tomcat-9.0.0.M21 -Djava.io.tmpdir=/home/tomcat/apache-tomcat-9.0.0.M21/temp org.apache.catalina.startup.Bootstrap start
Note: If it didn’t start, then you might have to set the JAVA_HOME location properly as shown below.
View Tomcat Log files
On the Tomcat is started, you can view the log files to make sure whether tomcat started properly. This will also tell you whether all your web applications are deployed without any issues.
The log file is catalina.out as shown below. This shows partial output of catalina.out
cd $CATALINA_HOME/logs $ more catalina.out This is the partial output from the top of the catalina.out file. All of these line start with timestamp followed by "INFO [main] org.apache.catalina.startup.VersionLoggerListener.log", which is not shown here. $ more catalina.out Server version: Apache Tomcat/9.0.0.M21 Server built: May 4 2017 22:42:36 UTC Server number: 18.104.22.168 OS Name: Linux OS Version: 3.10.0-327.10.1.el7.x86_64 Architecture: amd64 Java Home: /usr/java/jre1.8.0_131 JVM Version: 1.8.0_131-b11 JVM Vendor: Oracle Corporation CATALINA_BASE: /home/tomcat/apache-tomcat-9.0.0.M21 CATALINA_HOME: /home/tomcat/apache-tomcat-9.0.0.M21
The following is the last few lines of the above catalina.out log file where it shows that the tomcat server started properly without any issues.
INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/home/tomcat/apache-tomcat-9.0.0.M21/webapps/manager] has finished in  ms INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"] INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 18181 ms
Stop Apache Tomcat
To sop the Apache Tomcat, use the same catalina.sh script with stop parameter as shown below.
$ $CATALINA_HOME/bin/catalina.sh stop Using CATALINA_BASE: /home/tomcat/apache-tomcat-9.0.0.M21 Using CATALINA_HOME: /home/tomcat/apache-tomcat-9.0.0.M21 Using CATALINA_TMPDIR: /home/tomcat/apache-tomcat-9.0.0.M21/temp Using JRE_HOME: / Using CLASSPATH: /home/tomcat/apache-tomcat-9.0.0.M21/bin/bootstrap.jar:/home/tomcat/apache-tomcat-9.0.0.M21/bin/tomcat-juli.jar
After “catalina.sh stop”, you will not see any java tomcat process running in the background as shown below.
$ ps -ef | grep -i tomcat root 3412 2045 0 17:27 pts/0 00:00:00 su - tomcat tomcat 3413 3412 0 17:27 pts/0 00:00:00 -bash tomcat 3592 3413 0 17:42 pts/0 00:00:00 ps -ef tomcat 3593 3413 0 17:42 pts/0 00:00:00 grep --color=auto -i tomcat
Instead of using the catalina.sh script, you can also use the shutdown.sh script as shown below. This does the same thing.
The following is the output from catalina.out log file which shows that the tomcat server is properly shutdown without any issues. All of these lines start with a timestamp at the beginning, which is not shown here.
INFO [main] org.apache.catalina.core.StandardServer.await A valid shutdown command was received via the shutdown port. Stopping the Server instance. INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"] INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"] INFO [main] org.apache.catalina.core.StandardService.stopInternal Stopping service [Catalina] INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"] INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-8009"] INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"] INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-8009"]
JAVA_HOME and JRE_HOME Env Variable
It is important to understand how these two environment variable will affect your Apache Tomcat:
If you have multiple Java installed on your system, then you may want Apache tomcat to use a specific version of Java instead of the default version.
For this you need to set either your JRE_HOME or JAVA_HOME appropriately.
JRE_HOME – This is for Java Runtime Environment. Use this environment variable to specify the location of the Java JRE 8 on your system.
JAVA_HOME – This is for Java Development Kit. Use this to specify the location of Java JDK 8 on your system.
If you have JDK installed, it is best to use JAVA_HOME, as this will give you additional startup options that are not allowed when you use only JRE.
For some reason, if you’ve set both JRE_HOME and JAVA_HOME, then tomcat will use JRE_HOME during startup.
The best way to specify these environment variable is in your setenv.sh script which is located under $CATALINA_HOME/bin directory.
Add your JAVA_HOME environment variable to the setenv.sh file as shown below.
$ cd $CATALINA_HOME/bin $ vi setenv.sh JAVA_HOME=/usr/java/jdk1.8.0_131
Now when you start Tomcat as shown below, you can see that it is using the JAVA_HOME environment’s value to start the tomcat.
$ $CATALINA_HOME/bin/catalina.sh start Using CATALINA_BASE: /home/tomcat/apache-tomcat-9.0.0.M21 Using CATALINA_HOME: /home/tomcat/apache-tomcat-9.0.0.M21 Using CATALINA_TMPDIR: /home/tomcat/apache-tomcat-9.0.0.M21/temp Using JRE_HOME: /usr/java/jdk1.8.0_131 Using CLASSPATH: /home/tomcat/apache-tomcat-9.0.0.M21/bin/bootstrap.jar:/home/tomcat/apache-tomcat-9.0.0.M21/bin/tomcat-juli.jar Tomcat started.
Note: Eventhough it says JRE_HOME in the above output, notice how the value of this is actually the value of JAVA_HOME that we set in the setenv.sh file.
Also, from the catalina.out log file, you’ll see the following during startup, which confirms that this is using this new value that we set.
INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/java/jdk1.8.0_131/jre
Apache Tomcat UI – Default Home Page
When everything is up and running, you should be able to go to the following URL To view the tomcat home page.
By default, Apache tomcat is installed to run on port 8080.
If you already have some other application on your system that is running on port 8080, then you can change this by modifying this port value from 8080 to something else in the following server.xml file:
The following is the default Apache Tomcat home page after installation.