How to Install and Configure Apache Tomcat 9 on Ubuntu 18.04 LTS

Apache Tomcat is an open source Java Servlet implementation developed by the Apache Software Foundation. In addition to the implementation of Java Servlets, Tomcat supports other Java server technologies too, including JavaServer Pages (JSP), Java Expression Language, and Java WebSocket. Tomcat provides an HTTP Web Server for Java applications that supports HTTP/2, OpenSSL for JSSE, and TLS virtual hosting.

In this tutorial, we will show you how to install and configure the Apache Tomcat 9.0.8 on Ubuntu 18.04 LTS (Bionic Beaver). We will also learn how to install Java on Ubuntu 18.04 LTS, configure a user for apache tomcat, and run the apache tomcat as a systemd service.

Prerequisites

  • Ubuntu 18.04 – 64bit
  • 2 GB or more memory (Recommended)
  • Root Privileges

What we will do?

  1. Install Java on Ubuntu 18.04
  2. Configure Java Environment
  3. Install Apache Tomcat 9
  4. Configure Apache Tomcat as a Service
  5. Configure Apache Tomcat Users
  6. Testing

Step 1 – Install Java on Ubuntu 18.04

In this step, we will install Java JRE and JDK from the PPA repository. To do that, we have to install a new package ‘software-properties-common’ first for managing the repository.

Install ‘software-properties-common’ package.

sudo apt install software-properties-common -y

Add the java ‘webupd8team’ repository using the ‘add-apt-repository’ command.

sudo add-apt-repository ppa:webupd8team/java

And install java using the apt command below.

sudo apt install oracle-java8-installer -y

When the installation is complete, check the java version installed on the system.

java -version

And you will get the result as below.

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)

Install Java on Ubuntu

Java has been installed on Ubuntu 18.04.

Step 2 – Configure Java Environment

In the first step, we’ve installed Java. Now we need to configure the JAVA_HOME environment variable on the Ubuntu server so that Java applications can find the Java installation directory. Tomcat needs a JAVA_HOME environment to be set up properly.

Before we configure the JAVA_HOME environment, we need to know where the Java directory is. Check the location of the Java directory with the command below:

sudo update-alternatives –config java

Configure Java Environment

The java directory  is “/usr/lib/jvm/java-8-oracle/jre

Now edit the ‘/etc/environment’ file using vim.

vim /etc/environment

Paste the following configuration there.

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

Save and exit.

Edit the ‘~/.bashrc’ file.

vim ~/.bashrc

Paste the below configuration.

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

Save and exit.

Now reload the ‘~/.bashrc’ script and test the ‘JAVA_HOME’ directory.

source ~/.bashrc
echo $JAVA_HOME

Setup Java environment

The java environment setup has been completed.

Step 3 – Install Apache Tomcat

In this step, we will install the Apache Tomcat under the ‘tomcat’ user and group. We will download the apache tomcat binary files, configure the Catalina tomcat servlet container environment, and do a first test of the Tomcat server.

Add new user and group named ‘tomcat’ using the commands below.

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

Now go to the ‘/opt/’ directory and download the latest apache tomcat stable version (9.0.8) using the wget command.

cd /opt/
wget http://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz

Extract the apache tomcat package file and rename the directory to ‘tomcat’.

tar -xzvf apache-tomcat-9.0.8.tar.gz
mv apache-tomcat-9.0.8/ tomcat/

Change the owner of tomcat directory and files to the ‘tomcat’ user and group, then make all apache tomcat binary files executable.

chown -R tomcat:tomcat /opt/tomcat
chmod +x /opt/tomcat/bin/*

Next, we will configure the Catalina tomcat servlet container environment by editing the ‘~/.bashrc’ file using vim.

vim ~/.bashrc

Paste the following configuration there.

export CATALINA_HOME=/opt/tomcat

Save and exit.

Reload the ‘~/.bashrc’ file and test the Catalina environment.

source ~/.bashrc
echo $CATALINA_HOME

You will get the result as shown below.

Setup Tomcat

Now test to run the Apache Tomcat itself.

Run the command below to start Apache tomcat.

$CATALINA_HOME/bin/startup.sh

And you’ll see the result – apache tomcat has been started. It will run on the default port ‘8080’ – you can check the port using netstat command.

netstat -plntu

The Apache Tomcat is running the server under port 8080.

Install and start Apache Tomcat

Another way for testing the apache tomcat is by visiting the server IP address with port 8080.

http://192.168.10.100:8080/

And you will see the apache tomcat homepage as below.

Tomcat started

Apache Tomcat has been installed on Ubuntu 18.04.

Now run the command below to stop the apache tomcat.

$CATALINA_HOME/bin/shutdown.sh
chown -hR tomcat:tomcat /opt/tomcat/

Stop Tomcat server

Step 4 – Configure Apache Tomcat as a Service

In this tutorial, we want to run Apache Tomcat as tomcat user with a systemd service file so it can be started and stopped easily. Now we need to create the ‘apache-tomcat.service’ file.

Go to the systemd system directory and create a new file ‘apache-tomcat.service’.

cd /etc/systemd/system/
vim apache-tomcat.service

Paste the following configuration there:

[Unit]
Description=Apache Tomcat 9 Servlet Container
After=syslog.target network.target

[Service]
User=tomcat
Group=tomcat
Type=forking
Environment=CATALINA_PID=/opt/tomcat/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Save and exit.

Now reload the systemd service.

systemctl daemon-reload

And start the ‘apache-tomcat’ service using the systemctl commands below.

systemctl start apache-tomcat
systemctl enable apache-tomcat

Configure Apache Tomcat as a Service

The Apache Tomcat is now running as a service on Ubuntu 18.04, check it using following commands.

netstat -plntu
systemctl status apache-tomcat

Check Tomcat service

The Apache Tomcat is now running and it’s using the port 8080 as a default port.

Step 5 – Configure Apache Tomcat User

In this step, we will configure the users for Apache Tomcat. We will add a new user to access the manager UI, and then allow the manager and host-manager access.

Go to the ‘/opt/tomcat/conf’ directory and edit the ‘tomcat-users.xml’ file using vim.

cd /opt/tomcat/conf
vim tomcat-users.xml

Paste the following configuration before the closing configuration ‘</tomcat-users>‘.

<role rolename="manager-gui"/>
<user username="hakase" password="hakasepassword01" roles="manager-gui,admin-gui"/>

Save and exit.

Configure Apache Tomcat User

Now allow external access to the ‘manager’ dashboard by editing it’s configuration ‘context.xml’ file.

cd /opt/tomcat/webapps/manager/META-INF/
vim context.xml

Comment the ‘allow’ line 19-20.

<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
 allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

Save and exit.

Configure Tomcat tolisten on external network interface

And for the ‘host-manager’.

cd /opt/tomcat/webapps/host-manager/META-INF/
vim context.xml

Comment the ‘allow’ line 19-20.

<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
 allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

Save and exit.

comment out org.apache.catalina.valves.RemoteAddrValve

Restart the Apache Tomcat service using the systemctl command.

systemctl restart apache-tomcat

The apache tomcat-users configuration, manager, and host-manager configuration has been completed.

Step 6 – Testing

Open your web browser and type in your server IP with port 8080. You will see the Apache Tomcat home page.

http://192.168.10.100:8080/

Apache Tomcat runs as a service

Go to the manager dashboard with the URL below:

http://192.168.10.100:8080/manager/html

Type the admin username ‘hakase’ with password ‘hakasepassword01’ from step 5.

Apache Tomcat application manager

Now go to the host-manager dashboard through URL below:

http://192.168.10.100:8080/host-manager/html

Enter the admin user and password from step 5, you will see the Tomcat Virtual Host Manager.

Apache Tomcat Virtual Host Manager

Apache Tomcat 9.0.8 has been installed successfully on Ubuntu 18.04 Bionic Beaver.

Reference

Share this page:

how to install and configure apache tomcat 9 on ubuntu 18 04 lts 15
how to install and configure apache tomcat 9 on ubuntu 18 04 lts 16
how to install and configure apache tomcat 9 on ubuntu 18 04 lts 17
how to install and configure apache tomcat 9 on ubuntu 18 04 lts 18

نوشته های مشابه