Install Apache Tomcat And Nginx on Ubuntu 24.04

Jason Bodie
10 min readMay 16, 2024

--

Install Apache Tomcat 10 on Ubuntu 24.04
Apache Tomcat 10 and Nginx on Ubuntu 24.04

This tutorial will walk you through installing and setting up Apache Tomcat 10 on Ubuntu 24.04 with an Nginx reverse proxy serving our Tomcat dashboard. This tutorial will cover the following major topics:

By default, Tomcat is set up to communicate over localhost port 8080. To get access to the Tomcat manager and host manager pages we need to use Nginx as a reverse proxy to serve files over port 80.

If you want to follow along, consider doing so on DigitalOcean. Their cloud platform is extremely easy to use. Follow this affiliate link to get $200 in free credit.

What Is Apache Tomcat

Apache Tomcat is an open-source web server and servlet container developed by the Apache Software Foundation. It provides a Java-based environment for running and managing web applications, supporting features such as servlets, JavaServer Pages (JSP), and WebSockets.

Apache Tomcat is widely used in enterprise environments due to its lightweight yet powerful nature, making it an ideal choice for hosting dynamic websites and services. It also offers various security mechanisms and integration with popular databases, making it a versatile tool for developers.

Overall, Apache Tomcat plays a crucial role in enabling the deployment of robust and scalable web applications on the internet.

How To Install The Java Development Kit (JDK)

Java Development Kit (JDK) is an essential tool for running Tomcat. It provides the necessary components, such as the Java Virtual Machine (JVM), compiler, and libraries that are required to develop and run Java-based applications.

Without installing a JDK, Tomcat will not be able to compile and run your code. Additionally, it provides debugging tools, performance monitoring capabilities, and security measures that ensure your application runs smoothly.

Let’s start updating our package manager repository database:

sudo apt update

Then, install the JDK by running the following command:

sudo apt install default-jdk -y

After the installation, you can check the version using the following command:

java -version

The output should be similar to this:

# Expected Output
openjdk version "21.0.3" 2024–04–16
OpenJDK Runtime Environment (build 21.0.3+9-Ubuntu-1ubuntu1)
OpenJDK 64-Bit Server VM (build 21.0.3+9-Ubuntu-1ubuntu1, mixed mode, sharing)

The Java Development Kit has officially been installed.

How To Create A Dedicated Apache Tomcat 10 User On Ubuntu 24.04

Creating a dedicated Linux Tomcat user is important for the security and stability of the Tomcat server. A dedicated user allows for better control over who has access to the server, as well as providing a separate environment for running applications.

By having a specific user for Tomcat, we can limit its permissions and restrict it from accessing other parts of the system that it does not need access to. This helps protect sensitive data and prevents potential malicious attacks on the server.

Let’s start by adding a user with the following command.

sudo useradd -m -d /opt/tomcat tomcat

The ‘-m’ option stands for “make home directory.” When creating a new user, this option specifies that their home directory should be created automatically if it does not already exist.

The ‘-d’ option lets you specify the location of the user’s home directory instead of using the default location /home/username. If you take a look at the command above. We chose /opt/tomcat for Tomcat’s home directory.

We will use this directory later when we unpack the latest version of Tomcat.

Even though this account will not have login access, it’s important to set a password for security purposes.

Use the passwd command followed by the username to initiate the set password process. After typing in a password, hit enter.

sudo passwd tomcat

Also by default, Linux assigns /bin/bash shell access for each new account created using the useradd command. This is a potential security risk so we will restrict access to a shell for our tomcat user using the following command.

sudo chsh -s /sbin/nologin tomcat

Now that we have the JDK installed and the Tomcat 10 user created. We need to download the latest version of Tomcat 10 and install it.

How To Download And Install Apache Tomcat 10 On Ubuntu 24.04

To install Tomcat 10 on your server, we have to begin with the following steps. Let us start by creating a ‘tmp’ directory and traversing to it.

mkdir ~/tmp
cd ~/tmp

Use wget to download the latest version of Tomcat. If you’re interested in searching for a more recent version than the one being downloaded right now. You can search the following archive: https://dlcdn.apache.org/tomcat

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.24/bin/apache-tomcat-10.1.24.tar.gz

Linux wget is a command-line utility used for retrieving files from the internet.

Next, we will use tar to extract the Tomcat 10 archive that we just downloaded to the Tomcat home directory /opt/tomcat. This is the directory we specified earlier in the tutorial when we created the Tomcat user.

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

Now let's grant our dedicated tomcat user ownership of the home directory:

sudo chown -R tomcat:tomcat /opt/tomcat/
sudo chmod -R u+x /opt/tomcat/bin

Linux chown -R is a command used to recursively change the user and group ownership of files and directories in Linux. This means that not only will the specified file or directory have its ownership changed, but any subdirectories and their contents will also be affected.

Linux chmod -R u+x is a command used to change the permissions of a file or directory. It allows the user who owns the directory to have executable permission (represented by “+x”) on that file or directory recursively, meaning that it will apply to all files and subdirectories within it.

How To Configure The Tomcat Manager And Host Manager Roles

To access the manager and host-manager pages later on in the tutorial, we must create privileged users within Tomcat’s configuration.

By default, Tomcat users are defined in the /opt/tomcat/conf/tomcat-users.xml file. Let us open the file for editing using the Nano editor.

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

Add the code written below in between the <tomcat-users></tomcat-users> tags. Don’t forget to change the password if you need to. After completion, hit Ctrl+x, hit the y key, and hit Enter to save the file.

<!-- Filename: /opt/tomcat/conf/tomcat-users.xml --> 

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

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

In the code block above, you define two roles, the manager-gui role and the admin-gui role. These roles grant access to the manager and/or host manager pages. You also define two users, which will have access for their respective roles.

Let’s move on to creating a systemd service.

How To Create A Systemd Service For Tomcat 10

systemd is a system management tool used to control and manage various processes and services on a Linux operating system. It provides a framework for starting, stopping, monitoring, and restarting daemons or background services in an organized manner.

This allows for better performance, resource allocation, and efficient handling of dependencies between different processes. systemd services are configured using simple text files and can be easily customized to meet specific needs. They play a crucial role in the smooth operation of a Linux system by providing centralized control over all running processes.

Because we are creating a Tomcat service, we need to know where Java is located within our file system. You can look this up using the following command:

sudo update-java-alternatives -l

The output will be similar to this:

# Output:
java-1.21.0-openjdk-amd64 2111 /usr/lib/jvm/java-1.21.0-openjdk-amd64

Make note of the Java path you’re going to need it later: /usr/lib/jvm/java-1.21.0-openjdk-amd64/

Now, let’s create and open a tomcat.service file within the /etc/systemd/system directory using the nano editor.

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

Add the code snippet below to the tomcat.service file.

[Unit]
Description=Tomcat
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/java-1.21.0-openjdk-amd64/"

Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
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

RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Be sure the JAVA_HOME environment variable above points to your Java directory. This is the output directory you noted a couple of steps back.

Once the file has been saved, you can reload the systemctl daemon for it to recognize the new Tomcat service.

sudo systemctl daemon-reload

After reloading the systemctl, you can start the Tomcat service using the following command.

sudo systemctl start tomcat

After starting the service, we can verify it’s in operation by using the following command.

sudo systemctl status tomcat

Your terminal output should look like this:

#Expected Output
● tomcat.service - Tomcat
Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; preset: enabled)
Active: active (running) since Wed 2024–05–15 07:34:43 UTC; 6s ago
Process: 8157 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 8164 (java)
Tasks: 30 (limit: 1130)
Memory: 134.5M (peak: 134.7M)
CPU: 3.826s
CGroup: /system.slice/tomcat.service
└─8164 /usr/lib/jvm/java-1.21.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/lo>

To ensure Tomcat automatically starts with the system, execute the following command.

sudo systemctl enable tomcat

Tomcat is now good to go.

How To Install Nginx To Access The Tomcat Dashboard Via A Web Browser

Because Tomcat utilizes http://localhost:8080 to communicate with other devices, we need to install Nginx to act as a reverse proxy to serve the Tomcat dashboard over a web browser.

Nginx will receive requests from a web browser over port 80, and proxy those requests to Tomcat over port 8080. To do this, let's first set up our firewall and open a few ports.

sudo ufw enable
sudo ufw allow ssh
sudo ufw allow http

These commands enable the firewall and opens up port 22 for your SSH access and port 80 for your browser access.

You can verify the ports are open using the following command:

sudo ufw status

Now, let us install Nginx.

sudo apt install nginx

After the installation has completed, open the default nginx configuration file with the nano editor using the following command.

sudo nano /etc/nginx/sites-available/default

Navigate halfway down the Nginx configuration file and ensure your settings looks like what I have below and save the file.


server_name _;

location / {
proxy_pass http://localhost:8080; #This is Tomcat
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
}

Here we exclude the server_name using an underscore `_` because we don’t have a domain name.

We then specified a few settings within the `location {}` code block. Nginx will accept requests over port 80 and redirect them to `localhost:8080`, which is our Tomcat service.

After you save the file, let us test our Nginx config file to ensure our syntax is correct. You can test the config file using the following command.

sudo nginx -t

If everything is successful the Nginx service will accept the newly defined configuration file. All we have to do now is restart the service for the new configuration to take effect. We can do that with the following command.

sudo systemctl restart nginx

You should be able to open a web browser and navigate to your server’s IP address.

Note: If you run into any issues with the above tutorial, post them in the comments and I’ll see if I can find a fix for you.

Here is the default Tomcat welcome page:

Default Tomcat Welcome Page
Default Tomcat Welcome Page

You can also navigate to both the `Manager App` and `Host Manager` pages using the buttons to the right of the Tomcat logo. These same buttons are just below the Apache logo on the top right. You will need your manager and admin username and password that you created earlier.

You should see a page that looks similar to the following:

Tomcat Web Application Manager
Tomcat Web Application Manager

Learn Linux: Tomcat Web Application Manager Explained

The Tomcat Web Application Manager is a powerful tool that allows users to easily deploy, manage, and monitor web applications within the Apache Tomcat server. It provides a user-friendly interface for managing multiple web applications, allowing users to start, stop, and reload them with just a few clicks.

The manager also displays key information such as memory usage and request processing time for each application, making it easier for developers to troubleshoot any issues that may arise. Overall, the Tomcat Web Application Manager streamlines the process of working with web applications on the Apache Tomcat server and enhances its efficiency.

Tomcat Virtual Host Manager
Tomcat Virtual Host Manager

Learn Linux: The Tomcat Virtual Host Manager Explained

The Tomcat Virtual Host Manager is a tool that allows users to manage and configure virtual hosts on their Apache Tomcat server. A virtual host refers to the ability of a web server to serve multiple domains from a single IP address, making it an essential feature for hosting multiple websites on one server.

With the Virtual Host Manager, users can create new virtual hosts, edit existing ones, and assign different aliases or contexts to each host. This provides greater flexibility in managing website resources and improving overall performance. The easy-to-use interface makes it accessible for both experienced developers and beginners alike.

Conclusion: How To Install Apache Tomcat 10 on Ubuntu 24.04

In conclusion, installing Apache Tomcat 10 on Ubuntu 24.04 is a fairly straightforward process that requires basic knowledge of the Linux command line.

By following the steps outlined in this guide, you can successfully install and run a web server using Apache Tomcat 10 on your Ubuntu machine. With its powerful features and continuous updates, Tomcat remains one of the most popular choices for hosting Java-based web applications.

--

--

Jason Bodie

Internet Enthusiast. I search the planet for noteworthy information. Follow to learn more.