How to Install Tomcat 10 on Ubuntu 22.04

Introduction:

In this extensive guide, we will walk you through the step-by-step process of installing Apache Tomcat 10 on your Ubuntu 22.04 system. Apache Tomcat is a widely-used open-source application server that enables you to run Java Servlets and JavaServer Pages (JSP). By the end of this tutorial, you will have Apache Tomcat 10 up and running, ready to host and serve your Java web applications on your Ubuntu 22.04 machine.

Before we begin, let’s ensure you have the following prerequisites in place:

  • A computer running Ubuntu 22.04.
  • Access to a terminal or command-line interface.
  • A user account with sudo privileges.
  • A stable internet connection.

Now that we have the prerequisites covered, let’s proceed with the installation.

  1. Update Your System:

Before installing any new software, it’s essential to update your system to ensure you have the latest package information. Open a terminal and run the following commands:

sudo apt update
sudo apt upgrade -y

This will update the package list and upgrade any installed packages to their latest versions.

  1. Install Java Development Kit (JDK):

Apache Tomcat requires Java to run. While Ubuntu 22.04 comes with OpenJDK pre-installed, it’s a good practice to install a specific version of the JDK to ensure compatibility with Tomcat 10. You can install the OpenJDK 11 with the following command:

sudo apt install openjdk-11-jdk -y

Once the installation is complete, verify the Java version by running:

java -version

You should see output indicating the installed Java version.

  1. Download and Extract Apache Tomcat:

Now, let’s download and extract Apache Tomcat 10 to your Ubuntu 22.04 system:

Step 1: Visit the Apache Tomcat Website

Go to the Apache Tomcat download page using your web browser. You can find it at the following URL:

Apache Tomcat Downloads

Step 2: Choose a Tomcat 10 Distribution

Scroll down to the “Binary Distributions” section and choose the Core distribution that corresponds to your system’s architecture. You’ll typically want the “tar.gz” version for Linux. Right-click the link and select “Copy link address” to get the download URL.

Step 3: Download Apache Tomcat

Back in your terminal, use the “wget” command to download Apache Tomcat. Replace URL with the link address you copied in the previous step:

wget URL

For example, if the URL is https://downloads.apache.org/tomcat/tomcat-10/v10.0.14/bin/apache-tomcat-10.0.14.tar.gz, you would run:

wget https://downloads.apache.org/tomcat/tomcat-10/v10.0.14/bin/apache-tomcat-10.0.14.tar.gz

Step 4: Extract Apache Tomcat

Extract the downloaded Apache Tomcat archive with the following command:

tar -xzvf apache-tomcat-10.*.*.tar.gz

Replace *.*.* with the actual version number you downloaded. For example:

tar -xzvf apache-tomcat-10.0.14.tar.gz

This will create a directory with the same name as the archive, such as apache-tomcat-10.0.14.

  1. Configure Environment Variables:

To run Apache Tomcat, we need to configure some environment variables. Open the .bashrc file in your home directory for editing:

nano ~/.bashrc

Add the following lines at the end of the file:

export CATALINA_HOME=~/apache-tomcat-10.*.*
export PATH=$PATH:$CATALINA_HOME/bin

Replace *.*.* with the actual version number you downloaded. For example:

export CATALINA_HOME=~/apache-tomcat-10.0.14
export PATH=$PATH:$CATALINA_HOME/bin

Save the file by pressing Ctrl + O, then press Enter. Exit the editor by pressing Ctrl + X.

To apply the changes, source the .bashrc file:

source ~/.bashrc
  1. Start and Test Apache Tomcat:

You can now start Apache Tomcat and test whether it’s running correctly. Use the following command to start Tomcat:

catalina.sh run

Apache Tomcat will start, and you will see logs indicating its status. If there are no errors, open a web browser and navigate to:

http://localhost:8080

You should see the Apache Tomcat default homepage, confirming that Tomcat is running.

To stop Apache Tomcat, press Ctrl + C in the terminal where it’s running.

  1. Configure Tomcat Users:

Apache Tomcat provides a user management system that allows you to secure your Tomcat installation. To configure Tomcat users, follow these steps:

Step 1: Open the Tomcat Users Configuration File

Open the tomcat-users.xml file for editing:

nano ~/apache-tomcat-10.*.*/conf/tomcat-users.xml

Replace *.*.* with the actual version number you downloaded. For example:

nano ~/apache-tomcat-10.0.14/conf/tomcat-users.xml

Step 2: Add User Roles

Inside the <tomcat-users> section, add user roles with appropriate permissions. For example, you can create an admin user with both manager and admin roles:

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

Replace "your_password" with a strong password of your choice.

Step 3: Save and Exit

Save the file by pressing Ctrl + O, then press Enter. Exit the editor by pressing Ctrl + X.

  1. Secure Your Tomcat Installation:

To enhance the security of your Apache Tomcat installation, consider the following best practices:

  • Change Default Ports: By default, Tomcat runs on port 8080. You can change this to a custom port by editing the server.xml file located in the conf directory of your Tomcat installation.
  • Disable Default Applications: Disable or remove the default applications that come with Tomcat if you don’t need them. You can do this by deleting the corresponding directories in the webapps directory.
  • Use Strong Passwords: Ensure that your Tomcat manager and admin user passwords are strong and not easily guessable.
  • Regularly Update Tomcat: Stay up to date with Apache Tomcat releases and apply updates and security patches as needed.
  1. Automate Apache Tomcat Startup (Optional):

If you want Apache Tomcat to start automatically when your system boots up, you can create a systemd service unit file.

Step 1: Create a systemd Unit File

Open a terminal and create a systemd unit file with the following command:

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

Step 2: Add Service Configuration

Add the following content to the tomcat.service file:

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

[Service]
Type=forking

Environment=CATALINA_PID=/tmp/tomcat.pid
Environment=CATALINA_HOME=~/apache-tomcat-10.*.*
Environment=CATALINA_BASE=~/apache-tomcat-10.*.*
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=~/apache-tomcat-10.*./bin/startup.sh
ExecStop=~/apache-tomcat-10.*./bin/shutdown.sh

User=<your_username>
Group=<your_group>

[Install]
WantedBy=multi-user.target

Replace <your_username> with your Ubuntu username and <your_group> with your user group. Additionally, replace *.*.* with the actual version number you downloaded.

Step 3: Save and Exit

Save the file by pressing Ctrl + O, then press Enter. Exit the editor by pressing Ctrl + X.

Step 4: Enable and Start the Service

Enable the Tomcat service to start automatically on boot:

sudo systemctl enable tomcat.service

Start the Tomcat service:

sudo systemctl start tomcat.service

You have now configured Apache Tomcat to start automatically when your system boots up.

  1. Conclusion:

In this comprehensive guide, we have covered the step-by-step process of installing Apache Tomcat 10 on your Ubuntu 22.04 system. Apache Tomcat is a versatile application server that enables you to run Java web applications efficiently. With Apache Tomcat up and running, you can develop and deploy Java Servlets and JSP applications with ease. Additionally, we have discussed securing your Tomcat installation and automating its startup for added convenience. You are now well-prepared to harness the power of Apache Tomcat for your web development projects on Ubuntu 22.04.

Leave a Comment