How to Install Tomcat 9 on Ubuntu 24.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install Tomcat 9 on Ubuntu 24.04, let's briefly understand – What is Tomcat 9?

Tomcat 9 is an open-source Java servlet container developed by the Apache Software Foundation. It serves as a web server, handling Java-based web applications with ease. With its versatility and robust features, Tomcat 9 is renowned for its reliability and scalability.

This software efficiently manages JavaServer Pages (JSP) and Java Servlets, aiding seamless web application deployment. Tomcat 9 supports various operating systems and is a popular choice among developers for Java web development projects. Its user-friendly interface and extensive documentation make it an ideal solution for hosting dynamic web content.

In this tutorial, you will install Tomcat 9 on Ubuntu 24.04. We will also address a few FAQs on how to install Tomcat 9 on Ubuntu 24.04.

Advantages of Tomcat 9

  1. Ease of Use: Tomcat offers a user-friendly interface, simplifying web application deployment.
  2. Scalability: It supports scalable applications, accommodating varying user loads effectively.
  3. Reliability: Known for its stability, ensuring consistent performance.
  4. Extensive Documentation: Well-documented features aid developers in utilizing Tomcat effectively.
  5. Support for Java Technologies: Tomcat seamlessly manages JavaServer Pages and Servlets, ideal for Java-based web projects.

1. How to Install Tomcat on Ubuntu 24.04

To install Tomcat on Ubuntu 24.04, you can use the Tomcat tar file. First, download the Tomcat package with the wget command. However, before you begin the Tomcat installation, ensure that Java is already installed on your system.

2. Installing Prerequisite (Java)

Java is required to run Tomcat. Therefore, before installing Tomcat, ensure that Java is installed on your Ubuntu 24.04 system. We'll use OpenJDK, the standard Java development kit.

Start by updating your software package lists with the apt command to ensure you have access to the latest Java version:

sudo apt update

Next, install the default Java JDK by running:

sudo apt install default-jdk

After installing the Java JDK, you can verify the installation by running the following command:

java -version

With the Java JDK installed, the next step is to create a dedicated user for Tomcat.

3. Creating a Tomcat User

For security reasons, it's not advisable to run Tomcat under the root account. To address this, we'll create a separate user specifically for running Tomcat.

The first step is to create a group for Tomcat services:

sudo groupadd tomcat

After creating the Tomcat group, the next step is to add a new user to it. We'll create a Tomcat user with a home directory at /opt/tomcat and assign this user to the Tomcat group to manage permissions for the Tomcat service.

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

4. Installing Tomcat Server on Ubuntu 24.04

After adding a new user for Tomcat, the next step is to download the latest stable binary version of Tomcat. In this case, Tomcat 10 will be downloaded, but it's advisable to verify the latest release and download it using the curl command.

First, change to a temporary directory to facilitate the automatic removal of unused items once the Tomcat installation is complete.

cd /tmp

Go to the download page for the Tomcat version you need. Copy the URL for the binary and replace it in the command below:

curl -O https://downloads.apache.org/tomcat/tomcat-9/v9.0.91/bin/apache-tomcat-9.0.91.tar.gz

The curl command will download the Tomcat binary files and save them locally on your system.

5. Updating and Configuring Admin Users

Even though Tomcat is installed, permissions still need to be configured. The Tomcat user created earlier requires full access to the Tomcat directory (/opt/tomcat) to run the server effectively.

First, create the Tomcat directory:

sudo mkdir /opt/tomcat

Now, navigate into that directory:

cd /opt/tomcat

Next, extract the Tomcat binary file using the tar command:

sudo tar xzvf /tmp/apache-tomcat-*tar.gz -C /opt/tomcat --strip-components=1

This will extract the Tomcat binary into the /opt/tomcat directory.

sudo tar xzf apache-tomcat-*.tar.gz -C /opt/tomcat

Now, grant the Tomcat user full permissions for the /opt/tomcat directory using the chgrp command:

sudo chgrp -R tomcat /opt/tomcat

The Tomcat user also requires permission to read the files in the conf directory. To grant this access, run the following chmod command:

sudo chmod -R g+r conf

Additionally, the Tomcat user needs permission to navigate to the directory itself. Use the chmod command to grant execute permissions for the Tomcat group on the conf directory:

sudo chmod g+x conf

Likewise, grant the Tomcat user permissions for the work, temporary, and logs directories:

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

6. Creating a systemd User File

After installing Tomcat, it needs to be configured and set up to run as a service, enabling it to start automatically at boot. To achieve this, we'll create a systemd unit file named tomcat.service. You can use any text editor to create this file, as shown below:

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

After creating the new file, paste the following content into it. Make sure to adjust the JAVA_HOME variable if your system's Java location differs from the one specified.

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment="JAVA_HOME=/usr/lib/jvm/java-1.21.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

To save the file, exit the editor.

After creating the service file, reload the system daemon to ensure it is recognized in the system configuration:

sudo systemctl daemon-reload

To start the Tomcat service, first navigate to the Tomcat bin directory:

sudo chmod 777 /opt/tomcat/bin
cd /opt/tomcat/bin
ls

Finally, execute the following command to start the Tomcat service:

sudo ./startup.sh run

You can also use the following command to start the Tomcat service directly:

sudo systemctl start tomcat

To check the status of the Tomcat service, use the following command:

sudo systemctl status tomcat

If the Tomcat service is not active, enable it by running the following command:

sudo systemctl enable tomcat

7. Configuring the System Firewall for Tomcat

After starting the Tomcat service, the next step is to allow traffic through port 8080 in the UFW firewall. This will enable Tomcat to communicate with external or local networks.

sudo ufw allow 8080

You can check if UFW is already enabled by running:

sudo ufw status

If UFW is disabled, you can enable it by running:

sudo ufw enable

By default, UFW blocks all incoming traffic. To permit SSH access (typically on port 22), run:

sudo ufw allow 22

Now, enable remote access to port 9200:

sudo ufw allow from external_IP to any port 9200

Note: Replace <your_remote_machine_ip> in the command with your local remote machine's IP address. To find your IP address, use the ip addr command.

Finally, verify whether the UFW rule has been added.

sudo ufw status

8. Creating Tomcat User Applications Accounts

Next, create a user account for the Tomcat application to enhance security. To do this, edit the tomcat-users.xml file.

Open the file using your preferred text editor, here nano is used:

sudo nano /opt/tomcat/conf/tomcat-users.xml

Copy the provided code and paste it into the file. Be sure to customize the names and passwords as needed.

<!-- user manager can access only the manager section -->

<role rolename="manager-gui" />

<user username="manager" password="_SECRET_PASSWORD_" roles="manager-gui" />

<!-- user admin can access manager and admin section both -->

<role rolename="admin-gui" />

<user username="admin" password="_SECRET_PASSWORD_" roles="manager-gui,admin-gui" />

After saving the changes, exit the editor to apply them.

tomcat-users.xml — Admin User

<tomcat-users . . .>

<tomcat-users . . .>

<user username="admin" password="password" roles="manager-gui,admin-gui"/>

</tomcat-users>

To grant remote hosts access to the Tomcat server, you need to edit the context.xml file. You can configure this file to allow access for a specific remote host or for all hosts.

To configure the manager type, edit this file:

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

Find the section in the file that restricts connections based on IP address. To permit connections from any machine, uncomment this section:

<Context antiResourceLocking="false" privileged="true" >

 <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"

 sameSiteCookies="strict" />

 <!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"

 allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

 ...

</Context>

Similarly, for host type, edit this file:

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

Comment out the below section to allow the connection from anywhere.

<Context antiResourceLocking="false" privileged="true" >

 <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"

 sameSiteCookies="strict" />

 <!--<Valve className="org.apache.catalina.valves.RemoteAddrValve"

 allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

 ...

</Context>

After saving both these files, you can exit the editor.

9. Accessing the Web Management Interface

Once the Tomcat server is configured, you can access the Tomcat Web interface from your web browser. By default, Tomcat operates on port 8080.

To reach the Tomcat server, enter the following address in your web browser:

http://server_domain_or_IP:8080/

In the command above, replace server_domain_or_IP with your server's IP address or domain name that points to the server.

For example, to access the Tomcat server on a local host, use the following command:

http://localhost:8080

After entering this address, you will see the Tomcat web interface:

To access the Manager App, click the "Manager App" option at the top right. Alternatively, you can access it by appending the /manager tag to the end of the server URL.

To access the Host Manager, click the "Host Manager" button. Alternatively, you can add the /host-manager tag to the end of the Tomcat server URL.

10. How to Uninstall Tomcat on Ubuntu 24.04

Before uninstalling the Tomcat server, ensure that its services are stopped. Use the following command to stop the Tomcat service:

sudo systemctl stop tomcat

If you manually downloaded the Tomcat file, use the following command to remove the Tomcat directory:

sudo rm -rf /opt/tomcat

If you installed Tomcat using the apt package manager, use the following command to remove it from your system:

sudo apt remove tomcat

To delete the dedicated user created for the Tomcat server, run the following command:

sudo userdel tomcat

By default, systemd unit files for services are stored in /etc/systemd/system. If you created a unit file for Tomcat (e.g., tomcat.service), you can remove it using the following command:

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

FAQs to Install Tomcat 9 on Ubuntu 24.04

What are the prerequisites for installing Tomcat 9 on Ubuntu 24.04?

To install Tomcat 9 on Ubuntu 24.04, you need a non-root user with sudo privileges and Java installed on your system. It's recommended to use OpenJDK version 11 or later.

Where is Tomcat installed by default on Ubuntu?

The default installation directory for Tomcat 9 on Ubuntu is /opt/tomcat, but it can be installed in any directory of your choice.

Can I configure multiple instances of Tomcat on Ubuntu?

Yes, you can install multiple instances of Tomcat by duplicating the installation directory and configuring them to run on different ports to avoid conflicts.

What ports does Tomcat use by default?

Tomcat uses port 8080 for HTTP requests by default. You can change this in the server.xml file.

Can I run Tomcat behind a reverse proxy?

Yes, Tomcat can be configured to run behind a reverse proxy like Nginx or Apache HTTP Server.

How do I remove a deployed application from Tomcat?

You can remove an application from the Manager interface or delete the corresponding WAR file from the webapps directory.

How can I check the available memory for Tomcat?

You can check the available memory by using the free -h command in the terminal.

Conclusion

We hope this tutorial helped you understand how to install Tomcat 9 on Ubuntu 24.04.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.