201
5. Apache Tomcat
Apache Tomcat is a web container that allows you to serve Java Servlets and JSP Java Server Pages web applications.
The Tomcat 6.0 packages in Ubuntu support two different ways of running Tomcat. You can install them as a classic unique system-wide instance, that will be started at boot time will run as the
tomcat6 unprivileged user. But you can also deploy private instances that will run with your own user rights, and that you should start and stop by yourself. This second way is particularly useful in a
development server context where multiple users need to test on their own private Tomcat instances.
5.1. System-wide installation
To install the Tomcat server, you can enter the following command in the terminal prompt:
sudo apt-get install tomcat6
This will install a Tomcat server with just a default ROOT webapp that displays a minimal It works page by default.
5.2. Configuration
Tomcat configuration files can be found in
etctomcat6
. Only a few common configuration tweaks will be described here, please see Tomcat 6.0 documentation
20
for more. 5.2.1. Changing default ports
By default Tomcat 6.0 runs a HTTP connector on port 8080 and an AJP connector on port 8009. You might want to change those default ports to avoid conflict with another server on the system. This is
done by changing the following lines in
etctomcat6server.xml
:
Connector port=8080 protocol=HTTP1.1 connectionTimeout=20000
redirectPort=8443 ...
Connector port=8009 protocol=AJP1.3 redirectPort=8443
5.2.2. Changing JVM used By default Tomcat will run preferably with OpenJDK-6, then try Suns JVM, then try some
other JVMs. If you have various JVMs installed, you can set which should be used by setting JAVA_HOME in
etcdefaulttomcat6
:
JAVA_HOME=usrlibjvmjava-6-sun
20 http:tomcat.apache.orgtomcat-6.0-docindex.html
202 5.2.3. Declaring users and roles
Usernames, passwords and roles groups can be defined centrally in a Servlet container. In Tomcat 6.0 this is done in the
etctomcat6tomcat-users.xml
file:
role rolename=admin user username=tomcat password=s3cret roles=admin
5.3. Using Tomcat standard webapps
Tomcat is shipped with webapps that you can install for documentation, administration or demo purposes.
5.3.1. Tomcat documentation The tomcat6-docs package contains Tomcat 6.0 documentation, packaged as a webapp that you
can access by default at http:yourserver:8080docs. You can install it by entering the following command in the terminal prompt:
sudo apt-get install tomcat6-docs
5.3.2. Tomcat administration webapps The tomcat6-admin package contains two webapps that can be used to administer the Tomcat server
using a web interface. You can install them by entering the following command in the terminal prompt:
sudo apt-get install tomcat6-admin
The first one is the manager webapp, which you can access by default at http:yourserver:8080 managerhtml. It is primarily used to get server status and restart webapps.
Access to the manager application is protected by default: you need to define a user with the role manager in
etctomcat6tomcat-users.xml
before you can access it. The second one is the host-manager webapp, which you can access by default at http:
yourserver:8080host-managerhtml. It can be used to create virtual hosts dynamically. Access to the host-manager application is also protected by default: you need to define a
user with the role admin in
etctomcat6tomcat-users.xml
before you can access it. For security reasons, the tomcat6 user cannot write to the
etctomcat6
directory by default. Some features in these admin webapps application deployment, virtual host creation need write access to
that directory. If you want to use these features execute the following, to give users in the tomcat6 group the necessary rights:
203
sudo chgrp -R tomcat6 etctomcat6 sudo chmod -R g+w etctomcat6
5.3.3. Tomcat examples webapps The tomcat6-examples package contains two webapps that can be used to test or demonstrate Servlets
and JSP features, which you can access them by default at http:yourserver:8080examples. You can install them by entering the following command in the terminal prompt:
sudo apt-get install tomcat6-examples
5.4. Using private instances
Tomcat is heavily used in development and testing scenarios where using a single system-wide instance doesnt meet the requirements of multiple users on a single system. The Tomcat 6.0 packages
in Ubuntu come with tools to help deploy your own user-oriented instances, allowing every user on a system to run without root rights separate private instances while still using the system-installed
libraries. It is possible to run the system-wide instance and the private instances in parallel, as long as
they do not use the same TCP ports. 5.4.1. Installing private instance support
You can install everything necessary to run private instances by entering the following command in the terminal prompt:
sudo apt-get install tomcat6-user
5.4.2. Creating a private instance You can create a private instance directory by entering the following command in the terminal
prompt:
tomcat6-instance-create my-instance
This will create a new
my-instance
directory with all the necessary subdirectories and scripts. You can for example install your common libraries in the
lib
subdirectory and deploy your webapps in the
webapps
subdirectory. No webapps are deployed by default. 5.4.3. Configuring your private instance
You will find the classic Tomcat configuration files for your private instance in the
conf
subdirectory. You should for example certainly edit the
confserver.xml
file to change the default ports used by your private Tomcat instance to avoid conflict with other instances that might be
running.
204 5.4.4. Startingstopping your private instance
You can start your private instance by entering the following command in the terminal prompt supposing your instance is located in the
my-instance
directory:
my-instancebinstartup.sh
You should check the
logs
subdirectory for any error. If you have a java.net.BindException: Address already in usenull:8080 error, it means that the port
youre using is already taken and that you should change it. You can stop your instance by entering the following command in the terminal prompt supposing
your instance is located in the
my-instance
directory:
my-instancebinshutdown.sh
5.5. References
• See the Apache Tomcat
21
website for more information. • Tomcat: The Definitive Guide
22
is a good resource for building web applications with Tomcat. • For additional books see the Tomcat Books
23
list page. • Also, see theUbuntu Wiki Apache Tomcat
24
page.
21 http:tomcat.apache.org 22 http:oreilly.comcatalog9780596003180
23 http:wiki.apache.orgtomcatTomcatBooks 24 https:help.ubuntu.comcommunityApacheTomcat5
205
Chapter 12. Databases