Tuesday, 31 December 2024

How to Install Tomcat on Ubuntu 24.04 LTS

 

How to Install Tomcat on Ubuntu 24.04 LTS

In This Article We Will Learn How to  install  Tomcat on Ubuntu 24.04 LTS.

Tomcat is a free and open-source Java Servlet container, a software application that executes Java Server Pages (JSP) and Java servlets. It is widely used for deploying Java-based web applications. We will walk you through the process step-by-step, including installing Java, creating a dedicated Tomcat user, downloading and extracting the Tomcat archive, configuring systemd, and verifying the installation.

Table of Contents

Prerequisites

  • AWS Account with Ubuntu 24.04 LTS EC2 Instance.
  • Java Development Kit (JDK) installed.

Step #1:Install Java on Ubuntu 24.04 LTS

To install Tomcat you should have Java installed. Tomcat relies on Java to function.

First update the package repository.

sudo apt update
How to Install Tomcat on Ubuntu 24.04 LTS 1

Install Openjdk (Java).

sudo apt install openjdk-17-jdk
How to Install Tomcat on Ubuntu 24.04 LTS 2

Step #2:Create a Tomcat User

For security reasons, it’s best not to run Tomcat as the root user. Let’s create a dedicated user and group.

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

This command creates a user named “tomcat” with a home directory at /opt/tomcat. The /bin/false shell prevents login access.

How to Install Tomcat on Ubuntu 24.04 LTS 3

Step #3:Install Tomcat on Ubuntu 24.04 LTS

Now lets install Tomcat. First navigate to the Tomcat official website and download the latest version of Tomcat 10. You can use wget for this purpose.

sudo wget https://www-eu.apache.org/dist/tomcat/tomcat-10/v10.1.24/bin/apache-tomcat-10.1.24.tar.gz -P /tmp
How to Install Tomcat on Ubuntu 24.04 LTS 4

extract it to the /opt/tomcat directory.

sudo tar -xvf /tmp/apache-tomcat-10.1.24.tar.gz -C /opt/tomcat
How to Install Tomcat on Ubuntu 24.04 LTS 5

Step #4:Update Permissions of Tomcat

Change ownership of the Tomcat directory to the tomcat user and group:

sudo chown -R tomcat:tomcat /opt/tomcat
How to Install Tomcat on Ubuntu 24.04 LTS 6

Step #5:Configure Tomcat as a Service

A systemd unit file tells systemd how to manage a service. Create a file named tomcat.service under the /etc/systemd/system directory using a text editor.

navigate to the /etc/systemd/system.

cd /etc/systemd/system
How to Install Tomcat on Ubuntu 24.04 LTS 7

Create a tomcat.service file using nano command.

sudo nano tomcat.service
How to Install Tomcat on Ubuntu 24.04 LTS 8

add the following content into it.

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

[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64"
WorkingDirectory=/opt/tomcat/apache-tomcat-10.1.24
ExecStart=/opt/tomcat/apache-tomcat-10.1.24/bin/startup.sh

[Install]
WantedBy=multi-user.target
How to Install Tomcat on Ubuntu 24.04 LTS 9

Save and close the file.

Adjust the JAVA_HOME environment variable path based on your OpenJDK installation location.

Step #6:Reload systemd and Start Tomcat

Reload the systemd daemon to apply the changes.

sudo systemctl daemon-reload
How to Install Tomcat on Ubuntu 24.04 LTS 10

Start the Tomcat service.

sudo systemctl start tomcat
How to Install Tomcat on Ubuntu 24.04 LTS 11

Enable Tomcat to start on boot.

sudo systemctl enable tomcat
How to Install Tomcat on Ubuntu 24.04 LTS 12

Now you can verify if your service is running properly or not by running your public ip address with port 8080 which is default port for Tomcat in url. You should see the Tomcat welcome page if everything is set up correctly.

How to Install Tomcat on Ubuntu 24.04 LTS 13

Conclusion:

In conclusion, installing Apache Tomcat on Ubuntu 24.04 is a straightforward process that involves updating your system, installing Java, creating a Tomcat user, downloading and configuring Tomcat, setting up a systemd service. By following these steps, you can easily install Tomcat and set up a robust environment for deploying Java-based web applications on your server.


SOURCE

No comments:

Post a Comment