Setting Up a Linux Based Tomcat Server (for Java Applications)

Tutorial Difficulty Level    

Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, released by the Apache Software Foundation. This tutorial covers the basic installation and some configuration of Tomcat  on your Linux server.

Please Note That an Ubuntu 14.04 server  was used in the creation of this tutorial. You will also need to have a non-root user account set up on your server.  This link will explain how.

 We will be using a user called Ubuntu for the rest of this tutorial.

Tomcat requires that Java is installed on the server, so any Java web application code can be executed. Let’s satisfy that requirement by installing OpenJDK 7 with apt-get.

First, update your apt-get package index:

Then install the Java Development Kit package with apt-get:

Answer y at the prompt to continue installing OpenJDK 7.

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:

Then 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 will install Tomcat), and with a shell of /bin/false (so nobody can log into the account):

Now that our tomcat user is set up, let’s download and install Tomcat.

The easiest way to install Tomcat 8 is to download the latest binary release then configure it manually. (We are using Tomcat 8 rather than 9 because Tomcat 9 as of this date is still in Alpha development).

Find the latest version of Tomcat 8 at the Tomcat 8 Downloads page. At the time of writing, the latest version is 8.5.12. Under the Binary Distributions section, then under the Core list, copy the link to the “tar.gz”.

Let’s download the latest binary distribution to our home directory.

First, change to your home directory:

Then use wget and paste in the link to download the Tomcat 8 archive, like this (your mirror link might differ from the example):

We’re going to install Tomcat to the /opt/tomcat directory. Create the directory, then extract the the archive to it with these commands:

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

Change to the Tomcat installation path:

Then give the tomcat user write access to the conf directory, and read access to the files in that directory:

Then make the tomcat user the owner of the work, temp, and logs directories:

Now that the proper permissions are set up, let’s set up an Upstart init script.

Because we want to be able to run Tomcat as a service, we will set up an Upstart script.

Tomcat needs to know where Java was installed. This path is commonly referred to as “JAVA_HOME”. The easiest way to look up that location is by running this command:

Output:

The JAVA_HOME will be in the output, without the trailing /bin/java. For the example above, the JAVA_HOME is /usr/lib/jvm/java-7-openjdk-amd64/jre

Now we’re ready to create the Upstart script. Create and open it by running this command:

Paste in the following script, and modify the value of JAVA_HOME if necessary. You may also want to modify the memory allocation settings that are specified in CATALINA_OPTS:

Save and exit. This script tells the server to run the Tomcat service as the tomcat user, with the settings specified. It also enables Tomcat to run when the server is started.

Now let’s reload the Upstart configuration, so we can use our new Tomcat script:

Tomcat is ready to be run. Start it with this command:

Tomcat is not completely set up yet, but you can access the default splash page by going to your domain or IP address followed by :8080 in a web browser:

Open in web browser:

http://server_IP_address:8080 (e.g. http://localhost:8080)

You will see the default Tomcat splash page, in addition to other information. Now we will go deeper into the installation of Tomcat.

In order to use the manager webapp that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the tomcat-users.xml file:

This file is filled with comments which describe how to configure the file. You may want to delete all the comments between the following two lines, or you may leave them if you want to reference the examples:

You will want to add a user who can access the manager-gui and admin-gui (webapps that come with Tomcat). You can do so by defining a user similar to the example below. Be sure to change the username and password to something secure:

Save and quit the tomcat-users.xml file. To put our changes into effect, restart the Tomcat service:

Now that Tomcat is up and running, let’s access the web management interface in a web browser. You can do this by accessing the public IP address of the server, on port 8080:

http://server_IP_address:8080 (e.g. http://localhost:8080)

You will see something like the following image:

As you can see, there are links to the admin webapps that we configured an admin user for.

Let’s take a look at the Manager App, accessible via the link or http://server_IP_address:8080/manager/html:

The Web Application Manager is used to manage your Java applications. You can Start, Stop, Reload, Deploy, and Undeploy here. You can also run some diagnostics on your apps (i.e. find memory leaks). Lastly, information about your server is available at the very bottom of this page.

Now let’s take a look at the Host Manager, accessible via the link or http://server_IP_address:8080/host-manager/html/:

From the Virtual Host Manager page, you can add virtual hosts to serve your applications from.

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