Author |
Topic: Install Tomcat on AWS EC2 instance |
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Install Tomcat on AWS EC2 instance |
Download and Install Tomcat
Go to download page: http://tomcat.apache.org/download-60.cgi. Look for "tar.gz" under "Binary Distributions/Core" and copy the link address.
# mkdir /env
# mkdir /env/tomcat
# cd /env/tomcat
# curl http://mirror.atlanticmetro.net/apache/tomcat/tomcat-6/v6.0.33/bin/
apache-tomcat-6.0.33.tar.gz > apache-tomcat-6.0.33.tar.gz
# tar zxvf apache-tomcat-6.0.33.tar.gz
# cd apache-tomcat-6.0.33
# bin/startup.sh
To verify the installation, load the root page from a web browser: http://INSTANCE_PUBLIC_DNS_NAME:8080/
Note: Substitute the public DNS address of your instance, as shown on the EC2 dashboard. Port 8080 must be set for the given security group. JAVA_HOME must be correctly set.
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Configure Tomcat to survive instance reboot |
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_29
CATALINA_HOME=/env/tomcat/apache-tomcat-6.0.33
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.
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Deploy your own web application into Tomcat |
Now, we'll install our web application and tweak the Tomcat configuration a bit. First, edit the conf/server.xml file. 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". Finally, inside the <Host> tag, add a <Context> tag for your application like this:
<Context docBase="myApps.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 "Export...", 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 myApps.war
root@INSTANCE_PUBLIC_DNS_NAME:/env
/tomcat/apache-tomcat-6.0.33/webapps/myApps.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
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Increase Java heap for Tomcat |
Edit the Tomcat startup script "bin/catalina.sh", find the section below:
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
and then add:
JAVA_OPTS="$JAVA_OPTS -Xms128M -Xmx512M"
You'll have to restart Tomcat with bin/shutdown.sh; bin/startup.sh for this change to take effect.
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
The working case for my web application deploy |
Step 1) Keep Tomcat configuration untouched:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
Step 2) Copy myConext.war onto <TOMCAT>/webapps:
+ .. webapps
+ .. jsp-examples
+ .. ROOT
+ .. webdav
.... myContext.war
Step 3) Startup Tomcat
+ .. webapps
+ .. jsp-examples
+ .. ROOT
+ .. myContext
+ .. css
+ .. img
+ .. js
+ .. WEB-INF
+ .. classes
+ .. lib
.... web.xml
.... index.html
+ .. webdav
.... myContext.war
Step 4) That's it.
No configuration about <context/> is needed since it's under webapps already. The web application can be accessed by:
http://localhost:8080/myContext/index.html
Note: The war file name turns to be the context name of your web application. While building war file, make sure the war file name is the context name rather than the project name:
+ .. myEclipseWorkplace
+ .. myProject1 <--right click on this to export web/war file
+ .. .settings
+ .. src
+ .. myContext <--choose this name as war file name
+ .. css
+ .. img
+ .. js
+ .. WEB-INF
+ .. classes
+ .. lib
.... web.xml
.... index.html
+ .. myProject2
+ Servers
|
|
|
|
|
|
|
AwsEC2 member offline |
|
posts: |
39 |
joined: |
08/28/2012 |
from: |
CA |
|
|
|
|
|
Side notes for .war deployment. |
1) Upload .war file to folder you have the write (-w) permission as ec2-user.
2) As root user, move (sudo mv) the .war file to webapps folder (owner=root; drwxr-xr-x).
Note: the owner of the file remains unchanged after move.
3) AWS has no outbound security rules. That means your application running within EC2 can use any outgoing port, like 587 for SMTP TLS mail service. If the mail failed, it was not because the port 587 got blocked. Most likely, as it did happen for Gmail, the SMTP server rejected the request.
|
|
|
|
|
|
|
AwsEC2 member offline |
|
posts: |
39 |
joined: |
08/28/2012 |
from: |
CA |
|
|
|
|
|
Upgrade Tomcat to Version 7u68 |
Download and unpack Tomcat 7
# mkdir /env
# mkdir /env/tomcat
# cd /env/tomcat
# curl http://apache.go-parts.com/tomcat/tomcat-7/v7.0.68/bin/apache-tomcat-7.0.68.tar.gz >
apache-tomcat-7.0.68.tar.gz
# tar zxvf apache-tomcat-7.0.68.tar.gz
Configure Tomcat 7
Modify the file "/etc/rc.d/init.d/tomcat" accordingly:
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
JAVA_HOME=/opt/jdk1.7.0_79
CATALINA_HOME=/env/tomcat/apache-tomcat-7.0.68
export JAVA_HOME CATALINA_HOME
exec $CATALINA_HOME/bin/catalina.sh $*
Build Local .war file
Step 1. Use Eclipse Export tool to generate your project .war file; Step 2. Use 7-z archive tool to remove your data: conf,data,install (this is a update) Step 3. Upload by WinScp your modified .war unto /env/tomcat/apache-tomcat-7.0.68/webapps/
Migrate customer's data
Step 1. Shutdow your old Tomcat: ./apache-tomcat-x.x.xx/bin/shutdown.sh Step 2. Start your new Tomcat: ./apache-tomcat-7.0.68/bin/startup.sh to deploy your .war contents Step 3. Shutdown ./apache-tomcat-7.0.68/bin/shutdown.sh Step 4. Copy the removed parts: # cp -rf apache-tomcat-x.x.xx/webapps/myContext/data apache-tomcat-7.0.68/webapps/myContext/ # cp -rf apache-tomcat-x.x.xx/webapps/myContext/conf apache-tomcat-7.0.68/webapps/myContext/ Step 5. Modify apache-tomcat-7.0.68/webapps/myContext/conf by adding additional settings Step 6. Start your new Tomcat: ./apache-tomcat-7.0.68/bin/startup.sh
|
|
|
|
|
|
|
AwsEC2 member offline |
|
posts: |
39 |
joined: |
08/28/2012 |
from: |
CA |
|
|
|
|
|
Increase Tomcat JVM Heap Memory |
It turns out that Tomcat has the default memory setting of around 128MB. If you web application needs more memory to run within the Tomcat container, it is VERY IMPORTANT to increase the heap memory for Tomcat in file "bin/catalina.sh" as shown below:
CATALINA_OPTS="$CATALINA_OPTS -Xms128M -Xmx512M"
|
|
|
|
|
|
|
|