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)

UPDATE: Please read the new article: Tomcat 7 on Debian

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

This Post Has 64 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

    1. Hallo,

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

      Danke

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

  3. 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

    1. 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 PostgresSQL Driver in the context tag the jdbc resource.

      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 !

  4. 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

  5. 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

  6. 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

  7. 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)

  8. 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.

  9. 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.

    1. 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

  10. 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?

    1. 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

  11. 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?

  12. 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

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

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

  15. 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

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

  17. As soon as possible … 🙂
    I currently working on a new startup script.

  18. this works great on ubuntu too

    thanks Man

  19. 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 ! 🙂

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

  21. Hi Ralf,

    leider etwas wenig Information. Aber das Verzeichnis wird hier hauptsächlich durch den Apache bestimmt. Das solltest du mal überprüfen.

    Gruß

    Sebastian

  22. Vielen Dank für dieses geniale HowTo!
    Vielleicht solltest du noch erwähnen, dass
    man die Kommentare in der config der User noch
    löschen muss.

    Liebe Grüße,
    Das Müsli 🙂

  23. Ralf,

    welche Version von Tomcat verwendest du ?
    Ich bekomme diesen Fehler mit Tomcat 7 (welcher aktuell noch Beta ist !) versuch es doch mal mit Tomcat 6

    Gruß

    Sebastian

  24. Bei funktioniert es nicht auf Anhieb Deine Anleitung, ich hab dann noch eine gute Hilfe hier gefunden:
    http://oreilly.com/java/archive/tomcat-tips.html

    Den Fehler den ich hatte war mit der groß und klein Schrebung (klar Linux!) und zwar bei mir ging es erst nachdem ich in der server.xml die mit ersetzt habe genau so wie beim .

    Trotzdem vielen Dank für die klasse Anleitung.

  25. Zu spät um noch richt auf zupassen, also in der server.xml
    host -> Host ersetzt
    alias -> Alias ersetzt

    dann ging es.

    1. Hi elch,

      richtig Host muss mit einem großem H geschrieben werden (usw.). Hat aber nichts mit Windows oder Linux zu tun. Das ist erst seit Tomcat 7 so.

      Gruß

      Sebastian

  26. Alles schön und gut, aber nach Deiner Anleitung läuft der Tomcat unter root. Bei Debian wird hingegen ein ziemlicher Aufwand getrieben, um das ganze sicher zu machen. Das ersparst Du Dir geflissentlich. Deshalb ist diese Anleitung mit Vorsicht zu genießen.

    Ich empfehle, sich die /etc/init.d/tomcat5.5 anzusehen und die auf /opt/tomcat6 umzukupfern.


    Christoph

    1. Hi Christoph,

      ja du hast Recht, Tomcat unter root laufen zu lassen ist nicht optimal.

      Ich hab das in meinem Tomcat 7 Artikel berücksichtigt.

      So groß ist der Aufwand gar nicht. Der User muss nur in der richtigen Gruppe sein und eben die Startparameter berücksichtigen um den Server unter dem Tomcat-User laufen zu lassen.
      Außerdem müssen die Rechte auf dem Dateisystem entsprechend eingestellt sein.

      Gruß

      Sebastian

  27. Hi folks,

    don’t forget that the apache will run under “root” user and that is not what you want…

    See the new version where you can find the steps to run tomcat under “tomcat” user

  28. Hallo,

    mein Deutsch ist nicht sher gut, deshalb sage ich mein Problem auf English.

    I installed tomcat on Debian. when I start my tomcat via my ssh session it works but when I close my session it doesn’t work any more!

    I used :
    update-rc.d tomcat defaults
    to install tomcat as service but it’s not working!

    Ich danke mich im Voraus auch für die Antworten.

    Ali

  29. HI

    Thanks for the article, unfortunately I am unable to do so the 2nd time around.

    Unable to open
    lynx http://localhost:8080 -> not found
    as well as
    lynx http://www.testsrv.local/test.jsp -> making HTTP connection for very long time, just to the say unable to connect to remote host . . .!?!?!?

    I have also added the line with my local IP and http://www.testsrv.local to /etc/hosts page to get rid of a previous error in which Apache complained about being able to resolve testsrv addy. . .

    I am new to Linux and need this set-up with Apache and tomcat 6 !! in order to run our applications . .

    Any idea where my error could be, I have chosen this article to follow, as most people seem to succeed . .

    Many thanks for any hints and tips here, greatly appreciated the whole article.
    Kind regards
    Alex

  30. edited:

    I have also added a line to /etc/hosts file
    LO.CAL.IP.ADD http://www.testsrv.local

    did that to get rid of a previously experienced error !
    regards
    alex

  31. Hi,

    on online servers just replace testsrv.local with your domain.

    If you just make a local setup on your personal computer, replace testsrv.local with localhost in the apache and tomcat configuration.

    Please check out the tomcat 7 article, too. There are changes in the config. If you use the new versions you have to notice that.

    Greetings

    Sebastian

  32. Hi Sebastian,
    danke für das gute Tutorial.
    Leider erreiche ich die Startseite vom Tomcat (lokal und über’s Netz) nicht.
    Unser Apache zwingt alle Aufrufe von Port 80 auf 443. Schätze, unser Problem liegt irgendwo in der SSL Ecke. Hast Du eine Idee wo? Muss ich Tomcat noch SSL beibringen?
    Gruß
    Andreas

      1. Hi Sebastian,
        da war ich wohl in wenig zu knapp in meiner Beschreibung; also:
        1) Apache läuft, alle Anfragen auf http werden auf https umgeleitet.
        2) Tomcatinstallation nach deinem Tut. fertig, Tomcat startet ohne Fehlermeldungen.
        3) Startseite von Tomcat ist nicht erreichbar, ebenso die von Dir beschriebene Adresse für den Tomcatmanager.
        4) Aufruf von http://www.testsrv.local:443 im Browser liefert die index.html die zu apache gehört.
        5) Habe auf http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html Hinweise gefunden, dass man auch Tomcat SSL beibringen kann, weiß nur nicht, ob das in unserem Fall nötig bzw. hilfreich ist.
        6) Mit lynx an der Konsole auf dem Server bekomme ich die Seite auch nicht; da läuft eine SSL Fehlermeldung auf.
        Hoffe das macht das Problem ein bisschen runder und Du hast noch ein Tipp für mich.
        Gruß
        Andreas

        1. Hi,

          ok also ich würde erstmal versuchen alles ohne SSL zum laufen zu bringen.

          Die Adresse des Tomcat Managers auf Port 8080 hat mit dem Apache und dem dort konfigurierten SSL nichts zu tun. An Port 8080 läuft der Tomcat direkt. Wenn du dieses also nicht erreichen kannst läuft der Tomcat noch nicht einwandfrei. (Nicht vergessen den Port ggf. hinterher durch Zugriffe von außen zu schützen)

          Wenn wie in meinen HowTo der Apache vor dem Tomcat sitzt, ist eine SSL Konfiguration des Tomcats nicht nötig. Da ja der Apache die SSL Verschlüsselung macht und die Kommunikation zwischen dem Apache und dem Tomcat eigentlich nicht verschlüsselt ablaufen muss (ich gehe dabei davon aus das beide auf der selbe Maschine laufen oder anderweitig sicher miteinander kommunizieren können)

          https://www.testsrv.local/test.jsp wäre wohl der richtige Aufruf. Mit http://www.testsrv.local:443 kommt du hier mal gar nicht weiter.

          Für den SSL Betrieb nach dieser Anleitung ist eigentlich nichts anderes nötig als einen entsprechenden SSL Vhost im Apache anzulegen. Aber zuerst sollte es ohne SSL funktionieren.

          Gruß

          Sebastian

          1. Hi Sebastian,
            ok, also kein SSL für Tomcat.
            Die Zwamgsumleitung auf SSL hab ich zwischenzeitlich ausgeschaltet gehabt – ohne Änderung.
            Dann habe ich (während ausgeschalteter Umleitung) die Tomcatinstallation nach HowTo wiederholt. Keine Änderung im Verhalten – Tomcat zuckt nie und nirgends.
            Einzige negative Rückmeldung während der Installation:
            /etc/init.d/tomcat missing LSB information
            Irgendwelche Abhängigkeiten nicht ok?
            Meldung nach Tomcat Start:

            Using CATALINA_BASE: /opt/tomcat
            Using CATALINA_HOME: /opt/tomcat
            Using CATALINA_TMPDIR: /opt/tomcat/temp
            Using JRE_HOME: /usr
            Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar

            Etwas ratlos 😕
            Andreas

  33. Hm also leider fehlen mir hier die Informationen warum das bei dir nicht funktioniert.

    Das HowTo ist auch noch für das alte Debian Lenny und den alten Tomcat 6.

    Bist du dir sicher das du auch diese beiden Versionen einsetzt ?

  34. Hi Sebastian,

    ja, bin ich:
    Linux **** 2.6.26-2-486 #1 Fri Aug 14 01:02:21 UTC 2009 i686

    The programs included with the Debian GNU/Linux system […]

    und:

    -rw-r–r– 1 root root 6507748 2. Feb 2011 apache-tomcat-6.0.32.tar.gz

    Werde wohl mal eine zweite Installation auf einer anderen Maschine versuchen.

    Die fehlende LSB Information kann’s nicht sein?

    Gruß
    Andreas

    1. Hi Sebastian,
      Problem gelöst: in der server.xml sind die Einträge Case sensitiv, da lag wohl der Hund begraben.

      Jetzt geht’s – vieln Dank nochmal
      Andreas

      1. Case sensitiv in der server.xml ist mir erst in der Version 6 auf 7 aufgefallen als ich alte config files portiert habe.
        Aber Hauptsache es läuft …

  35. What’s up colleagues, fastidious article and good arguments commented here, I am really enjoying by these.

  36. First of all I would like to say awesome blog! I had a quick question that I’d like to ask if you do not mind.
    I was curious to know how you center yourself and clear your thoughts
    before writing. I have had a difficult time clearing my thoughts in getting my thoughts out there.
    I truly do take pleasure in writing but it just seems like the first
    10 to 15 minutes are wasted just trying to figure out how to begin.
    Any suggestions or hints? Thank you!

Schreibe einen Kommentar zu ein lookup Antworten abbrechen

eMail-Benachrichtigung bei weiteren Kommentaren.
Auch möglich: Abo ohne Kommentar.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.