How to Install Tomcat 9 on Ubuntu 20.045 min read

Introduction

Apache Tomcat is a Java servlet container and an open-source web server. It is one of the most widely used programming languages for creating Java-based websites and applications. Tomcat is a lightweight framework that is simple to use and has a wide ecosystem of add-ons. This tutorial describes how to install and configure Tomcat 9 on Ubuntu 20.04.

Note: If you are using CentOS, then refer our article How to install Tomcat 9 on CentOS 8.

Install Java in Ubuntu 20.04

1. First we will make sure our system is up-to-date

sudo su
apt update

2. We need to verify if java is installed

java -version
java -version

3. If we do not have Java installed, we will receive the output shown above. We can install java with the below command.

apt install openjdk-8-jdk -y
java -version
java -version

Install Tomcat 9 on Ubuntu 20.04

Note: At the time of this writing, the latest version of Tomcat is 9.0.41. If you would like to download the latest version, visit this download link for Tomcat to check for the newest version. Copy the tar.gz file link from the Binary Distributions and replace it with the link in Step 2 of this tutorial.

1. First, we need to create a group and a user for Tomcat.

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

2. Download tomcat tar file

cd /tmp
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.41/bin/apache-tomcat-9.0.41.tar.gz
downloading tomcat 9 on ubuntu

3. Create a directory for tomcat and unpack the tar file in it

mkdir /opt/tomcat
tar -xvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1

4. Provide permissions to the tomcat directory

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/

5. Open a file called tomcat.service in the /etc/systemd/system directory by executing the below command

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

Paste the below content in the file:

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre
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

Save and exit.

6. Execute the below command to reload the daemons

systemctl daemon-reload

7. Start the Tomcat service by executing the below command

systemctl start tomcat

8. Check tomcat status with the below command

systemctl status tomcat
installed tomcat 9 on ubuntu

Note: Press q to come out of the status screen.

Now you can access the tomcat service on your browser. If you have installed tomcat in your local system, then enter the URL: http://localhost:8080. If you have installed it on a cloud instance, then enter the URL: http://<public_ip>:8080

tomcat webpage

Done. We have successfully installed Tomcat 9 on Ubuntu 20.04.


Change Tomcat port

If you have any other service already running on port number 8080, then you with either have to stop that service and then start tomcat to access it on the browser. Or else, you can change the tomcat port so that there is no conflict between the port and services. Follow the below steps to change the tomcat port,

  • Edit the server.xml file in tomcat directory to change the port number. Execute the below command to edit the service.xml file
sudo nano /opt/tomcat/conf/server.xml
  • Move down and find the below lines in configuration file
   <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
  • Change the port number here from 8080 to 8081 or any other port that is free in your system
   <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"                                                     
               redirectPort="8443" />
  • Save and exit by pressing Ctrl X -> Y -> Enter keys
  • Now restart tomcat with the below command for the changes to take effect
sudo systemctl restart tomcat

Now you can access the Tomcat webpage with the new port number.


Share:

Leave a Reply