I’ve spent quite some time on deploying my first grails application to a Tomcat server, and get the setup just the way I want it. I thought that writing a small tutorial on how to do it was a good idea, as I then have it for later use, and perhaps someone else can have good use of it. The grails part of this is not very important, as it will be just the same for Java applications. I don’t claim this to be the ultimate(or even correct) setup, but at least it is working the way I want it to. Feel free to comment!
Deploying a grails application to Tomcat is actually quite easy. Just create a war file, and place it in the webapps folder.
To create a war with production environment settings issue the command
grails prod war
When placing this in the webapps folder, Tomcat will automatically unpack the war to a folder with the same name. You can access it at http://server.address:8080/appname (8080 is the default listen port). For some cases this might be good enough. If you on the other hand want to access the webapp without having to specify both the port number and the application name, you want some kind of virtual hosting.
In my case I wanted to access the application from a domain name. I don’t have much experience with either tomcat or apache configuration, but I knew how to set up a name based virtual host on apache. Name based means that Apache interprets the incoming request and decides what to serve it based on the domain name. In addition to this we can also set up a name based virtual host on Tomcat. Having this we just need some kind of connection between Tomcat and Apache. This way Apache becomes the entry point, while Tomcat is the application server. Let’s start!
In this example I’m using a Ubuntu server, Tomcat 6.0 and Apache 2.2
Set up a virtual host in Apache and load the necessary modules
Create a new file in /etc/apache2/sites-available/ and name it after the domain name. Edit it, and write something like this:
<VirtualHost *:80>
ServerName domain.com
ProxyPass / ajp://localhost:8009/
</VirtualHost>
This will redirect all incoming requests for domain.com to Tomcat. AJP is a protocol used to connect Tomcat and Apache, and 8009 is the default port. Read more here or google it. AJP depends on the mod_proxy, but (at least on my system) when you enable mod_proxy_ajp mod_proxy will be enabled automatically.
sudo a2enmod proxy_ajp
When we’re at it, you also have to enable your new site (the one we defined in sites-available)
sudo a2ensite name-of-site (the filename you chose some steps ago)
Note that it now also is listed under sites-enabled. Now we’ll continue with configuring Tomcat.
Tomcat configuration
There isn’t too much to be done with Tomcat. Open server.xml in <tomcat-dir>/conf/. Scroll to the bottom, (or wherever your </Engine> tag is. Inside it add a new name basd host like this:
<Engine>
<!-- *snip* -->
<Host name="domain.com" appBase="webapps/appName">
<Context path="" docBase="." />
</Host>
</Engine>
Now we’re ready, and hopefully everything works! Remember to restart both apache and tomcat!
Resources
http://oreilly.com/java/archive/tomcat-tips.html