How to Install Tomcat on Ubuntu 20.04

Install Tomcat on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure Tomcat on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Apache Tomcat is an open-source web server and Java servlet container. It is one of the most popular choices for building Java-based websites and applications. Tomcat is lightweight, easy to use, and has a robust ecosystem of add-ons.

Install Tomcat on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install Java.

Java is required for Tomcat to serve Java applications. You can install Java 11 using the following command:

sudo apt install default-jdk

You can verify the installation with:

java --version

Step 3. Create Tomcat Service Account.

First, create a new tomcat group called tomcat:

sudo groupadd tomcat

Next, run the commands below:

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

Step 4. Install Apache Tomcat.

Now download the latest binary release of Tomcat for the official Tomcat downloads page:

sudo mkdir /opt/tomcat
cd /tmp
curl -O http://mirrors.estointernet.in/apache/tomcat/tomcat-9/v9.0.35/bin/apache-tomcat-9.0.35.tar.gz
sudo tar xzvf apache-tomcat-9.0.35.tar.gz -C /opt/tomcat --strip-components=1

Then, Setup correct permissions for tomcat user:

cd /opt/tomcat
sudo chgrp -R tomcat /opt/tomcat
sudo chmod -R g+r conf
sudo chmod g+x conf
sudo chown -R tomcat webapps/ work/ temp/ logs/

Step 5. Create Systemd Unit File.

To run Tomcat as a service you need to setup this with a systemd service file. We are going to use tomcat as a service so we need to create a tomcat service file. But before that, we have to find the location where java is installed we will use this path in the tomcat service file. Use the following command to locate it:

sudo update-java-alternatives -l

Now copy the highlighted string and paste it in JAVA_HOME variable. And add /jre at the end of the string So that the whole string should look like this:

java-1.11.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.11.0-openjdk-amd64

Next, create a systemd service file with the following command:

sudo nano /etc/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

After that, run the commands below to reload systemd profiles and enable tomcat service:

sudo systemctl daemon-reload
sudo systemctl start tomcat.service
sudo systemctl enable tomcat.service

Step 7. Configure Firewall.

Tomcat uses port 8080 to accept the requests. Allow the traffic to this port with the following command:

sudo ufw allow 8080

Step 8. Test the Tomcat Installation.

To test tomcat open a browser and type the domain or IP of the server followed by :8080. Type the following:

http://domain_or_server_ip:8080

You should get following output for successful installation:

Install Tomcat on Ubuntu 20.04

That’s all you need to do to install Tomcat on Ubuntu 20.04 LTS Focal Fossa. I hope you find this quick tip helpful. Don’t forget to share your valuable queries/suggestions in the below comment box & also drop your worthwhile feedback.