How to install Apache Tomcat on Ubuntu Server

Tomcat is an open source server application for Linux, Windows, and other operating systems that are designed to run Java Serverlet Containers, and can also run Java Server Page technology. In this guide, we’ll be going over how to install Apache Tomcat version 9 on Ubuntu Server 18.04 LTS.

 

Note: while this guide focuses on getting Apache Tomcat working on Ubuntu 18.04, it will also run on 18.10 as well.

Set up Java

Apache Tomcat is a Java server, so it’s not possible to use the software without first installing Java. Thankfully, there’s a PPA out there for Ubuntu that takes the difficulty out of getting a working version of the Java runtime environment.

To install the PPA, launch a terminal on your Ubuntu server and enter the following command.

Note: if your Ubuntu server can’t use PPAs out of the box, you’ll need to install the software-properties-common package beforehand.

sudo add-apt-repository ppa:webupd8team/java

After adding the PPA to Ubuntu, do yourself a favor and read the prompt that appears on-screen. It gives information relating to the PPA and how your system will be supported. Then, when you’re done reading the prompt, run the update command.

sudo apt update

With update command finished, all that’s left to do is to install the Java packages using Apt install.

sudo apt install oracle-java8-installer

Configure Java

The WebUpd8 PPA makes it very easy to get a version of Java working on Ubuntu server. However, the Java environment doesn’t automatically set itself up for use when it is installed. Instead, you must set up Java yourself adding things to the /etc/environment file.

In a terminal, open /etc/environment with the Nano text editor.

sudo nano -w /etc/environment

Navigate to the bottom of the file and disregard any text you see in this file. Then, paste the code below into Nano.

JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre"

Save the edits to the /etc/environment file you just made by pressing the Ctrl + O keyboard combination. Then, close the editor by pressing Ctrl + X.

Once the environment is set, we must edit the Bashrc file and set the path for Jaa.

nano -w ~/.bashrc

Navigate to the very bottom of the file and add the code below to the Bashrc file.

# Java Path
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre
export PATH=JAVA_HOME/bin:$PATH

Save your edits to the Bashrc file by pressing Ctrl + O on the keyboard. Then, close it with Ctrl + X and enter the source and echo commands to finish setting everything up.

source ~/.bashrc

Once you’ve closed the Nano text editor, reboot to make sure that the Java environment is ready to go.

Install Apache Tomcat

Java is working, so now we can set up Tomcat. To do it, gain root, CD into the /tmp directory and download version 9.0.13 of Tomcat.

sudo -s
cd /tmp
wget http://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.13/bin/apache-tomcat-9.0.13.tar.gz

When the Tomcat package is done downloading, it’s safe to extract it to /opt.

mkdir -p /opt/tomcat
tar xzvf /tmp/apache-tomcat-9.0.13.tar.gz -C /opt/tomcat/ --strip-components=1

With the software installed in the /opt/tomcat folder, it’s now time to create the “Tomcat” user and create the “Tomcat” group.

groupadd tomcat

useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Next, give the new Tomcat user permissions to access and work with the /opt/tomcat directory on Ubuntu by using the chown command.

chown -R tomcat:tomcat /opt/tomcat

Next, use the chmod command to make all of the files in the /opt/tomcat/bin/ directory executable.

cd /opt/tomcat/bin
chmod +x *

Open up the Bashrc file one last time in Nano.

nano -w ~/.bashrc

Once the file is open, define the Catalina environment for Tomcat, by adding the code below to the bottom of the file.

#Catalina
export CATALINA_HOME=/opt/tomcat

Save the edits by pressing Ctrl + O on the keyboard.

Source the changes to the Bashrc file with:

source ~/.bashrc

Finally, start up the server by running the following command:

sudo $CATALINA_HOME/bin/startup.sh

Turning off Tomcat

To stop the Tomcat server, run the shutdown script.

sudo $CATALINA_HOME/bin/shutdown.sh

Access Apache Tomcat server

Tomcat opens up on port 8080 by default, so to access it, you’ll need to find the local IP address of the server, and access it at the following URL in a web browser.

http://ip-address-of-server:8080

Unsure of what the local IP address of your Ubuntu server is? You can easily find it by going to a terminal and running the ip addr command.

ip addr show | grep 192.168*

If your server doesn’t use 192.168, remove the Grep command and run it like:

ip addr show

Source