Setting up Tomcat on Ubuntu
If you want to run a grails app (or any Java app that requires a servlet container) Tomcat is an excellent choice. You’ll need to do this if you’re hosting your app on a VPS like Linode (highly recommended) or if you’re using AWS and want a little more control than Elastic Beanstalk allows you. I preformed the following steps on an Ubuntu 10.04 server.
Before we install tomcat we should have apache and java installed on the server.
We should also create a new user for tomcat to run as
In order to easily accomodate multiple instances of tomcat on this machine I don’t use the package manager to install tomcat but instead download it myself. You can find the closest mirror for Tomcat 7 on their site. Here’s the process I use to get my base tomcat install ready:
This way if I want to upgrade my tomcat I simply download the new version into /opt and change the link.
Now let’s setup a separate tomcat base folder. This will contain just the folders and settings for the specific instance of tomcat. I keep my tomcat instances on a separate partition called /web so you might have to change the paths here. Here’s the steps I follow to create a new tomcat instance called tomcat-craig.
Now if you have multiple instances of tomcat you need to make sure the shutdown and startup ports are unique in conf/server.xml. Here’s what my server.xml file looks like:
Alright now we should create a startup script (/etc/init.d/tomcat-craig) that’s going to use our tomcat base we installed in /opt/tomcat and deploy the wars found in /web/tomcat-craig. I’ve tweaked this excellent startup script to allow for multiple instances. So here’s what my startup script file looks like:
The really cool part about this is that it attempts to shutdown tomcat gracefully, waits for a specified period of time and then just kills the process if it’s still hanging there. Also since we set the JVM heap size in the CATALINA_OPTS we can tweak the values in the script and simply restart if we ever need to bump that up. Now that we’ve got the script in place we should use it to startup tomcat
Now to have tomcat startup automatically after a server restart do the following:
In this example we setup tomcat to run on AJP port 8009 (see the server.xml file above). The last step is to have apache proxy all request to our tomcat instance, so that a request to www.yourdomain.com gets directed to tomcat.
First we install mod-jk
Next we create the file /etc/apache2/workers.properties that looks something like this:
Then the last thing we need to do is configure a site in apache that uses this worker process. Here’s a simplified version of my site found in /etc/apache2/sites-available/default
Now you’re ready to drop your war (grails or otherwise) into your tomcat webapps folder and deploy your app.