Getting Groovy

Lately I’ve tried out the Grails framework a bit for a little pet project. It started with a little peek on Groovy, and then after a little messing around with the “getting started” part, I wanted to check out grails as well. I must say it was very pleasant to work with!

Though it was a very little project I did with Grails, I think ihas potential. I really like the way everything is done by convention, and the plugins just makes it incredible simple to have a lot of functionality up and running in no time! Security, searching and JMS just to mention a few. Check out the plugin portal if you are curious. One more thing I find cool is that you can run Grails applications on Google App Engine. This is done very easily by using a plugin!

I also noticed a project named Griffon which is a Grails like framework for developing desktop applications in Groovy. I haven’t tried it out, but it seems interesting. As I read about Griffon I also got aware of the UI toolkits SWT and Pivot. Frankly I did only know of AWT and Swing, but the next time I decide to write a GUI app in Java or Groovy, I’ll definitely consider trying out one of these :-)

Finally, some groovy and grails resources.

http://grailstutorials.com – The design is crap, but there are a lot of tutorials)
http://grailspodcast.com/blog/list – Interesting podcasts with the latest grails/groovy news
http://goo.gl/uTEM – An article on groovy meta programming (there’s a lot of groovy stuff on the same site)

Tomcat + Apache (+ Grails)

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/. First find the AJP connector line, and uncomment it if it’s currently active. Then scroll to the bottom, (or wherever your </Engine> tag is. Inside it add a new name basd host like this:

 <!-- Uncomment this line -->
 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

 <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