Setup Apache2 Proxy Server for Tomcat 9 on Ubuntu 16.04 / 17.10 / 18.04

Yesterday we showed students and new users how to install Apache Tomcat 9 web server on Ubuntu 16.04 / 17.10 and 18.04… Tomcat by default communicates over port 8080… which is not a standard port… You must always use the server name and port 8080 to be able to access Tomcat’s content… In some cases, this might not be what users want…

This post will show you how to setup Apache2 HTTP server as a reverse proxy for Tomcat 9 webserver. A reverse proxy is a type of service where the proxy server (frontend server) takes HTTP(S) requests and forwards them to a backend server.

In this setup, Apache2 will sit in front of Tomcat 9 and accepts all requests over port 80 (the default). Then those requests are transparently sent to Tomcat 9 webserver to fetch and retrieve the content….

To get started with setting up Apache2 as a reverse proxy, follow the steps below

Step 1: Install Tomcat 9 Server

To use Apache Tomcat 9 as a backend server, you must install and configure it… We’ve covered that on this website… To learn how to install Tomcat webserer, read the post below:

Setup Apache Tomcat9 on Ubuntu 16.04 / 17.10 / 18.04

Step 2: Install and Configure Apache2

Now that Tomcat is installed and configured, go and install Apache2 HTTP server. To do that run the commands below

sudo apt-get install apache2

After installing Apache2, the commands below can be used to stop, start and enable Apache2 service to automatically start up everytime the server starts.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

Step 3: Enable Apache2 Proxy Modules

Now that Apache2 is installed, run the commands below to enable its proxy modules.

sudo a2enmod proxy
sudo a2enmod proxy_http

When you’re done, restart Apache2 to enable reload the modules.

sudo systemctl restart apache2.service

After that, run the commands below to create a new site configuration file called example.conf

sudo nano /etc/apache2/sites-available/example.conf

Then copy and paste the lines below into the file and save.

    <VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        ServerAdmin webmaster@example.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        ProxyRequests Off
        <Proxy *>
          Order deny,allow
          Allow from all
        </Proxy>
        
        ProxyPass / http://127.0.0.1:8080/
        ProxyPassReverse / http://127.0.0.1:8080/

        <Location />
          Order allow,deny
          Allow from all
        </Location>

   </VirtualHost>

After that, save the file.

Then enable and restart Apache2 web server…

sudo a2ensite example.conf
sudo systemctl restart apache2.service

If Tomcat fails to start, you may want to disable the default site configuration for Apache2 site configuration file or change the port in the file to 8080.

sudo a2dissite 000-default.conf

Now, all you have to do is type the domain name and you should be sent to Tomcat web server without typing the port number (8080)…

Tomcat proxy server ubuntu

Enjoy!

You may also like the post below:

Setup Matomo (PIWIK) on Ubuntu 16.04 / 17.10 / 18.04 with Nginx, MariaDB and PHP 7.2 Support