Install Tomcat 6 on Debian (Lenny) with virtual hosts and Apache2 integration

This article is a new version of my Apache Tomcat 5.5 article. In this version i descripe the installation of the open bluedragon in a seperate article. This article only describes how to install Apache Tomcat 6 on Debian Lenny, Apache2 integration and virtual hosts. Tocat 6 is not available over the regular sources on Debain Lenny (and Etch) it need to be installed by hand. (On Ubuntu you can get Tomcat 6 with an apt-Installation)

1. Install Java 6 runtime:

Choose one of the following Java JDK installations:

1.1 OpenJDK

aptitude install java6-runtime

1.2 Sun JDK (non-free)

vim /etc/apt/sources
deb http://ftp.de.debian.org/debian/ lenny main non-free
deb-src http://ftp.de.debian.org/debian/ lenny main non-free
aptitude update
aptitude install sun-java6-jdk

Note: Keep in mind that you need a different Apache connector configuration if you use the Sun JDK.

2. Download Tomcat

Download Tomcat 6 from Tomcat 6 Download page.

For example:

wget http://apache.imsam.info/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz

3. Install Tomcat

tar -xzvf apache-tomcat-6.0.18.tar.gz
mv apache-tomcat-6.0.18 /opt/tomcat

4. Create Init-Script

vim /etc/init.d/tomcat
#!/bin/sh
# Tomcat Init-Script
case $1 in
    start)
        sh /opt/tomcat/bin/startup.sh
    ;;
    stop)
        sh /opt/tomcat/bin/shutdown.sh
    ;;
    restart)
        sh /opt/tomcat/bin/shutdown.sh
        sh /opt/tomcat/bin/startup.sh
    ;;
esac
exit 0
update-rc.d tomcat defaults

5. Activate the Tomcat manager

vim /opt/tomcat/conf/tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
    <role rolename="manager"/>
    <role rolename="admin"/>
    <user username="YOUR_USERNAME" password="YOUR_PASSWORD" roles="admin,manager"/>
</tomcat-users>

6. Start Tomcat

/etc/init.d/tomcat start

Now you can access the Tomcat manager with http://SERVER:8080/manager/html.

5. Install Apache2 connector

You can use the Tomcat as a standalone webserver, but the apache webserver has more features and you can use the apache modules. (mod_rewrite for example)

aptitude install apache2 libapache2-mod-jk
vim /etc/apache2/workers.properties
workers.tomcat_home=/opt/tomcat
workers.java_home=/usr/lib/jvm/java-6-openjdk
ps=/
worker.list=default
worker.default.port=8009
worker.default.host=localhost
worker.default.type=ajp13
worker.default.lbfactor=1

Note: Replace “/usr/lib/jvm/java-6-openjdk” with “/usr/lib/jvm/java-6-sun” if you using the non-free Sun Java runtime.

JK configuration file

vim /etc/apache2/conf.d/jk.conf
<ifmodule mod_jk.c>
    JkWorkersFile /etc/apache2/workers.properties
    JkLogFile /var/log/apache2/mod_jk.log
    JkLogLevel error
</ifmodule>
/etc/init.d/apache2 stop
/etc/init.d/tomcat restart
/etc/init.d/apache2 start

6. Create a new VirtualHost

Creating a new VirtualHost: (In Apache AND Tomcat)

6.1 Create directories

mkdir /var/www/vhost1
mkdir /var/www/vhost1/htdocs
mkdir /var/www/vhost1/logs
vim /etc/apache2/sites-available/vhost1

6.2 Apache

<virtualhost www.testsrv.local>
    JkMount /*.jsp default
    ServerName www.testsrv.local
    ServerAdmin servermaster@testsrv.local
    DocumentRoot /var/www/vhost1/htdocs
    ErrorLog /var/www/vhost1/logs/error.log
    CustomLog /var/www/vhost1/logs/access.log common
    <directory /var/www/vhost1/htdocs>
        Options -Indexes
    </directory>
</virtualhost>

Note: You can forward all files “JkMount /*” or all files in a folder “JkMount /folder/*” to the Tomcat, too.

a2ensite vhost1
/etc/init.d/apache2 reload

6.3 Tomcat

vim /opt/tomcat/conf/server.xml
<!-- www.testsrv.local -->
<host name="www.testsrv.local" appBase="/var/www/vhost1" unpackWARs="true" autoDeploy="true">
    <context path="" docBase="htdocs" debug="0" reloadable="true"/>
    <valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/www/vhost1/logs"  prefix="tomcat_access_" suffix=".log" pattern="common" resolveHosts="false"/>
</host>
/etc/init.d/tomcat restart

Note: You can add additional domains with:

<alias>additionaldomain.com</alias>

7. Create a Testpage

vim /var/www/vhost1/htdocs/test.jsp
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <h1>Hello World</h1>
        Today is: <%= new java.util.Date().toString() %>
    </body>
</html>

Now you can test your configuration with http://www.testsrv.local/test.jsp

Leave a comment

30 Comments.

  1. Danke für das kleine howto. Hat mir weitergeholfen :)

    Ein kleiner Fehler hat sich in der tomcat-users.xml eingeschlichen: Die Tags müssen und heissen.

    Cheers

  2. Hallo,

    danke für den Hinweis. Ich denke ich habe alles entsprechend korrigiert, auch wenn die Tags aus deinem Kommentar entfernt wurden …

    Danke

  3. This page of instructions has been extremely helpful. Many thanks for putting together this comprehensive and well laid out page.

  4. hi is a great how to but i have a lot of doubts 1. how i make that test page shows in my others computer a lan of four computer witn s.o. windows 2 do you know how connect this configuration with postgresql
    bye

  5. Hi rolando,

    i don’t understand your problem with the lan computers, i need more informations.

    For PostgresSQL you need the JDBC driver from http://jdbc.postgresql.org/download and save this in the “lib” folder of your “WEB-INF” folder.

    And insert between the context tag the jdbc resource:

    postgressql tomcat

    More informations:
    http://www.howtoforge.com/xwiki-tomcat-mysql-debian-etch
    http://www.michael-brockhaus.de/howtos/java/tomcat_dbcp_postgres.html

    Please keep me up to date with your configuration !

  6. hi thanks for response, well its not problem with the lan the thing is i try to make a web server rigth now i have apache tomcat 6 java jdk 1.0.6 bind 9 and postgresql 8.3 (postgrest i not configured yet is only install) you example works fine my doubt is i want the beta of my web pages is only avaible in my lan and 2 the web page finish can view online in any machine. i running all in ubuntu server 8.10 64 bit edition pc dell server t104
    bye

  7. my lan have five machines 3 with windows vista home premiun wireless,1 with xp, 1 mac i think is s.o. x not sure

  8. Hi Rolando,

    i create a small article http://www.mogilowski.net/?p=346 how to secure apache only for non local network access. I hope it helps …

    greetings

    Sebastian

  9. hello i have a doubt wich connector need this is my version of java i downloaded directly from page of sun Java SE Development Kit 6u12 jdk-6u12-linux-i586-rpm.bin this is for my configurartion of 32 bits and i have another for 64 bits this its Java SE Development Kit 6u12
    jdk-6u12-linux-x64.bin how to know what connector to use
    bye
    pd
    recommend a book for doing this

  10. hi i have tomcat 6 working without any configuration then follow the steps to make a virtual host because i want learn how configured that everything okay but when put /etc/init.d/tomcat restart appears various lines of errors “expected a token in line 54 …” after that i try to open the server.xml to erase the modifications but only appears the erros so i try stop tomcat and the same open another terminal the same so i close all after that let me open server.xml and put the backup restart all no errors…. apparently because now start tomcat but in the browser only says waiting for localhost
    How uninstall tomcat apache2 but deleting all the files for make a clean installation and star over
    take care

    (i made a copy server.xml at the moment apache2 not installed later install)

  11. Three cheers for the creator of this howto!!! I havnt tested it yet but this actually looks like tomcat made operational in a virtual host environment!

    Thank you…Thank you … Thank you.

  12. Hello,

    Thanks for the tutorial. I had 2 questions:

    1°) I followed the instructions above (with OpenJDK). Everything went fine until step 7. When I try the test.jsp, the code was just dumped in the browser instead of being interpreted. Why?

    2°) Where should I place my WAR files for the deployment? In the /opt/tomcat/webapp directory or in the /var/www/vhost1 directory?

    Thanks a lot for your help.

  13. Hi,

    1) Check if the apache connector is installed and working and if you created the JkMount entry in the virtual host configuration.

    2) We moved the tomcat webapp path to appBase=”/var/www/vhost1″ in the host configuration so you have to place the war files there. Make sure you have the setting unpackWARs=”true” and autoDeploy=”true”.

    cheers

    Sebastian

  14. Hi Sebastian,

    Thanks for your fast answer!

    I made some modifications (the JkMount entry was not defined indeed) and now, the HTML code is displayed properly but my browser still does not interpred the Java piece of code…

    Any idea?

    Thanks?

  15. Hi,

    make sure you have installed libapache2-mod-jk and check the settings in /etc/apache2/workers.properties

    Watch the syslog on apache startup and take a look at the apache error and access logs.

    greez

    Sebastian

  16. Hallo,

    ich habe eben mal die Anleitung auf meinem Lenny System getestet, zuerst musste ich im Apache vhost1 File in ändern, da sonst bei Aufruf des VHosts im falschen Verzeichnis gesucht wurde. Nun habe ich aber das Problem, das meine test.jsp gefunden wird, allerdings weder der Quelltext dieser, noch die Testseite erscheint. D.h. wenn ich mir den Quelltext anzeigen lasse, ist dieser leer. Irgendwelche Ideen wo der Fehler liegt?

  17. Hi,
    leider wurde deine Änderung im Kommentar nicht mit gespeichert. Kann dir so leider nicht helfen. Pack deine vhost1 config doch mal in http://pastebin.com und schick mir dann den entsprechenden Link dazu.

    greez

    Sebastian

  18. Hi, good post. I have been wondering about this issue,so thanks for posting.

  19. Wow … that is one of the best tutorials I have ever read!
    It works also with little changes in Etch.
    Many thanks … :) )

  20. Thanks for the tutorial!

    One thing is missing which is vital for production use: in your configuration tomcat runs with superusercredentials.

    First this is unnessesary hence tomcat runs by default on a unprevileged port 8080.

    Secondly every application on the tomcat instance will have full rights on your machine. This means every security issue on a single application can cause the full compromise of your machine!

    Please consider using something like

    su tomcat /opt/tomcat/bin/startup.sh

    where tomcat is a nonprivileged user.

    regards from Düsseldorf

  21. How soon will you update your blog? I’m interested in reading some more information on this issue.

  22. As soon as possible … :-)
    I currently working on a new startup script.

  23. Hi Everyone,

    Have a look at the blog given below for multiple instances of apache tomcat6 on debian lenny just in few very easy steps;

    http://www.itoperationz.com/2009/07/multiple-instances-of-apache-tomcat-6-on-debain5-lenny/

    Please post your valueable comments.
    Regards,
    Azhar Ali

  24. this works great on ubuntu too

    thanks Man

  25. Hi there !
    There’s an extra space in the code for your test page : “< %=" should be "<%="
    I spent two hours looking for the reason why i couldn't get this "simple" page working !

    Thanks for this great tutorial by the way ! :)

  26. Thank you !

    I removed the extra space from the example.

  27. It is not working with me!!!!!! I even don’t know where is the mistake!!!

Leave a Reply


[ Ctrl + Enter ]