habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Easy Install Tomcat9 in Ubuntu 20.04

4 min read

Today, i will post about install Tomcat9 in Ubuntu 20.04. Tomcat is software is an open source implementation of the Jakarta Servlet, Jakarta Server Pages, Jakarta Expression Language, Jakarta WebSocket, Jakarta Annotations and Jakarta Authentication specifications. Java give name Jakarta like similiar capital city of Indonesia. Haha. Kidding dude. :).

Tomcat is one of the most popular choices for building Java-based websites and applications, lightweight and easy to use.

I will install tomcat in 2 ways. At first via repository and last via tarball/compile.

Install Tomcat9 via Repository

This is easiest way.

At first, i am disable firewall internal server, because live live production because the firewall is already handled by the router.

systemctl stop ufw
systemctl dsiable ufw

Setting Hostname and /etc/hosts

First, Set server hostname

root@tcserver:~# hostnamectl set-hostname tcserver.habibza.in

Second, Set hostname in /etc/hosts.

root@tcserver:~# cat /etc/hosts<br>127.0.0.1 localhost<br>192.168.197.144 tcserver.habibza.in tcserver

And then install use apt.

root@tcserver:~# apt update
root@tcserver:~# apt-get install tomcat9 tomcat9-docs tomcat9-examples tomcat9-admin

Reading package lists… Done
Building dependency tree
Reading state information… Done
The following additional packages will be installed:
ca-certificates-java default-jre-headless fontconfig-config fonts-dejavu-core java-common libapr1 libavahi-client3 libavahi-common-data libavahi-common3 libcups2
libeclipse-jdt-core-java libfontconfig1 libgraphite2-3 libharfbuzz0b libjpeg-turbo8 libjpeg8 liblcms2-2 libpcsclite1 libtcnative-1 libtomcat9-java openjdk-11-jre-headless tomcat9-common
Suggested packages:
default-jre cups-common liblcms2-utils pcscd libnss-mdns fonts-dejavu-extra fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei fonts-indic tomcat9-admin
tomcat9-docs tomcat9-examples tomcat9-user
The following NEW packages will be installed:
ca-certificates-java default-jre-headless fontconfig-config fonts-dejavu-core java-common libapr1 libavahi-client3 libavahi-common-data libavahi-common3 libcups2
libeclipse-jdt-core-java libfontconfig1 libgraphite2-3 libharfbuzz0b libjpeg-turbo8 libjpeg8 liblcms2-2 libpcsclite1 libtcnative-1 libtomcat9-java openjdk-11-jre-headless tomcat9
tomcat9-common
0 upgraded, 23 newly installed, 0 to remove and 0 not upgraded.
Need to get 51.9 MB of archives.
After this operation, 193 MB of additional disk space will be used.
Do you want to continue? [Y/n]Y

After installation fisinshed, lets check java version.

root@tcserver:~# java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

after that go to /etc/tomcat9/tomcat-users.xml

<tomcat-users . . .>
    <user username="admin" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>
Install Tomcat9 ubuntu
this is tomcat-user.xml

Open browser and type this url.

http://192.168.197.144:8080

So if wverything wis fine, will show tomcat default page.

See also  Setting HA cluster with Corosync and Pacemaker Ubuntu 20.04 server
Install Tomcat9 ubuntu
tomcat-default-page

And now, we can access tomcat-admin in url http://192.168.197.144:8080/manager. Input user and password where we hade created in tomcat-user.xml.

Install Tomcat9 ubuntu

After input user and password, tomcat-manager will show like below.

Install Tomcat9 via Binary Source

Ok, after install via repository we tried install fresh tomcat9 via binary.

Lets go.

Install Java Kit.

root@tcserver:~# apt update
root@tcserver:~# apt install openjdk-11-jre-headless

Set Tomcat User.

For security reason, we will set tomcat user separate from root.

root@tcserver:~# useradd -m -U -d /opt/tomcat -s /bin/false tomcat

verify tomcat user had created.

root@tcserver:~# cat /etc/passwd | grep tomc
verify user

Download Tomcat

After that, download tomcat from official website.

root@tcserver:~# cd /tmp
root@tcserver:~# wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.54/bin/apache-tomcat-9.0.54.tar.gz

Extract .tar.gz and move to /op/tomcat

root@tcserver:~# tar -zxvf apache-tomcat-9.0.54.tar.gz -C /opt/tomcat/

And now, i want all of binary tomcat stored at /opt/tomcat, where /opt/tomcat is home directory of tomcat user. There is file .bashrc, .profile, and .bash_logout too.

root@tcserver:~# mv /opt/tomcat/apache-tomcat-9.0.54/* /opt/tomcat/

Change the directory ownership to user and group tomcat:

root@tcserver:~# chown -R tomcat: /opt/tomcat

Here is my tomcat home directory.

root@habibza:/opt/tomcat/bin# ls -al /opt/tomcat/
total 168
drwxr-xr-x 9 tomcat tomcat  4096 Nov 12 15:08 .
drwxr-xr-x 3 root   root    4096 Nov 12 13:59 ..
-rw-r--r-- 1 tomcat tomcat   220 Feb 25  2020 .bash_logout
-rw-r--r-- 1 tomcat tomcat  3771 Feb 25  2020 .bashrc
drwxr-x--- 2 tomcat tomcat  4096 Nov 12 14:08 bin
-rw-r----- 1 tomcat tomcat 18970 Sep 28 20:51 BUILDING.txt
drwx------ 3 tomcat tomcat  4096 Nov 12 14:10 conf
-rw-r----- 1 tomcat tomcat  6210 Sep 28 20:51 CONTRIBUTING.md
drwxr-x--- 2 tomcat tomcat  4096 Nov 12 14:08 lib
-rw-r----- 1 tomcat tomcat 57092 Sep 28 20:51 LICENSE
drwxr-x--- 2 tomcat tomcat  4096 Nov 12 14:10 logs
-rw-r----- 1 tomcat tomcat  2333 Sep 28 20:51 NOTICE
-rw-r--r-- 1 tomcat tomcat   807 Feb 25  2020 .profile
-rw-r----- 1 tomcat tomcat  3372 Sep 28 20:51 README.md
-rw-r----- 1 tomcat tomcat  6898 Sep 28 20:51 RELEASE-NOTES
-rw-r----- 1 tomcat tomcat 16507 Sep 28 20:51 RUNNING.txt
drwxr-x--- 2 tomcat tomcat  4096 Nov 12 14:08 temp
drwxr-x--- 7 tomcat tomcat  4096 Sep 28 20:51 webapps
drwxr-x--- 3 tomcat tomcat  4096 Nov 12 14:10 work

And then  shell scripts inside the Tomcat’s bin directory must be executable :

root@tcserver:~# chmod +x /opt/tomcat/bin/*.sh

Lets try startup tomcat.

root@tcserver:~# cd /opt/tomcat/bin/
root@tcserver:/opt/tomcat/bin# ./startup.sh
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.

Check service is running. If eberything well, we shoud see the service is running.

root@habibza:/opt/tomcat/bin# ps faxu | grep tomca

And then check port tomcat running in 8080.

root@habibza:/opt/tomcat/bin# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 820/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 892/sshd: /usr/sbin
tcp6 0 0 :::22 :::* LISTEN 892/sshd: /usr/sbin
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 8224/java
tcp6 0 0 :::8080 :::* LISTEN 8224/java

Lets open browser, and type http://ip-address-tomcat:8080

See also  Install Unbound Ubuntu 20.04/22.04 with Compile

Make Tomcat as a Service

At first, shutdown service tomcat.

root@habibza:/opt/tomcat/bin# cd /opt/tomcat/bin
root@habibza:/opt/tomcat/bin# ./shutdown.sh
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED

For easy manage tomcat service, we have to creating SystemD unit file. Open your text editor and create a tomcat.service unit file.

vim /etc/systemd/system/tomcat.service

Paste the following configuration:

[Unit]
Description=Tomcat 9 servlet container
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 -Djava.awt.headless=true"

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
Note : Modify the JAVA_HOME variable if the path to your Java installation is different.

Save and close the file and notify systemd.

systemctl daemon-reload
systemctl enable --now tomcat
systemctl start tomcat
systemctl status tomcat
tomcat status not running

if we get error “Permission denied”, please check permission in /opt/tomcat or re-run chown -R tomcat: /opt/tomcat again.

This error occurs because we previously execute ./startup from the bin folder with root access mode. And files create files in /opt/tomcat/logs/catalina.out have root access mode.

tomcat status running

Hurray, our tomcat is running and ready to deploy apps here. That is Install Tomcat9 in Ubuntu 20.04. May be it’s helpful, please feel free to leave a comment if you have any questions and I’ll appreciate it.


Let's Buy Me Coffee.

Buy Me a Coffee at ko-fi.com

https://saweria.co/habibzain
https://ko-fi.com/habibzain
habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Ansible Ubuntu 20.04

Introduction Ansible is an open-source automation tool that simplifies configuration management, application deployment, and task automation. It is designed to be simple, lightweight, and...
habibzain
1 min read

Leave a Reply

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

Never miss good article from us, get weekly updates in your inbox