(Last Updated On: June 14, 2018)

The objective of this guide is to help you get Apache Tomcat up and running on your Ubuntu 18.04 server (Bionic Beaver).  Apache Tomcat is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies.

Apache Tomcat Requirements

  • – Tomcat 9.x requires Java SE 8 or later.
  • – Updated Ubuntu 18.04 OS
  • – A user with sudo privileges

Before we can start the installation of Apache Tomcat, we need to install and configure Java SE Development Kit 8 (JDK8).

Install Java JDK 8 on Ubuntu 18.04

For this guide, we will install Oracle Java JDK 8 using PPA repository available for Ubuntu 18.04. The PPA repository we’ll use is ppa:webupd8team/java which can be added to Ubuntu using the following command:

$ sudo add-apt-repository ppa:webupd8team/java
 Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK8). There are no actual Java files in this PPA.

Important -> Why Oracle Java 7 And 6 Installers No Longer Work: http://www.webupd8.org/2017/06/why-oracle-java-7-and-6-installers-no.html

Update: Oracle Java 9 has reached end of life: http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html

The PPA supports Ubuntu 18.04, 17.10, 16.04, 14.04 and 12.04.

More info (and Ubuntu installation instructions):
- for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html

Debian installation instructions:
- Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html

For Oracle Java 10, see a different PPA: https://www.linuxuprising.com/2018/04/install-oracle-java-10-in-ubuntu-or.html
 More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Press <Enter> key to continue with the installation.

Then install Java JDK 8  using the command:

$ sudo apt install oracle-java8-set-default
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following additional packages will be installed:
gsfonts-x11 java-common libfontenc1 oracle-java8-installer x11-common
xfonts-encodings xfonts-utils
Suggested packages:
......
Do you want to continue? [Y/n] y

Note that you can also install Java Open JDK version 8 using official Ubuntu repositories, just execute:

$ sudo apt install openjdk-8-jdk

Confirm the version of Java installed:

$ java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

If you want to change the default version of Java used in your system, use the command:

$ sudo update-alternatives --config java
There is 1 choice for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 auto mode
* 1 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode

Press <enter> to keep the current choice[*], or type selection number: 0

Install Apache Tomcat 9 on Ubuntu 18.04

Now that we have Java Open JDK installed, we should be ready to start the installation of Apache Tomcat on Ubuntu 18.04. The installation process is as below:

Step 1: Download Apache Tomcat archive

Let’s download Apache tomcat 9 using wget:

$ sudo apt-get install wget
$ wget http://ftp.riken.jp/net/apache/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
or
$ wget http://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz

For Apache Tomcat 8, use:

$ wget http://ftp.riken.jp/net/apache/tomcat/tomcat-8/v8.0.52/bin/apache-tomcat-8.0.52.tar.gz
or
$ wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.52/bin/apache-tomcat-8.0.52.tar.gz

Step 2: Extract tar archive and install Tomcat

# tar xvf apache-tomcat-9.0.8.tar.gz

Move extracted files to /usr

# mv apache-tomcat-9.0.8 /usr/share/

Create apache tomcat9 user

# useradd -M -d /usr/share/apache-tomcat-9.0.8/ tomcat9
# chown -R tomcat9 /usr/share/apache-tomcat-9.0.8/

Allow access to Apache Tomcat UI from trusted network/Ip:

Edit the files:

/usr/share/apache-tomcat-9.0.8/webapps/manager/META-INF/context.xml
/usr/share/apache-tomcat-9.0.8/webapps/host-manager/META-INF/context.xml

Modify allow line to add ip from which you want to access UI interface.

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.1.20" />

Secure Access to admin/manager dashboard

We need to secure access to admin areas of tomcat UI, edit the file:

/usr/share/apache-tomcat-9.0.8/conf/tomcat-users.xml

Add the following content before </tomcat-users>

<!-- manager section user role -->
<role rolename="manager-gui" />
<user username="manager" password="[email protected]" roles="manager-gui" />

<!-- admin section user role -->
<role rolename="admin-gui" />
<user username="admin" password="[email protected]" roles="manager-gui,admin-gui" />

Start Apache Tomcat service:

You can now manage apache tomcat service with the user account we added (tomcat9)

# su - tomcat9 -c /usr/share/apache-tomcat-9.0.8/bin/startup.sh start
Using CATALINA_BASE: /usr/share/apache-tomcat-9.0.8
Using CATALINA_HOME: /usr/share/apache-tomcat-9.0.8
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-9.0.8/temp
Using JRE_HOME: /usr/lib/jvm/java-8-oracle
Using CLASSPATH: /usr/share/apache-tomcat-9.0.8/bin/bootstrap.jar:/usr/share/apache-tomcat-9.0.8/bin/tomcat-juli.jar
Tomcat started.

If the start was successful, you should see port 8080 on your system being used by a Java process.

# ss -tunelp | grep 8080
tcp LISTEN 0 100 *:8080 *:* users:(("java",pid=15131,fd=49)) ino:9110112 sk:18 v6only:0 <->

Try to access port 8080 on your web browser using the server IP address or its hostname: