Configuring Domain, Apache + Tomcat + mod_jk + mod_rewrite on Ubuntu 14.04

This tutorial will walk you through the steps required to point your domain web address at the Greenfield application running in Tomcat.

This tutorial assumes that you have installed and configured Apache & Tomcat and have Greenfield running in Tomcat. If you haven't done this already, follow Minimum Server Setup : Apache, Tomcat & Greenfield on Ubuntu 14.04 for detailed instructions on how to install and configure Apache & Tomcat for Greenfield on an Ubuntu 14.04 Web Server

This tutorial also assumes that you have pointed your @ record at your VPS server's IP address in your ISP Domain management system.

1. Install & configure Apache mod_jk module

The Apache mod_jk module will allow us to mount our Greenfield application enabling you to browse to the Greenfield application without having to specify Tomcat's 8080 port.

Example

http://{{server ip address}}/greenfield

This is an important step allowing you to later rewrite requests to the Greenfield application running in Tomcat

Install mod_jk module

sudo apt-get install libapache2-mod-jk

Edit workers.properties file

sudo vim /etc/libapache2-mod-jk/workers.properties

Confirm java_home is set to the following

workers.java_home=/usr/lib/jvm/default-java

Confirm tomcat_home is properly set to your tomcat install location.

workers.tomcat_home=/usr/local/tomcat/tomcat-current

Verify Tomcat's server.xml has AJP/1.3 definition

Open File

sudo vim /usr/local/tomcat/tomcat-current/conf/server.xml

Verify following line is uncommented

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

2. Enable Apache mod_rewrite module

cd into Apache's mods-available directory

cd /etc/apache2/mods-available/

Run command to enable mod_rewrite module

sudo a2enmod rewrite

Restart Apache

sudo service apache2 restart

3. Create Virtual Host files for your website domain

In this section we will be creating 2 Virtual Host files for your domain. One for http://www.domain.com, the other for http://domain.com

cd into sites-available directory

cd /etc/apache2/sites-available

Create www_domain.conf Virtual Host file

This file will tell Apache to take requests for your domain and pass them to the Greenfield application running in Tomcat

Substite 'domain' for a descriptor matching your domain

sudo vim www_domain.conf

Past the following into the file

Substitute your domain web address for www.domain.com

<VirtualHost *:80>
	ServerName www.domain.com
	ServerAdmin admin@domain.com
	
	JkMount /greenfield* ajp13_worker
	
	<Directory />
	        Options FollowSymLinks
	        AllowOverride None
	        RewriteEngine On
	        RewriteRule \/$ /greenfield [L]
	</Directory>
</VirtualHost>

Save file

Create domain.conf Virtual Host file

This file will rewrite requests from domain.com to ww.domain.com

sudo vim domain.conf

Past the following into the file

<VirtualHost *:80>
	ServerName domain.com
	ServerAdmin admin@domain.com
	
	RewriteEngine On
	RewriteRule \/$ http://www.domain.com [L]
</VirtualHost>

Save file

Enable both site configurations

sudo a2ensite www_domain.conf
sudo a2ensite domain.conf

Create greenfield directory

sudo mkdir /var/www/html/greenfield

Restart Apache

sudo service apache2 restart

Verify domain successfully pointed at Greenfield application running in tomcat by browsing to

http://www.domain.com

If everything is setup correctly, Apache will take the request and forward to the Greenfield application running in Tomcat. The final Url will read something like

http://www.domain.com/greenfield

Again, substituting your web domain address for domain.com