Gentoo Wiki



Introduction to Tomcat

This wiki will show you how to install the Jakarta Project's Tomcat 6 on Gentoo. Tomcat is a servlet container which allows the execution of Java Servlets and JavaServer Pages. Both provide an excellent platform for creating dynamic content. But Tomcat can do much more than just host servlets or JSP's for you. For instance, it can host HTML, PHP and PERL and do about anything a normal webserver can. But if you still prefer Apache, Tomcat can also be configured to work together with Apache (see mod_jk). Then if for some obscure reason your questions aren't answered here please check the Tomcat Website or the Tomcat Wiki.
Then, at last, I will opt some alternatives to Tomcat. Just incase you want something to compare it to.

Since there are many different servlet engine implementations I will not give you a complete list. If you really miss your favorite servlet engine please add it ... That is the whole idea of a wiki :D You could also check this list at Sun or this list at the Wikipedia

Installing Tomcat 6

Installation takes only a simple emerge:

 emerge -av tomcat

Note: If you are using Apache or Apache2, you may want to add the corresponding USE variables to your /etc/make.conf.

Starting & Testing Tomcat 6

To start Tomcat 6 run:

 /etc/init.d/tomcat-6 start 

To see if Tomcat 6 is functioning properly browse to http://localhost:8080 or http://YOUR_SERVER_IP:8080 (since by default Tomcat6 listens on port 8080 and there is a default website installed eventhough you didn't specify the 'examples' in the USE variable).

Note: Ok this was throwing me off. At this stage i got no response. Page was unreachable, but on the server i got no error message and no entry in any of my logs. Don't forget to edit /etc/conf.d/tomcat-6 and select the proper java environment. After switching from Blackdown JDK to Sun JDK it finally worked. Snv 12:55, 19 June 2008 (UTC)

Configuring Tomcat 6

Configuring Tomcat 6 to Start on Boot

To make Tomcat 6 start everytime you reboot your machine:

 rc-update add tomcat-6 default

Changing the webapps Directory

To change the webapps directory in Tomcat 6 you need to edit server.xml, manager.xml and host-manager. But we start by creating the new data directory and copying the default webapps data into the new dir.

Creating the new webapps dir (In this example I use "/data/tomcat/webapps")

 mkdir -p /data/tomcat/webapps/ROOT

There are two ways to do this. You only need to follow one of the below:

... through symbolic links

The easy way is through using symbolic links:

Move the data from the default webapps dir into the new dir:

 cp -R /usr/share/tomcat-6/webapps/* /data/tomcat/webapps
 rm -R /usr/share/tomcat-6/webapps

Create a symbolic link (this will prevent reconfiguration in the future):

 ln -s /data/tomcat/webapps/ /usr/share/tomcat-6/

The ROOT webapplication directory for some reason ended up in /var/lib/tomcat-6/webapps/ thus we need some extra commands to move it

 cp -R /var/lib/tomcat-6/webapps/* /data/tomcat/webapps/
 rm -R /var/lib/tomcat-6/webapps

Again create a symbolic link :

 ln -s /data/tomcat/webapps/ /var/lib/tomcat-6/

Now all the webapplication data has been moved and the symbolic links are made the relocation is complete.

... through changing configuration

If you don't want to use symbolic links or you want to save CPU time by preventing symbolic link loopups here is how :

First server.xml:

 nano -w /etc/tomcat-6/server.xml

Then search for the <host> tag (search for "<host name") then change the appBase to your prefered location (as stated before I use "/data/tomcat/webapps") :

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

Save the file and exit the editor.
Secondly we have to edit manager.xml. Since 2 versions reside on your harddisk we need to edit both. First is the /etc/ manager.xml :

 nano -w /etc/tomcat-6/Catalina/localhost/manager.xml

Change the docBase in the <context> tag:

 <Context docBase="/data/tomcat/webapps/manager"
  privileged="true" antiResourceLocking="false" antiJARLocking="false">

Save the file and exit the editor.

To edit the second copy of manager.xml open this file :

 nano -w /data/tomcat/webapps/manager/manager.xml

Change the docBase in the <context> tag too (like above and save and exit).

Thirdly we need to edit the host-manager.xml file again there reside 2 copies of this file on your harddrive thus we need to edit them both. First one :

 nano -w /etc/tomcat-6/Catalina/localhost/host-manager.xml

Change the docBase in the <context> tag:

 <Context docBase="/data/tomcat/webapps/host-manager" privileged="true"
  antiResourceLocking="false" antiJARLocking="false">

Save the file and exit the editor.

To edit the second copy open this file :

 nano -w /data/tomcat/webapps/host-manager/host-manager.xml

Change the docBase in the <context> tag too (like above and save and exit).

Now restart tomcat to reload the configuration:

/etc/init.d/tomcat-6 restart

Configuring Manager Application Access

This is the short version of how to get into the Manager Web Application. If you want to know more please read the documentation section on the example page. But if you are just intrested in getting in to take a quick look read on

Open the Tomcat users file

  nano -w /etc/tomcat-6/tomcat-users.xml

Create the role "manager" :

   <role rolename="manager"/>

Create a user and give him the "manager" role

   <user username="LetMeIn" password="secret" roles="manager"/>

Restart tomcat

 /etc/init.d/tomcat-6 restart

Then go to http://localhost:8080/manager/html or just click on "Tomcat Manager" from here and use your freshly created user to login

Configuring host-manager Application Access

This is the short version of how to get into the Admin Web Application. If your have read the Manager section it will look very familiar.

Open the Tomcat users file

 nano -w /etc/tomcat-6/tomcat-users.xml

Create the role "admin":

   <role rolename="admin"/>

Create a user and give him the "admin" role

   <user username="LetMeIn" password="secret" roles="manager,admin"/>

Restart tomcat

 /etc/init.d/tomcat-6 restart

Then go to http://localhost:8080/admin/ and use your freshly created user to login

Memory Minimum and Maximum Settings

The following settings will prevent accumulating Java Memory Leaks from consuming all your memory

 nano -w /etc/conf.d/tomcat-6

Uncomment (remove the '#') the JAVA_OPT line and change it to

 JAVA_OPTS="-server -Xms256M -Xmx512M"

(Since memory relocation is slow in java prevent it) You can also use for 32b system :

 JAVA_OPTS="-d32 -server -Xms256M -Xmx512M"

Or for 64b system :

 JAVA_OPTS="-d64 -server -Xms256M -Xmx512M"

Log File Rotation

Log File Rotation prevents accumulating log data from taking over all your harddisk space. Tomcat will rotate the log file each day but doesn't delete obsolete logs (rotate 6 weeks old logs out of existance). When you install gentoo you (should) have emerged logrotation to handle your syslog files. I will use this tool to rotate the logfiles of tomcat. You can also make a bash script and load it into your Cron Daemon however I found it cleaner to keep logrotation in one place and better for maintenance. In case you didn't install logrotation for your syslog here is how :

 emerge logrotate

Main configuration file is /etc/logrotate.conf and additional logrotate files are placed in /etc/logrotate.d/. Then it is time to make our logrotation entry for Tomcat

 nano -w /etc/logrotate.d/tomcat

The script should look like :

Code: tomcat
# Compress tomcat log
# Provided by DouweQuerty

/var/log/tomcat-6/tomcatlog.tar {
  rotate 7
    cd /var/log/tomcat-6/
    tar -C /var/log/tomcat-6/ -cp --remove-files --wildcards -f tomcatlog.tar --exclude "*$(date +%Y-%m-%d)*" *.log
  touch tomcatlog.tar

This will collect (tar) all the existing logfiles, except todays logfile, before rotating them. Because the active logfile isn't touched no reload of tomcat is needed. But we do need to create a dummy tar file else logrotation doesn't do his job.

 echo "" > /var/log/tomcat-6/tomcatlog.tar

Then check whether it is working by forcing a logration

 logrotate -f /etc/logrotate.conf

JDBC Drivers

JDBC is the intermediate between java and the database program of your choosing. Thus allowing your JSPs and Servlets to execute SQL statements and retrieve information from the database. Since the JDBC driver are written in java itself there is a significant speed advantage over the JDBC-ODBC bridge. Here I will show you how to make postgresql databases accessible :

 emerge -av jdbc-postgresql

Then make the jar available in tomcat

 ln -s /usr/share/jdbc-postgresql/lib/jdbc-postgresql.jar /usr/share/tomcat-6/lib/jdbc-postgresql.jar

That is it ... Now you use the flowing code to retrieve data

Code: Java Example
   Class.forName("org.postgresql.Driver"); //load the driver
   Connection db = DriverManager.getConnection("jdbc:postgresql:"+database,username,password); //connect to the db
   Statement sql = db.createStatement(); //create a statement that we can use later
   ResultSet results = sql.executeQuery("select current_date");
   while (

For mysql users : Personally I think mysql fails as database program since it fails to implement key features of the SQL-standard. Therefor I advice everybody not to use mysql until they decides to properly support the SQL language. If you already have mysql running and have it working with tomcat add your experience to this page.

Finishing Up

That's it! Tomcat 6 is now configured and ready to host whatever webpages you had in mind. If if you wish to run Tomcat 6 alone as a webserver you may want to learn about redirecting port 80 to port 8080 in the wiki on iptables. Please keep this page up to date

-- 4 march 2007 (UTC)

Based on: Tomcat 5 Install

Retrieved from ""

Last modified: Fri, 05 Sep 2008 22:27:00 +0000 Hits: 11,443