How to install Apache Tomcat 9 on Ubuntu 18.04 LTS

Apache Tomcat is a web server and servlet container developed by the Apache Software Foundation.

-Advertisement-

Apache Tomcat is a web server and servlet container developed by the Apache Software Foundation. It implements several Java EE technologies including Java Servlet, JavaServer Pages, Java WebSocket, and Java Expression language.

In today’s tutorial, we are going to show you how to install and configure Apache Tomcat 9 with Oracle Java 8.

Step 1) Install Oracle Java

Enter the following commands one at a time in the Terminal to add the Java repository, update the system and install Java.

sudo add-apt-repository ppa:webupd8team/java
-Advertisement-
sudo apt-get update
sudo apt-get install oracle-java8-set-default

Select “yes” for the agreement and installation will continue. After installation is complete, you can check the Java Version and Java Home after installation as follows:

sudo java -version
-Advertisement-
sudo echo $JAVA_HOME

Step 2) Create a User for Tomcat

Typically, we don’t recommend you to run Tomcat under the root user for security purpose. Rather, we advise you to create a new user and group. Additionally, we are going to install tomcat to “/opt/tomcat” folder to use as a Tomcat user home directory.

Start with creating a group and user.

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

Step 3) Download and install Tomcat

Tomcat 9 binary release can be downloaded from official download page. You can download “zip” or “tar.gz” file from under the binary distribution in the “core” section.

Tomcat Download
Tomcat Download

We are going to install Tomcat to “/opt”directory. So we will download package to that location.

Change directory to /opt

 cd /opt

Copy the download URL and paste it in the below wget command to download Tomcat Package:

sudo wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.14/bin/apache-tomcat-9.0.14.tar.gz

Extract the tar package.

sudo tar -xvzf apache-tomcat-9.0.14.tar.gz

Rename the extracted file as tomcat.

sudo mv apache-tomcat-9.0.14 tomcat

Modify ownership and permissions of Tomcat folder.

sudo chown -R tomcat: tomcat

Give executed permission to “bin” directory.

sudo chmod o+x /opt/tomcat/bin/

Step 4) Create a “systemd” file for Tomcat service management

To start and stop Tomcat service we will create a file called “tomcat.service” in the “/etc/systemd/system”.

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

Add the following content to the file.

[Unit]
Description=Tomcat 9 Server
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/java-8-oracle"
Environment="JAVA_OPTS=-Xms512m -Xmx512m"

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

UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Then save and the exit file.

Restart “systemctl deamon”.

sudo systemctl daemon-reload

Start Tomcat service.

sudo systemctl start tomcat

Monitor Tomcat logfile.

tail -f /opt/tomcat/logs/catalina.out

If there is no error you will get similar to below output:

Tomcat Starting
Tomcat Starting

Check the Tomcat service status.

sudo systemctl status tomcat

Tomcat Status
Tomcat Status

Enable Tomcat service on system boot:

sudo systemctl enable tomcat

Default Tomcat port is “8080” So need to allow that port on Ubuntu firewall.

sudo ufw allow 8080/tcp

Check firewall status:

sudo ufw status

Step 5) Configure Tomcat Web Management Interface

Edit the tomcat-users file as follows to add the following line to before “</tomcat-users>”. Obviously, you will have to replace the role name, username and password as desired in the following command:

sudo vim /opt/tomcat/conf/tomcat-users.xml
<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<user username="tomcatadmin" password="abc123" roles="admin-gui,manager-gui"/>

Restart Tomcat for the new configuration to take into effect.

sudo systemctl restart tomcat

Step 6) Test Tomcat

If you made this far successfully, you should be able to browse the Tomcat server. Launch the following URL in the browser.

http://serverIp:8080

You should see the page as follows:

Tomcat Home Page
Tomcat Home Page

Click on the manager app or host Manager. You should see a prompt for the user name and password. Enter tomcat username and password that was created on “tomcat-users.xml” file in step 5.

Authentication
Authentication

If everything went well, you should get an “Access Denied” error. This is completely normal.

Access Denied
Access Denied

The reasoning behind this error is that the Tomcat web management interface is configured to restrict access to the apps Manager and Host Manager only from the localhost. Therefore remote IP access should be enabled. This is not recommended for production environments.

However, here is how to remove those restrictions and enable our public IP.

Open the following file for Manage App settings:

sudo vim /opt/tomcat/webapps/manager/META-INF/context.xml

For the Host Manager configurations:

sudo vim /opt/tomcat/webapps/host-manager/META-INF/context.xml

Modify the following section in both files. Replace your public IP with “yourIP” in the command below.

<Context antiResourceLocking="false" privileged="true" >
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|yourIP" />
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter$LruCache(?:\)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

Configuration
Configuration

Restart Tomcat and then browse Tomcat again, following by clicking on Manager App or Host Manager. You should see no problems now.

sudo systemctl restart tomcat

App Manager

App ManagerHost Manager

Host Manager
Host Manager

Server Status

Status
Status

Congratulations! You have now successfully installed and configured Tomcat 9. How did the instructions work for you? Let us know your feedback in the comments below and don’t forget to share the tutorial with your friends!

-Advertisement-
Darshana
Hey! I'm Darshana, a Linux / DevOps Engineer and also a contributor to FOSS Linux. I enjoy working on various kind of Linux distributions and cloud technologies. During my free time, I love to swim and hike across nature trails. Linux is my love and I'm here to share all my learnings with all of you! Hope you enjoyed reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,639FansLike
361FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

Top 20 Git Commands with Practical Examples

If you are here reading this post, there is a high probability that you have heard or interacted with Github, and you now want to learn Git. Before we continue with showing you some of the cool Git commands, let's understand the difference between Git and GitHub.

Top 10 New Features in Linux Kernel 5.7

Linus Torvalds has announced the release of Linux Kernel 5.7 after seven weeks of development. The release announcement comes as a piece of exciting news as it brings a host of new features for the hardware manufacturers as well as the developers.

How to install CMake on Ubuntu

CMake is a cross-platform free and open-source software tool designed to build, test, and package the software. CMake uses a simple platform and compiler-independent configuration files to control the software compilation process.

How to install Lightworks on Ubuntu

Even though Linux may not get a native installer of video editing software like Adobe Premiere or Final Cut Pro, that doesn't mean there are no industry standards tools available. Lightworks is non-linear editing (NLE) video mastering app for Windows, Linux, and macOS. Installing it on Ubuntu is simple due to deb package availability.

How to install DaVinci Resolve on Fedora

Davinci Resolve is a professional application used for color correction, video editing, visual effects, and motion graphics. It is one of the extensively used software by movie industries located in Hollywood.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

Ubuntu MATE 20.04 LTS Review: Refinement at its Best

Ubuntu MATE 20.04 LTS was released a week ago after two years of development. Official updates and security patches will be provided until April 2025. I have installed it on my test laptop for a spin and here are my observations based on almost a week usage.

All about Ubuntu editions and which version should you use?

Ubuntu is one of the most popular Linux distributions developed and released by Canonical, and not without reason. It has very enriched repositories, with support for all the programs you could ever need.

5 Best Application Launchers for Ubuntu

Ubuntu is one of the most used Linux distributions worldwide. It is also the reason why it has the maximum number of available programs for itself. Today we are going to talk about one category of those programs, the application launchers.

7 Best Ways to Kill Unresponsive Programs in Linux

For dealing with a frozen app or desktop, you can't use the CTRL+ALT+DEL in Linux system. Instead, there are powerful alternatives that come in handy in frustrating situations. We pick the best methods available for you.