Install And Configure APACHE TOMCAT 8 On Ubuntu 16.04 /18.04 LTS

Apache Tomcat is an open-source software implementation of the Java Servlet and Java Server Pages.Tomcat is used to serve Java applications and provides a Java HTTP web server environment in which Java code can run

This guide explains how to install Apache Tomcat on an Ubuntu 16.04/18.04 server.For this tutorials we are installing Apache tomcat 8.5.57 version.

Before We Begin:

  • root user or user with sudo privileges on ubuntu server
  • Oracle or openjdk java installed and configured with environment variables.

Tomcat requires Java JDK. If you want to know how to install java please go through our previous post related to java installation and configure on How to install and configure java on ubuntu 16.04/18.04 LTS-PART 1

Test java version ,JAVA_HOME and PATH before the tomcat installation

java -version

openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~16.04-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

echo $JAVA_HOME

/usr/lib/jvm/java-1.8.0-openjdk-amd64

echo $PATH

/home/ubuntuclient/bin:/home/ubuntuclient/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin

Here we can see that openjdk java 1.8 version is installed where the installation path is

/usr/lib/jvm/java-1.8.0-openjdk-amd64.

Install Tomcat

The best and recommended way to install Tomcat 8 is to download the latest binary release then configure it manually.

Find the latest version of Tomcat 8 at the Tomcat 8 Downloads page.At the time of writing, the latest version is 8.5.57.Under the Binary Distributions section, then under the Core list, we can see the tar.gz file.copy the link address (ubuntu show copy link location when click )It will look like :

https://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.57/bin/apache-tomcat-8.5.57.tar.gz

Next, go to the /opt/tomcat directory and download tomcat with the wget command:

cd /opt/
wget https://mirrors.estointernet.in/apache/tomcat/tomcat-
8/v8.5.57/bin/apache-tomcat-8.5.57.tar.gz

Or you can directly download the tar.gz file and move into /opt directory also.

Unzip the files into the tomcat folder.

sudo tar xzvf apache-tomcat-8.5.57.tar.gz

Configure tomcat directory

After downloading we can see tomcat folder like /opt/apache-tomcat-8.5.57.For convenience just rename apache-tomcat-8.5.57 to tomcat

cd /opt

mv apache-tomcat-8.5.57 tomcat

Now our tomcat installation directory ready and it is /opt/tomcat.

This information is missing from many blogs related to tomcat installation since upon downloading tomcat file name look like apache-tomcat with version number.In this case apache-tomcat-8.5.57.We need to manually rename it to tomcat .Then only it become proper installation directory /opt/tomcat otherwise it will look like /opt/tomcat/apache-tomcat-8.5.57

Update Permissions

The tomcat user that we set up needs to have access to the Tomcat installation. We’ll set that up now.

Change to the directory where we unpacked the Tomcat installation:

cd /opt/
sudo chown -R tomcat tomcat/

Configure .bashrc File

Set the environment variables in .bashrc with the following command:

nano .bashrc

add the below contents to the end of the .bashrc file .save and close it

export CATALINA_HOME=/opt/tomcat

Verify your file paths! If you downloaded a different version or already installed Java, you may have to edit the file path or name. . Likewise, if you installed Tomcat in a different folder other than /opt/ (as suggested) you’ll indicate the path in your bash file and edit the lines above.

To save the changes load the command

source .bashrc

Create Tomcat User

For security purposes, Tomcat should be run as an unprivileged user (i.e. not root). We will create a new user and group that will run the Tomcat service

First, create a new tomcat group:

sudo groupadd tomcat

Next, create a new tomcat user. We’ll make this user a member of the tomcat group, with a home directory of /opt/tomcat (where we have installed install Tomcat), and with a shell of /bin/false (so nobody can log into the account):

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

Now we done tomcat user setup and installation.

Start tomcat and test

We can start tomcat as a service or using the script also.Here we are starting tomcat using the script as it is easy and comfortable method while working on application deployments

Note :

You cannot perform this as normal user.Need to login in as tomcat user and run the script .

Because we already given permissions and ownership to tomcat folder to user tomcat only

In order to start Tomcat ,login as tomcat user as below and run the script :

sudo -s -u tomcat

tomcat@gitserver:~$

Now we logged in as tomcat .Note Previously our username was ubuntuclient.

sh $CATALINA_HOME/bin/startup.sh

will get output as follows :

tomcat@gitserver:~$ sh $CATALINA_HOME/bin/startup.sh
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.8.0-openjdk-amd64
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Tomcat started

Note the javapath and tomcat installation path.

To verify that Tomcat is working, visit the IP.of.your.server:8080 in a web browser. For example, http://127.0.0.1:8080.

If you want to deploy a sample java file like war to test , go to https://tomcat.apache.org/tomcat-8.0-doc/appdev/sample/ and download the sample.war file

Go to /opt/tomcat/webapps folder .Copy the sample.war file to this location.

start the tomcat .check our war is running or not on tomcat

http://127.0.0.1:8080/sample

will get an output like

Conclusion

Your installation of Tomcat is complete! Your are now free to deploy your own Java web applications!

One Comment

  1. Dileep
    Jul 10, 2020 at 9:09 am

    Well said…. !!

Leave a Reply