So you’re running an Apache Tomcat environment on Linux but since this is a public facing web application, you want to get rid of the port 80 business so that your users don’t have to go remembering what port the application is running on. After all, port 8080 or 8084 isn’t exactly easy to remember for the average non-technical user. And god forbid you’ve decided to change this to something non-default, trying to remember that is going to be even more fun!
Thankfully it’s fairly straight forward to use Apache as a Reverse Proxy that sits in front of Apache Tomcat so that you can serve requests over port 80. You could configure Apache Tomcat to run on port 80, but if you’re running the Apache Tomcat Manager GUI then this restricts you to running your web applications only in sub-directories – and let’s be honest – no-one wants that. Everyone wants to be running their web application at the root of their domain, i.e. www.example.com rather than www.example.com/my-web-application/.
Configure an Apache Virtual Host File
Simply SSH into your server and run the command below;
sudo nano /etc/httpd/conf.d/httpd.conf
It’s worth nothing to double check the other files that currently exist in that directory before configuring this. Sometimes you will have slightly differently named files and/or this file may already exist with current configurations. Don’t just go adding configurations to this fil unless you’re sure what you are doing. At least take a backup of the current file(s) so you can revert your changes if you mess things up.
Thankfully on many vanilla installations of Apache this file will not exist, so the nano command above will create the file so you can edit it straight away.
To keep things simple all you need to do is add the following lines of code to that file;
<VirtualHost www.example.com:80> ProxyPreserveHost On ProxyPass / http://127.0.0.1:8080/ ProxyPassReverse / http://127.0.0.1:8080/ </VirtualHost>
Now run the command to restart Apache to ensure the changes take effect.
sudo service httpd restart
Now you’ll notice that the above doesn’t actually change anything. It just means the Apache Tomcat Manager GUI now loads over port 80 rather than port 8080. That isn’t quite what we want, so we need to configure the Virtual Hosts file to suit your application needs.
Let’s say your application is called MyWebApplication, which sits at www.example.com:8080/MyWebApplicaion on Apache Tomcat. You need to ensure your Virtual Hosts file looks as follows so that the application routes successfully;
<VirtualHost www.example.com:80> ProxyPreserveHost On ProxyPass / http://127.0.0.1:8080/MyWebApplication ProxyPassReverse / http://127.0.0.1:8080/ MyWebApplication </VirtualHost>
Simple. Now you’re done.
This is how you configure Apache as a Reverse Proxy for Apache Tomcat. This approach also allows you to run multiple Java web applications from a single Apache Tomcat instance if that suits your needs from a practical, management and security perspective. Alternatively, run everything from separate setups if you need something more segmented.