Home > Java > Developing with Grails and Apache2

Developing with Grails and Apache2

by paul on October 1, 2007

In production I use Apache2 in front of Tomcat. The configuration for this is quite simple and standard. However, while developing Grails applications I wanted to run the same configuration, so that there are very few differences between my development and production environments. In Apache, I use named virtual hosts to forward to Tomcat, and Tomcat is configured with web applications corresponding to the virtual host name, with just ‘/’ as the context path.

Grails uses Jetty, and when running ‘grails run-app’ a Jetty container is configured and started. This is fine for me, I don’t think I really need to switch to Tomcat in development, but running Apache2 in front of it would be very nice. This way I don’t need to worry about differences in URLs.

I’m not familiar with Jetty at all, so the simplest and fastest way to get this working was to make a copy of the RunApp.groovy script that comes with Grails and change it so that it looks for the context path definition in ‘application.properties’ in the root of the Grails project.

Update March 7 2008:

See the comments below – I have produced a better implementation:

– appContext is now defined in Init.groovy so it is available to all scripts
- RunApp.groovy and RunWar.groovy use the appContext to configure the server and display the url.
- RunAppHttps.groovy reuses RunApp.groovy so the only change here is for displaying the url.

This is now raised in JIRA as an enhancement.

So, in $GRAILS_HOME/scripts/RunApp.groovy add the following after the ‘includeTargets’ directive:

def props = Ant.antProject.properties
def appContext = props.'app.context' ? props.'app.context' : grailsAppName
if(!appContext.startsWith('/')) {
  appContext = "/${appContext}"
}

Now change the webContext definition:

webContext = new WebAppContext(“${basedir}/web-app”, “${appContext}”)

For completeness, update the StatusFinal event:

event(“StatusFinal”, ["Server running. Browse to http://localhost:$serverPort$appContext"])

Each Grails project has an application.properties file in the root of the project. If you want to change the context path from the default, define it in this properties file like so:

app.context=/mycontextpath

Now your application can be viewed at http://localhost:8080/<app.context>, or, with Apache in front of it, http://virtualhostname/<app.context>.

In my case I want to set the context path to merely ‘/’ so in application.properties I define:

app.context=/

And with Apache2 configured properly, I view my application at http://virtualhostname/.

Download the modified Grails 0.6 RunApp.groovy here.

Download the modified Grails 1.0-RC1 RunApp.groovy here.

Download the modified Grails 1.0-RC2 RunApp.groovy here.

Download the modified Grails 1.0-RC3 RunApp.groovy here.

Download the modified Grails 1.0 RunApp.groovy here.

Download the modified Grails 1.0.1 scripts here: grails-1.0.1-context-path-mod.tar.gz

{ 14 comments… read them below or add one }

PB October 11, 2007 at 9:08 am

I’m just recoding a website in grails and came across the problem of developing with Jetty (with the application context) but deploying onto Tomcat as a ROOT context.

This was proving imposible without this neat trick, so thankyou very much.

Please make sure it gets committed into the Grails codebase.

Paul.

paul October 12, 2007 at 12:06 am

Thanks for the feedback Paul.
I posted to the developers mailing list but haven’t had any feedback yet. Hopefully they will incorporate it at some point in time.

See http://www.nabble.com/Defining-the-context-path-in-application.properties-tf4547416.html

Luis Sanchez October 16, 2007 at 9:22 am

Very good tip.
You mention a setup for Tomcat and Apache to access a web application using virtualhost and / context. Could you share with us these setup for Tomcat/Apache2 ?

paul October 20, 2007 at 11:44 am

Luis, I’ve just written a post about Apache2 and Tomcat 5.5 – I hope this helps. See http://www.javathinking.com/?p=50

Paul February 6, 2008 at 4:20 am

Any chance of a release for Grails 1.0?

This REALLY needs adding to the codebase now – it’s such a useful hack. Point Graham in the direction of this page.

Paul.

interz February 19, 2008 at 12:13 pm

Hi,
Have you submitted as a JIRA issue to be incorporated into the grails distribution? Our app also requires this and it would be nice.

paul February 20, 2008 at 4:50 pm

I’ll raise a JIRA issue soon – I am hoping to add the suggestions in this post http://www.nabble.com/Defining-the-context-path-in-application.properties-td12976557.html#a15331841 before I do that though.

I’ll try to get to it on Friday.

interz February 22, 2008 at 6:51 am

great, the sooner the better, I think a 1.0.2 release is coming out soon, I see they already have several JIRA issues queued for the release, maybe you can get this one in there. Seems pretty simple.

thanks

paul February 27, 2008 at 1:14 am

Sorry for the delay. I’ve created a JIRA issue:
http://jira.codehaus.org/browse/GRAILS-2534

Attached to the issue is a modified RunApp.groovy in which the context path can be specified in the following locations (in order of precedence):
1. System property
2. application.properties
3. Config.groovy

This means you can now affect the context path in 1 of 3 ways:

1. grails -Dapp.context=mycontext run-app
2. in application.properties, define property:
app.context=mycontext
3. in Config.groovy define the app.context property:
environments {
development { app.context=’mycontext’ }
}

I hope the community finds this useful!

interz March 5, 2008 at 4:08 am

Thanks for the JIRA issue. Voted on it. Looks like it will be integrated into the 1.0.2 release! THanks!

interz March 7, 2008 at 6:41 am

Does this work with run-war as well? When I startup in run-war with the app.context=/ it still starts with my /projectName

paul March 7, 2008 at 11:26 pm

Interz, thanks for pointing this out. So far, I had only concerned myself with the RunApp.groovy script, so no it doesn’t work for run-war.

I have produced a better implementation though:
- appContext is now defined in Init.groovy so it is available to all scripts
- RunApp.groovy and RunWar.groovy use the appContext to configure the server and display the url.
- RunAppHttps.groovy reuses RunApp.groovy so the only change here is for displaying the url.

You can download these modified grails-1.0.1 scripts here:
grails-1.0.1-context-path-mod.tar.gz

interz March 21, 2008 at 10:42 am

Looks like 1.0.2 is out, I think your patch won’t be out till 1.0.3.

matt December 20, 2008 at 11:47 pm

I just came across this as I needed it for some Flex/Grails proof-of-concept work. Many thanks for putting this together and having it included into Grails!

Leave a Comment

Previous post:

Next post: