How To Install Apache Tomcat In Ubuntu 20.04 LTS?


Apache Tomcat or simply Tomcat is a web server and servlet container that is used to serve Java applications. It provides a pure Java HTTP server environment to run these java applications.

Tomcat is developed and maintained by the open community of developers and released under the Apache license. In this article, we will discuss installing and configuring tomcat in Ubuntu/ Linux Mint or distributions based on them.

How to install java?

Tomcat requires Java to run web applications. So before we start installing tomcat we should have java installed in our system. To install java use the following commands in your terminal –

First, update the package index of apt with-

sudo apt-get update

And then execute –

sudo apt-get install default-jdk

You can verify the installation with –

java --version

For more details, you can read the Java installation guide.

Create a user tomcat

For security reasons, you should not run tomcat under the root user. We will create a new system user and a group named tomcat to run the Tomcat services. The newly created user will have the home directory /opt/tomcat. Use the following command to create user and group –

sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat

Now the tomcat user is setup. Let’s download and install the Apache Tomcat in our system.

Download and install Tomcat

At the time of writing this article, Tomcat 9.0.33 is a stable version of Tomcat available. You can download the latest .tar.gz file from the core list under the binary distribution section of the download page. Open the download page and click on the link as shown in the image below –

Or copy the link and use the wget command to download it-

Change to the directory where you want to download it –

cd Download/
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.33/bin/apache-tomcat-9.0.33.tar.gz

Once it gets downloaded extract it into the directory where you want to install it. First, move to the directory where the file is downloaded if you already in that location then there is no need to run this command –

cd Download/

Execute the following command in your terminal –

sudo tar xf apache-tomcat-9*.tar.gz -C /opt/tomcat --strip-components=1

Grant required permissions

We need to provide the required permission to tomcat user so that it can access the tomcat directory and files etc. First, move to the directory where you extract the tomcat files.

cd /opt/tomcat

Give the tomcat group ownership on tomcat installation directory with –

sudo chgrp -R tomcat /opt/tomcat

Move to the extracted folder of tomcat and update the read and execute permissions of the conf directory and its content for the tomcat group. Use the following command –

sudo chmod -R g+r conf
sudo chmod g+x conf

Next, make the tomcat user owner of webapps, temp, work and logs directories with the following command –

sudo chown -R tomcat webapps/ temp/ work/ logs/

Create 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 –

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

Now create a systemd service file with the following command-

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

Paste the following content inside this file. Modify the value of JAVA_HOME if necessary to match the value that you found on your system.

[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/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 the content of the file by pressing ctrl+s and then exit from the editor by pressing ctrl+x.

Now reload the system daemon with the following command, so that it gets updated with the recent changes –

sudo systemctl daemon-reload

Start the tomcat by using –

sudo systemctl start tomcat

Check if Tomcat service is running or not with –

sudo systemctl status tomcat

If service is running correctly you will see the following output in your terminal –

Adjust the firewall

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

sudo ufw allow 8080

Once this command executed successfully, your firewall gets modified. Now you can test your tomcat server.

Test the Tomcat server

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

For example –

http://127.0.0.1:8080

This will display the default page of apache tomcat.

Conclusion

The apache tomcat server is now ready you can use this to deploy your Java web applications. With the help of this guide you install and setup apache tomcat on Ubuntu 20.04 LTS.

Leave a Comment

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