Hamsa K
Editor
9 min read | 3 years ago

How to Install Apache Tomcat 9 on Ubuntu 20 04

Install Apache Tomcat on Ubuntu 20.04

Apache Tomcat is an open-source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and WebSocket technologies.The Apache Tomcat provides pure 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 Apache Tomcat 9.0.41 on Ubuntu 20.04 Lts system.

update packages with apt and must have Java installed on your system to run tomcat server.Tomcat 9 required to have Java 8 or higher version installed on your system.By default,ubuntu 20.04 repository provides multiple Java versions, including the OpenJDK 11.

Install Java

sudo apt update
sudo apt install openjdk-11-jdk

Also Read -> How to Install Atom Text Editor on Ubuntu 20 04

once java is installed, you can check version with following command.

java --version

output:

openjdk 11.0.9.1 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

Install and configure tomcat

First we will create system user to run Apache tomcat server. beacuse it is not recommended to run Apache Tomcat as user root.

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

Now go to official tomcat website and download latest version of tomcat in opt directory as below.

cd /opt
wget https://mirrors.estointernet.in/apache/tomcat/tomcat-9/v9.0.41/bin/apache-tomcat-9.0.41.tar.gz

Now,extract Apache Tomcat binary file and rename the directory to 'tomcat'.

sudo tar -xvzf apache-tomcat-9.0.41.tar.gz
sudo mv apache-tomcat-9.0.41/* /opt/tomcat/

Also Read -> How to Install NodeJs and npm on Ubuntu 20 04

Change the ownership of the directory to allow tomcat user to write files.

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

Next configure environment variables using below command.

echo "export CATALINA_HOME="/opt/tomcat"" >> ~/.bashrc
source ~/.bashrc
echo $CATALINA_HOME

create systemd service

now create a tomcat systemd service file.

sudo nano /etc/systemd/system/tomcat.service

Add the following content to above file.

[Unit]
Description=Tomcat
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target

Now Reload the systemd daemon service,enable and start tomcat service.

sudo systemctl daemon-reload
sudo systemctl enable tomcat
sudo systemctl start tomcat
sudo systemctl status tomcat

By default, Apache Tomcat runs on port 8080. Use the netstat command to check the Tomcat service.

sudo netstat -antup | grep 8080

Also Read -> How to Install Ruby on Rails on Ubuntu 20 04

Adjust Firewall

if firewall is running on your server then you should open port 8080 to access tomcat outside of your local system.

sudo ufw allow 8080/tcp

Configure web management interface

now you can configure tomcat with user accounts to secure access of admin/manager pages by editing tomcat-users.xml file.

sudo nano /opt/tomcat/conf/tomcat-users.xml

Add following lines under tomcat users section

<tomcat-users>
<!--
    Comments
-->
   <role rolename="admin-gui"/>
   <role rolename="manager-gui"/>
   <user username="admin" password="Strong_password" roles="admin-gui,manager-gui"/>
</tomcat-users>

save and exit the file.

By default tomcat web management interface does not allow access web interface from a remote ip.To enable access to the web interface from anywhere, open the following two files and comment or remove lines as below.

sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

modify below line  to allow all the networks in both files.

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

save and close both the files.

It is also allowed to set a specific IP to access web interface instead of from anywhere. Do not comment the blocks add your public IP to the list.For example to allow 192.168.1.0/24 network only, you can use below values.

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

To allow specific ip.

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

You can add number of ip address with vertical bar separator.Now restart tomcat service with the following command.

sudo systemctl restart tomcat

Also Read -> How to Install MySQL Workbench on Ubuntu 20 04

Access Tomcat

open your favorite browser and type http://ip_address:8080 

and click on Manager app, it promts for login details and Enter the credentials which we created previously in tomcat-users.xml

http://ip_address:8080/manager/html

Virtual Host Manager App is available at http://ip_address:8080/host-manager/html. you can manage more vhosts using this.

That's it. Now you have successfully installed and configured tomcat 9 on ubuntu 20.04 Lts system.

Also Read -> How to Install Android Studio on Ubuntu 20 04



Warning! This site uses cookies
By continuing to browse the site, you are agreeing to our use of cookies. Read our terms and privacy policy