https://sites.google.com/site/amistrongeryet/setting-up-java-tomcat-on-ec2

 

Setting up Java+Tomcat on EC2

 

Setting up EC2 for Tomcat 

Prerequisites

(vague, since I did this a while ago and didn't keep careful track)

  • Sign up for an AWS account
  • Install Amazon's AWS tools
  • Set up authentication methods (key pair, etc.)
  • Configure security group

EC2 Setup

1. Launch an EC2 instance using Amazon's ami-84db39ed (fedora-8-i386-v1.14-std) AMI.  This appears to be a basic Fedora 8 image.

2. ssh into the instance, using a command like:
ssh -i <path to your .pem file> root@<public DNS address for your instance>

All subsequent command lines are executed in the instance, except where noted.

Install Java

Download a JDK RPM from http://download.java.net/jdk6/.  Look for "Linux RPM in self-extracting JDK file" under "Linux Platform".  I downloaded jdk-6u20-ea-bin-b02-linux-i586-01_apr_2010-rpm.bin, by copying the link address and then running curl:

This returned a redirect, which I then downloaded via curl:

mkdir /usr/local/java

I then followed the instructions on http://alwajdi.blogspot.com/2007/12/how-to-install-snns-jdk-in-fedora-8.html to install the downloaded JDK.  Concretely:

chmod 755 jdk-rpm.bin
./jdk-rpm.bin 
updatedb; locate javac | grep bin  # this step merely serves to verify the installation
/usr/sbin/alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_20/bin/java 100
/usr/sbin/alternatives --install /usr/bin/jar jar /usr/java/jdk1.6.0_20/bin/jar 100
/usr/sbin/alternatives --install /usr/bin/javac javac /usr/java/jdk1.6.0_20/bin/javac 100
/usr/sbin/alternatives --config java
updatedb

Note that some of these commands are slightly different than in the linked web page, because I downloaded a newer JDK release than the one mentioned in those instructions.

Install Tomcat

I installed it under /env/tomcat, for no reason other than the fact that the Amazon EMI used by Amazon's Eclipse plugin does so.

mkdir /env
mkdir /env/tomcat
cd /env/tomcat
tar zxvf apache-tomcat-6.0.26.tar.gz
cd apache-tomcat-6.0.26
bin/startup.sh  # this launches Tomcat

To verify the installation, load the root page from a web browser: http://INSTANCE_PUBLIC_DNS_NAME:8080

(Substitute the public DNS address of your instance, as shown on the EC2 dashboard.)


Configure Tomcat to launch automatically

Create a file "/etc/rc.d/init.d/tomcat" with the following content:

#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.

JAVA_HOME=/usr/java/jdk1.6.0_20
CATALINA_HOME=/env/tomcat/apache-tomcat-6.0.26
export JAVA_HOME CATALINA_HOME

exec $CATALINA_HOME/bin/catalina.sh $*

You may need to tweak JAVA_HOME and/or CATALINA_HOME, depending on exactly which versions of the Java SDK and Tomcat you installed.  Next, execute these commands to set the proper permissions for your init script and enable Tomcat for auto-launch:

chmod 755 /etc/rc.d/init.d/tomcat
chkconfig --level 2345 tomcat on

Tomcat should now be automatically launched whenever your server restarts.

Install your application

Now, we'll install our web application and tweak the Tomcat configuration a bit.  First, edit the server.xml file using "vi conf/server.xml".  Look for the connector for port 8080:

 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

and change the port from 8080 to 80, so that your web server is accessible on the normal HTTP port.  Next, find the Host tag (near the end of the file):

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

Change autoDeploy to "false".  (This is recommended by O'Reilly's Tomcat: The Definitive Guide, 2nd Edition.  I didn't quite follow their reasoning, it had something to do with applications being installed twice at server startup, once by autoDeploy and once by a different mechanism.)  Finally, inside the <Host> tag, add a <Context> tag for your application.  Mine looks like this:

   <Context docBase="CloudMetricsEC2.war" path="" />

Now you can close server.xml.  Next you need to build a WAR file for your application -- in Eclipse, you can do this by right-clicking on the project in Package Explorer, choosing "Expore...", and choose "Web -> WAR file" from the list of export formats.  Copy it to the instance using a command like this (on your local machine, not the EC2 instance):

scp -i ~/.ec2/Tomcat2.pem CloudMetricsEC2.war root@INSTANCE_PUBLIC_DNS_NAME:/env/tomcat/apache-tomcat-6.0.26/webapps/CloudMetricsEC2.war

Next, remove the default ROOT application, or at least move it out of the way:

mv webapps/ROOT webapps/xROOT

Otherwise, Tomcat won't serve your application at the root path, even if you've configured it that way.

Finally, restart Tomcat to pick up all the changes:

bin/shutdown.sh; bin/startup.sh 

If you need a larger Java heap

I had to tweak the JVM arguments used by Tomcat to enable a larger Java heap size.  Here's the clumsy way I found to do it (there's probably a better way).  Edit the Tomcat startup script using "vi bin/catalina.sh", find the section below and add the bolded line:

if [ -z "$LOGGING_MANAGER" ]; then
  JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
else
  JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER"
fi

JAVA_OPTS="$JAVA_OPTS "-Xms500M" "-Xmx500M

# ----- Execute The Requested Command -----------------------------------------

You'll have to restart Tomcat with bin/shutdown.sh; bin/startup.sh for this change to take effect.
  • No labels