Apache Tomcat

How to Setup Apache Tomcat Reverse Proxy

There are several options when setting up a proxy for the Apache Tomcat server. This tutorial shows you how to set up a reverse proxy for Apache Tomcat using the Apache HTTP web server.

Step 1: Install Apache Tomcat

The first step is to install the Apache Tomcat server. In this guide, we will use Apache Tomcat 10.

Update your system

Start by updating your system:

sudo apt update
sudo apt upgrade

Install Java JDK

Before installing Apache Tomcat, we need to ensure we have a working Java version installed.

Use the command below to install openjdk 11.

sudo apt install openjdk-11-jdk

Once the installation completes, open the terminal and check the JDK version as:

$ java --version
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

If you get the “command not found” error, ensure the installation was successful.

Install Apache Tomcat

The next step is to download and install Apache Tomcat. Open your terminal and enter the command:

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.12/bin/apache-tomcat-10.0.12.tar.gz

The above command will download the tomcat 10 package.

Next, unzip the package as:

tar xvf apache-tomcat-10.0.12.tar.gz

Start the Apache Tomcat service

Navigate into the extracted directory/bin:

cd apache-tomcat-10.0.12/bin/

Start the tomcat service:

sudo ./startup.sh

Step 2: Install Apache HTTPD

The following step is to install the Apache HTTPD server. Use the apt command as:

sudo apt-get install apache2 -y

Start the HTTPD service

Upon completion, start the httpd service:

sudo service apache2 start

Step 3: Enable mod_proxy module

Once we have all the servers up and running, we can now configure the reverse proxy for the tomcat server.

Start by enabling the mod_proxy module for the Apache HTTPD:

sudo a2enmod proxy

Step 4: Configure Apache Virtual Hosts

The next step is to configure virtual host routing for the httpd server. Doing this allows us to forward requests to their desired destinations.

sudo vim /etc/apache2/sites-enabled/000-default.conf

Enter the virtual host entry as:

<VirtualHost *:80>
        ProxyRequests Off
        ProxyPass /webapps http://localhost:8080/
        ProxyPassReverse /webapps http://localhost:8080/

        <Location "/webapps">
          Order allow,deny
          Allow from all
        </location>
</VirtualHost>

Save and close the file.

Finally, restart the apache server as:

sudo service apache2 restart

You can test the proxy by navigating to http://localhost/webapps

Conclusion

This guide showed you how to set up a reverse proxy for the Apache Tomcat using the Apache HTTPD server.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list