Install Apache Tomcat 9 on Ubuntu 20.04

0
1466

 Apache Tomcat is an opensource java based HTTP web server that implements  the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies. In this tutorial, you will learn how to install Apache Tomcat 9 on Ubuntu 20.04.

Install Apache Tomcat 9 on Ubuntu 20.04

Run system update

Ensure that your system package cache is up-to-date;

apt update

Install OpenJDK 14 on Ubuntu 20.04

Apache Tomcat 9 requires a Java Standard Edition Runtime Environment (JRE) version 8 or later. Therefore, install OpenJRE as follows;

apt install openjdk-14-jre

Once the installation completes, you can run the command below to verify the version.

java -version
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+7-Ubuntu-1ubuntu1)
OpenJDK 64-Bit Server VM (build 14.0.1+7-Ubuntu-1ubuntu1, mixed mode, sharing)

Download Apache Tomcat Binary Distribution

Navigate to  Apache Tomcat 9 downloads page and grab Apache Tomcat 9 binary distribution tarball. As of this writing, Apache Tomcat 9.0.36 is the current stable release.

wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.36/bin/apache-tomcat-9.0.36.tar.gz

Install Apache Tomcat 9 on Ubuntu 20.04

Once the download of the binary distribution is complete, installation of Apache Tomcat using the binary distribution is as easy as extracting it to some specific directory, which in this case, we used /opt/tomcat9 directory.

mkdir /opt/tomcat9
tar xzf apache-tomcat-9.0.36.tar.gz -C /opt/tomcat9 --strip-components=1

The command above extracts the contents of apache-tomcat-9.0.36.tar.gz tarball to the installation directory, /opt/tomcat9.

ls /opt/tomcat9/
bin BUILDING.txt conf CONTRIBUTING.md lib LICENSE logs NOTICE README.md RELEASE-NOTES RUNNING.txt temp webapps work

Set Apache Tomcat 9 Environment Variables

Various environment variables are used by the Tomcat startup scripts to prepare the command that runs Tomcat daemon.

Set CATALINA_HOME  environment variable to point to the base path of the Tomcat installation, which in this case is, /opt/tomcat9.

echo 'export CATALINA_HOME="/opt/tomcat9"' > /etc/profile.d/tomcat9.sh

Depending on the Java package installed, set JRE_HOME (if you installed JRE) or JAVA_HOME (if you installed JDK) environment variable for the Java version you have installed. Since we installed JRE, create the JRE_HOME environment as follows.

Locate the path to the installed Java version using update-java-alternatives command.

update-java-alternatives -l
java-1.14.0-openjdk-amd64      1411       /usr/lib/jvm/java-1.14.0-openjdk-amd64

Once you have the path, create the environment variable.

echo 'export JRE_HOME="/usr/lib/jvm/java-1.14.0-openjdk-amd64"' >> /etc/profile.d/tomcat9.sh

Reload the environment variables set above.

source /etc/profile.d/tomcat9.sh

Create Tomcat System User

Just like any other web server, Apache Tomcat should not be run with a privileged user. Hence, create a system user for Apache Tomcat as follows;

useradd -r -d /opt/tomcat9/ -s /bin/false tomcat

Next, you need to set the user and group ownership of Tomcat 9 directory to tomcat user.

chown -R tomcat: /opt/tomcat9/

Assign tomcat the read and execution permissions on the Tomcat 9 configuration files directory.

chmod -R u+x,g+rx /opt/tomcat9/

Configure Tomcat Web Management Accounts

Create Tomcat Web Management User

Define a user for the web management of Tomcat 9 Admin/Manager User interfaces.

This can be done by editing the /opt/tomcat9/conf/tomcat-users.xml file and adding the following highlighted lines below between the </tomcat-users> tag.

vim /opt/tomcat9/conf/tomcat-users.xml
<tomcat-users
...
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="must-be-changed" roles="tomcat"/>
  <user username="both" password="must-be-changed" roles="tomcat,role1"/>
  <user username="role1" password="must-be-changed" roles="role1"/>
-->
  <role rolename="admin-gui"/>
  <role rolename="manager-gui"/>
  <user username="admin" password="[email protected]" roles="admin-gui,manager-gui"/>
</tomcat-users>

Replace the password and usernames accordingly.

Configure Tomcat to allow remote connection to Manager and Host Manager apps.

Edit the configuration files below for Manager and Host Manager respectively and enter the IP addresses of the remote server you are accessing the Tomcat from. The IPs are separated by a pipe, |.

In this case, 192.168.56.1, is the IP address of the server to allow access of Tomcat from.

vim /opt/tomcat9/webapps/manager/META-INF/context.xml
...
<Context antiResourceLocking="false" privileged="true" >
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
          allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.56.1" />
...
</Context>
vim /opt/tomcat9/webapps/host-manager/META-INF/context.xml
...
<Context antiResourceLocking="false" privileged="true" >
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.56.1" />
...
</Context>

Save and exit the configuration file after changes are made.

Allow Tomcat Through Firewall

To allow external access to Tomcat, you need to open TCP port 8080 on UFW, if it is running;

ufw allow 8080/tcp

Running Tomcat 9 on Ubuntu 20.04

To run Apache Tomcat in standalone mode, execute the /opt/tomcat9/bin/startup.sh. script.

/opt/tomcat9/bin/startup.sh
Using CATALINA_BASE:   /opt/tomcat9
Using CATALINA_HOME:   /opt/tomcat9
Using CATALINA_TMPDIR: /opt/tomcat9/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.14.0-openjdk-amd64
Using CLASSPATH:       /opt/tomcat9/bin/bootstrap.jar:/opt/tomcat9/bin/tomcat-juli.jar
Tomcat started.

Accessing Tomcat 9 Web Interface

You can now access your Apache Tomcat9 from the browser using the address, http://server-hostname-or-ip:8080.

Install Apache Tomcat 9 on Ubuntu 20.04

Click Manager App to access the Tomcat Web Application Manager. When prompted for credentials, use the ones you set above;

Click Host Manager, to access Tomcat virtual host manager.

Install Apache Tomcat 9 on Ubuntu 20.04

Running Apache Tomcat 9 as a service

To be able to run Apache Tomcat9 as a service and ensure it runs on system reboots, you need to create a systemd service unit file it as follows.

vim /etc/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat Server
After=syslog.target network.target

[Service]
Type=forking
User=tomcat
Group=tomcat

Environment=CATALINA_PID=/opt/tomcat9/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat9
Environment=CATALINA_BASE=/opt/tomcat9

ExecStart=/opt/tomcat9/bin/catalina.sh start
ExecStop=/opt/tomcat9/bin/catalina.sh stop

RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target

Replace the paths accordingly as per your installation. Save and exit the file.

Reload systemd configurations

systemctl daemon-reload

Stop the initial Tomcat9 started with the script above, /opt/tomcat9/bin/startup.sh.

Start and enable Tomca9 systemd service;

systemctl enable --now tomcat

Check the status;

systemctl status tomcat
● tomcat.service - Apache Tomcat Server
     Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2020-06-19 21:26:19 UTC; 2s ago
    Process: 6834 ExecStart=/opt/tomcat9/bin/catalina.sh start (code=exited, status=0/SUCCESS)
   Main PID: 6855 (java)
      Tasks: 37 (limit: 2282)
     Memory: 88.3M
     CGroup: /system.slice/tomcat.service
             └─6855 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat9/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -D>

Jun 19 21:26:19 ubuntu20 systemd[1]: Starting Apache Tomcat Server...
Jun 19 21:26:19 ubuntu20 catalina.sh[6834]: Tomcat started.
Jun 19 21:26:19 ubuntu20 systemd[1]: Started Apache Tomcat Server.

And that marks the end of our guide on how to install Apache Tomcat 9 on Ubuntu 20.04.

Reference

Apache Tomcat 9.0 Doc -RUNNING.txt

Related Tutorials

Install Apache Tomcat 9 on Debian 10/Debian 9

Install Oracle Java (OpenJDK) 13 on CentOS 8

Install Oracle Java 12 on Debian 10

Install Oracle Java 12 on Ubuntu 18.04/Debian 9.8

Install Java 11 on Debian 9.8/Ubuntu 18.04

LEAVE A REPLY

Please enter your comment!
Please enter your name here