Wednesday, December 26, 2012


Installing Apache Tomcat on EC2 (Amazon Linux AMI)


Apache Tomact is an implementation of Java Servlet and JavaServer Pages (JSP) technologies. I recently installed it on an Amazon Linux AMI so that I could serve a website that was created with Java.

Installing Tomcat

yum install tomcat6-ad*
yum install tomcat6-web*
Once it has installed, start the Tomcat service:
/etc/init.d/tomcat5 start
By default, the port number for pages served with Tomcat is 8080. You can verify the server has started by navigating to http://your.server.ip.addr:8080 on your web browser. You should see the following screen:

Setting Up a Tomcat Admin

Before you can access the Tomcat Application manager, you’ll need to update your tomcat-users.xml file:
nano /etc/tomcat6/tomcat-users.xml
You should see the following:
<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<tomcat-users>
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.
-->
<!--
  NOTE:  The sample user and role entries below are wrapped in a comment
  and thus are ignored when reading this file. Do not forget to remove
  <!.. ..> that surrounds them.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
-->
<!-- The host manager webapp is restricted to users with role "admin" -->
<!--<user name="tomcat" password="password" roles="admin" />-->
<!-- The manager webapp is restricted to users with role "manager" -->
<!--<user name="tomcat" password="password" roles="manager" />-->
</tomcat-users>
Right before the closing </tomcat-users> tag, insert:
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager"/>
<role rolename="admin"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
<user username="adminuser" password="securepw" roles="admin,manager"/>
After the file has been modified, Tomcat will need to be restarted:
/etc/init.d/tomcat6 restart

Accessing the Application Manager

From the Tomcat homepage, there is a link to the Tomcat Web Application Manager. You can also access this link directly athttp://your.server.ip.addr:8080/manager/html. When you try to access this page, you will be prompted for a username and password. Enter the adminusername you entered above (in this case, it is adminuser, with a password being the securepw you set in the tomcat-users.xml file above.
Once you are logged in, you will see the Application Manager home page. It will display the applications that are currently being served, allow you to manage currently running apps, and give you options to deploy new web apps.
Under the Applications section, you should see a non-zero number of sessions for /manager - if you've logged in, then you would be included in that number. This number would also get incremented if you access the applications on a different browser window.

Deploying Web Applications



Using the Application Manager, you can deploy a WAR one of two ways:
  • Deploy a directory or WAR file located on server
  • Upload a WAR file
If you are managing Tomcat remotely (on a machine other than the server), it might be easier to choose the second option. This allows you to upload an entire WAR file.
If you decide to deploy a directory or WAR file that is located on the server, you will need to know the path to the WAR or directory.


The Tomcat Directory

Server code is located in
/var/lib/tomcat6/webapps
Configuration files are located in
/etc/tomcat6