How to Install Tomcat 9 on Ubuntu 18.04/16.04 LTS

In this article, We are going to perform How to Install Tomcat 9 on Ubuntu 18.04/16.04 LTS or any other cloud platform like Amazon EC2, Azure VM, Google Cloud Compute, etc.,

Introduction

Apache Tomcat is an open source implementation of Java Server pages ,Java Servlet and Java Websocket Technologies developed by Apache Software foundation. It is most widely used to deploy JSP and Java Servlet applications.

Step 1: Install and verify java version

Tomcat requires Java JDK to be function, we can either install Open JDK or Oracle JAVA JDK. In this installation we have preinstalled Oracle Java 8, if you are not installed follow below link to install.

How to Download and Install Oracle Java 8 On Ubuntu 18.04/16.04 LTS

$ java -version

Output:

java version "1.8.0_241"

Java(TM) SE Runtime Environment (build 1.8.0_241-b07)

Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)

Step 2: Creating directory

First , we are creating directory before downloading and extracting tomcat 8 setup, Enter below command to create directory

$ sudo mkdir -p /opt/tomcat

Step 3: Creating tomcat user and group

We have to run tomcat with it’s own group and user without root privileges to run tomcat service. Enter below command to create group.

$ sudo groupadd tomcat

Enter below command to create user in tomcat group with /opt/tomcat directory permission .

$ sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Step 4: Download and Install Tomcat 9 on Ubuntu 18.04/16.04 LTS

Download the latest binary release version from tomcat 9 download page and configure it manually. In this article we are configuring tomcat 9.0.33 version and we are using wget and unzip commands to download and extract the setup, if you don’t have installed on your system, enter below command to install.

$ sudo apt install unzip wget

Change to /tmp directory and Download the setup using below command.

$ cd /tmp
$ sudo wget https://mirror.olnevhost.net/pub/apache/tomcat/tomcat-9/v9.0.33/bin/apache-tomcat-9.0.33.zip

Output:

wget https://mirror.olnevhost.net/pub/apache/tomcat/tomcat-9/v9.0.33/bin/apache-tomcat-9.0.33.zip

--2020-03-21 08:17:33--  https://mirror.olnevhost.net/pub/apache/tomcat/tomcat-9/v9.0.33/bin/apache-tomcat-9.0.33.zip

Resolving mirror.olnevhost.net (mirror.olnevhost.net)... 188.165.227.148

Connecting to mirror.olnevhost.net (mirror.olnevhost.net)|188.165.227.148|:443... connected.

HTTP request sent, awaiting response... 200 OK

Length: 11581027 (11M) [application/zip]

Saving to: ‘apache-tomcat-9.0.33.zip’

apache-tomcat-9.0.33.zip               100%[===========================================================================>]  11.04M  6.92MB/s    in 1.6s

2020-03-21 08:17:36 (6.92 MB/s) - ‘apache-tomcat-9.0.33.zip’ saved [11581027/11581027]

Once download is complete, you will see output like above. Unzip/extract and move to /opt/tomcat/ directory.

$ unzip apache-tomcat-*.zip
$ sudo mv apache-tomcat-9.0.33/*   /opt/tomcat/

To have control on over versions and updates, we will create symbolic link latest which will point tomcat installation directory. when newer version comes and when you want to upgrade then you have to change symlink to new latest verision.

$ sudo ln -s /opt/tomcat /opt/tomcat/latest

change the directory ownership permission to user and group  tomcat.

$ sudo chown -R tomcat: /opt/tomcat

Make all scripts in the bin location executable.

$ sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'

Step 5: Configure Environment variables

Next configure environment variables using below command

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

$ source ~/.bashrc

Step 6: Create Systemd unit file

To run Tomcat as a service we will create a new tomcat.service unit file in the /etc/systemd/system/ directory with the following contents:

$ sudo nano /etc/systemd/system/tomcat.service
/etc/systemd/system/tomcat.service
[Unit]

Description=Tomcat 9 servlet container

After=network.target


[Service]

Type=forking

User=tomcat

Group=tomcat


Environment="JAVA_HOME=/opt/jdk/jdk1.8.0_241"

Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"


Environment="CATALINA_BASE=/opt/tomcat/latest"

Environment="CATALINA_HOME=/opt/tomcat/latest"

Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"

Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"


ExecStart=/opt/tomcat/latest/bin/startup.sh

ExecStop=/opt/tomcat/latest/bin/shutdown.sh
[Install]

WantedBy=multi-user.target

Modify the value of JAVA_HOME value,If the path of your Java installation is different.

Notify systemd that we created a new unit file and start the Tomcat service by executing:

$ sudo systemctl daemon-reload
$ sudo systemctl start tomcat

You can check the service status with the below command:

$ sudo systemctl status tomcat

Output:

tomcat.service - Tomcat 9 servlet container
Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-03-21 09:36:31 UTC; 28min ago
Main PID: 78764 (java)
Tasks: 30 (limit: 9513)
CGroup: /system.slice/tomcat.service
└─78764 /opt/jdk/jdk1.8.0_241/bin/java -Djava.util.logging.config.file=/opt/tomcat/latest/conf/logging.properties -Djava.util.logging.manager=or

If there are no errors you can enable the Tomcat service to be automatically started at system startup:

$ sudo systemctl enable tomcat

Output:

Created symlink /etc/systemd/system/multi-user.target.wants/tomcat.service → /etc/systemd/system/tomcat.service.

Step 7: Adjust the Firewall

To allow traffic on port 8080 enter the below command:

$ sudo ufw allow 8080/tcp

Step 7: Test the Installation

Open your browser and type:  http://localhost_or_IP Address:8080 If installation is successful, you will see tomcat default page like below,

tomcat 9 web interface

Step 7: Start, Restart and Stop tomcat in Ubuntu

Below are commands to start,restart and stop tomcat service in ubuntu,

$ sudo systemctl start tomcat
$ sudo systemctl restart tomcat
$ sudo systemctl stop tomcat

Successfully We have performed How to Install Tomcat 9 on Ubuntu.

To learn more about Tomcat 8 features. Please refer official documentation.

Conclusion

In this article, We have covered install Tomcat 9 on Ubuntu 18.04/16.04 LTS, created tomcat user and group, downloaded tomcat and extracted,configured environment variables, created systemd unit file, opened firewall port.

Related Articles

How to Install Tomcat 9 on Ubuntu 20.04 LTS

How to Install Tomcat 8 on Ubuntu 18.04/16.04 LTS

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap