apache

How To Install Apache Tomcat on Ubuntu 24.04

Welcome to the Greenhost.Cloud blog! In today’s post, we will guide you through the process of installing Apache Tomcat on Ubuntu 24.04. Apache Tomcat is an open-source application server that allows you to deploy and run Java web applications. With its widespread adoption and robust feature set, Tomcat is a favorite among developers.

Prerequisites

Before we start the installation process, ensure you have the following prerequisites:

  • A server or machine running Ubuntu 24.04.
  • A non-root user with sudo privileges.
  • Java Development Kit (JDK) installed. Tomcat requires Java to run, so let’s check if it’s installed.

Step 1: Install Java

Apache Tomcat requires JDK to run Java applications. You can install OpenJDK using the following commands:

  1. First, update your package index:
   sudo apt update
  1. Install OpenJDK 11 (or any other version you prefer):
   sudo apt install openjdk-11-jdk
  1. Verify the installation:
   java -version

You should see output indicating the installed version of Java.

Step 2: Download Apache Tomcat

  1. Go to the Apache Tomcat download page to find the latest stable release. As of this writing, the latest version is 9.0.x.
  2. To download Tomcat, use the following commands. Adjust the version number based on the latest release:
   cd /opt
   sudo wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.76/bin/apache-tomcat-9.0.76.tar.gz
  1. Extract the downloaded tar.gz file:
   sudo tar -xvf apache-tomcat-9.0.76.tar.gz
  1. Rename the extracted folder for easier access:
   sudo mv apache-tomcat-9.0.76 tomcat

Step 3: Set Permissions

Set the necessary permissions for the Tomcat installation:

sudo chown -R $USER:$USER /opt/tomcat

Step 4: Configure Environment Variables

To run Tomcat conveniently, it’s helpful to set environment variables. You’ll need to edit the ~/.bashrc file:

  1. Open the .bashrc file in a text editor:
   nano ~/.bashrc
  1. Add the following lines at the end of the file:
   export CATALINA_HOME=/opt/tomcat
  1. Save the changes and update your terminal session:
   source ~/.bashrc

Step 5: Start Apache Tomcat

To start Tomcat, navigate to the bin directory of Tomcat and execute the startup script:

cd $CATALINA_HOME/bin
./startup.sh

Step 6: Accessing Tomcat

The default port for Tomcat is 8080. You can access the Tomcat server by navigating to http://your_server_ip:8080 in your web browser. You should see the Tomcat welcome page, which confirms that Tomcat is up and running.

Step 7: Set Up Tomcat as a Service (Optional)

To manage Tomcat as a service, we can create a systemd unit file:

  1. Create a new service file:
   sudo nano /etc/systemd/system/tomcat.service
  1. Add the following configuration to the file:
   [Unit]
   Description=Apache Tomcat Web Application Container
   After=network.target

   [Service]
   Type=simple
   User=your_username
   Group=your_group
   Environment=CATALINA_HOME=/opt/tomcat
   Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
   ExecStart=/opt/tomcat/bin/startup.sh
   ExecStop=/opt/tomcat/bin/shutdown.sh

   [Install]
   WantedBy=multi-user.target

Replace your_username and your_group with the corresponding values for your system.

  1. Reload systemd to recognize the new service:
   sudo systemctl daemon-reload
  1. Start the Tomcat service:
   sudo systemctl start tomcat
  1. Enable it to start on boot:
   sudo systemctl enable tomcat

Conclusion

Congratulations! You have successfully installed Apache Tomcat on your Ubuntu 24.04 server. You can now deploy Java web applications using this powerful application server. If you encounter any issues or have questions, feel free to leave a comment below, or visit the Apache Tomcat documentation for more detailed information.

Stay tuned for more tutorials and guides on Greenhost.Cloud! Happy coding!