Configuring Tomcat Virtual Hosts With Apache

Configuring Virtual Hosts with Apache is pretty standard stuff, but adding virtual Tomcat Hosts to your Apache Web Server setup requires a few additional steps.

The default configuration for Tomcat is to use port 8080, with the service default page at http://localhost:8080 looking like this.

We want to serve up .JSP pages, Servlets and other Java goodies from port 80 with an everyday http://mysite url. The local site name we’re using is Javaweb, or http://javaweb. The site points to a Java Project in IntelliJ IDEA. We’ll cover the particulars of using virtual hosts for Java Project IntelliJ development in our next espisode. Today we’re going to focus on configuring Apache to serve up Java sites on virtual hosts like any other site on our Linux server.

The secret to Apache and Tomcat co-existence on Port 80 is the Apache Proxy and Proxy_httpd modules. Here we’ll install them together in a single command.

Next we create our Javaweb site definition file in /etc/apache2/sites-available. Notice the ProxyPass statement at about the middle of the file. That’s the trick. We’re going to load our Apache web site by way of Tomcat.

As I said, our Javaweb site is the root of our Java project in IntelliJ and not physically located in the Tomcat directory area.  ProxyPass requires us to travel through Tomcat to reach our destination, which is where the beauty of Linux linking comes into play. We’re going to create a Symbolic Link in our Tomcat webapps folder to appear as a subdirectory to Apache. It should be noted that /webapps is the default base directory of Tomcat, which is why we create our directory under /webapps rather than the Tomcat root directory.

Now we’re at the point where we can add the Javaweb site in Apache (with $ sudo a2ensite Javaweb) and restart Apache.

Here is http://javaweb/index.jsp served from Apache by way of Tomcat displaying our lovely .JSP!