Setting up a Tomcat in your AMI

I love Java and Object Oriented Paradigm, and I was looking for an option to develop Java apps in the cloud in a cheap way. The cheaper, light and non-restrictive way to do this is installing Tomcat in an AmazonWS Linux-AMI. You can do great applications in Tomcat with Spring/JPA/JSF2, but this architecture deserves another post.

Well, I want to show you how to configure Tomcat to deploy web applications in a basic-AMI (It’s free the first year):

  • Configuring memory heap
  • Provide gzip compression for the web contents
  • Two ways to redirect to port 80
  • And, if you use maven, deploying remotely with cargo

Installation and memory heap

Install with yum tomcat 6.0.32:

sudo yum install tomcat*

Preparing console access:

sudo vi /etc/tomcat6/tomcat-users.xml
		<user name="tomcat" password="youpasshere" roles="manager"></user>

Increasing the memory configuration:

sudo vi /etc/tomcat6/tomcat6.conf
JAVA_OPTS=" -Xmx512M -XX:MaxPermSize=128M"

GZIP compression

This is really easy, but useful:

sudo vi /etc/tomcat6/server.xml

Add at the Connector tag the attribute compression=”force”

Redirecting to port 80

The easy way with iptables, rerouting the port:

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
sudo iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to-ports 8080

The difficult way, installing Mod_JK:

sudo rpm -ivh mod_jk-ap20-1.2.28-2.el5.centos.i386.rpm
vi /etc/httpd/

You must write:

		worker.ajp13.type=ajp13 #Mod_jk

And in apache config write:

vi /etc/httpd/conf/httpd.conf
    LoadModule jk_module /etc/httpd/modules/
    # Where to find
    JkWorkersFile /etc/httpd/
    # Where to put jk logs
    JkLogFile /var/log/httpd/mod_jk.log
    # Set the jk log level [debug/error/info]
    JkLogLevel info
    # Select the log format
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
    # JkOptions indicate to send SSL KEY SIZE,
    JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
    # JkRequestLogFormat set the request format
    JkRequestLogFormat "%w %V %T"
    #Adding jsf preffix to proccess by tomcat
    JkMount /*.jsf ajp13
sudo /etc/init.d/httpd restart

Deploying remotely with Maven/Cargo

Inspired in this article from the great John Ferguson Smart. You can add the plugin in your pom.xml:

					<!-- Container configuration -->

And deploy remotely with the following command:

mvn package cargo:redeploy

Hope enjoy!!!

Leave a Reply

Your email address will not be published. Required fields are marked *