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 . 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.

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)
111

Java has been installed on Ubuntu 18.04.

GO2E

Dedicated Servers

If you’re unsure of the type of hosting you need, it’s always a good idea to look at the benefits. Compared to other types of hosting, dedicated server hosting provides the maximum-security level and the ability to use ALL the resources of the server for your website.……


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
2122

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.

ban3

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

source ~/.bashrc
echo $JAVA_HOME
323

The java environment setup has been completed.

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.

434

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.

545

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.

656

Apache Tomcat has been installed on Ubuntu 18.04.

Now run the command below to stop the apache tomcat.

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

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
878

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

netstat -plntu
systemctl status apache-tomcat
989

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

ban4

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.

1211

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.

1121

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.

1221

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.

ban2

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/

1331

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

1441

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.

1551

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

ban5

Leave a Reply

Your email address will not be published. Required fields are marked *