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:

wget http://dev.centos.org/centos/5/testing/i386/RPMS/mod_jk-ap20-1.2.28-2.el5.centos.i386.rpm
sudo rpm -ivh mod_jk-ap20-1.2.28-2.el5.centos.i386.rpm
vi /etc/httpd/workers.properties

You must write:

		workers.tomcat_home=/usr/share/tomcat6
		ps=/
		worker.list=ajp13
		worker.ajp13.port=8009
 		worker.ajp13.host=localhost
		worker.ajp13.type=ajp13 #Mod_jk

And in apache config write:

vi /etc/httpd/conf/httpd.conf
    #Mod_jk
    LoadModule jk_module /etc/httpd/modules/mod_jk.so
 
    # Where to find workers.properties
    JkWorkersFile /etc/httpd/workers.properties
 
    # 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:

<plugin>
				<groupid>org.codehaus.cargo</groupid>
				<artifactid>cargo-maven2-plugin</artifactid>
				<version>1.0</version>
				<configuration>
					<!-- Container configuration -->
					<container>
						<containerid>tomcat6x</containerid>
						<type>remote</type>
					</container>
					<configuration>
						<type>runtime</type>
<properties>
							<cargo.remote.username>tomcat</cargo.remote.username>
							<cargo.remote.password></cargo.remote.password>
							<cargo.tomcat.manager.url>http://labs.lebrijo.com:8080/manager</cargo.tomcat.manager.url>
						</properties>
					</configuration>
				</configuration>
			</plugin>

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 *