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

Posted by sebastian
Nov 24 2008

Dieser Artikel ist eine überarbeitete Version meines Tomcat 5.5 Artikels. Ich habe diesmal die Installation des Open Bluedragon in einen eigenen Artikel getrennt beschrieben. Es soll Tomcat 6 unter Debian Lenny installiert werden und mit mehreren virtuellen Hosts betrieben werden. Tomcat 6 ist unter Debian leider nicht über die Quellen zu beziehen, deshalb muss er manuell installiert werden. (Ubuntu bietet Tomcat 6 per apt-Installation an)

1. Installation von Java 6 runtime:

Eine der folgenden Java JDK Installationen wählen:

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

Bemerkung: Nicht vergessen, dass eine andere Konfiguration beim Apache Connector erforderlich sein wird, sollte Sun JDK verwendet werden.

2. Download Tomcat

Download Tomcat 6 von der Tomcat 6 Download Seite.

Zum Beispiel:

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

3. Installation von Tomcat

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

4. Init-Skript erstellen

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. Aktivieren des Tomcat Managers

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

Nun kann man den Tomcat Manager mit http://SERVER:8080/manager/html aufrufen.

5. Installation des Apache2 Connectors

Tomcat besitzt zwar einen eingebauten Webserver, jedoch ist der Apache2 Webserver wesentlich leistungsfähiger außerdem sind so die Apache Module verwendbar. (mod_rewrite zum Beipiel)

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

Bemerkung: Falls die non-free Java Runtime von Sun verwendet wird, muss “/usr/lib/jvm/java-6-openjdk” durch “/usr/lib/jvm/java-6-sun” ersetzt werden.

JK Konfigurationsdatei

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. Neuen Virtuellen Host anlegen

Nun soll ein neuer VirtualHost angelegt werden. Dieser muss im Apache und im Tomcat angelegt werden.

6.1 Verzeichnisse anlegen

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>

Bemerkung: Man könnte auch alle Dateien an den Tomcat weiterleiten “JkMount /*” oder alle Dateien innerhalb eines Verzeichnisses “JkMount /folder/*“.

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

Bemerkung: Man kann weitere Domains mit:

<alias>additionaldomain.com</alias>

hinzufügen.

7. Testseite anlegen

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>

Nun kann man durch den Aufruf von http://www.testsrv.local/test.jsp die Konfiguration/Installation testen.

30 Responses

  1. [...] Von diesen Artikel ist eine neue Version Verfügbar Tomcat 6 mit Lenny und OpenBlueDragon auf Tomcat 6 Share and [...]

  2. [...] den Adobe Coldfusion 8 Server auf einem Tomcat 6 mit virtuellen Hosts installiert. Bitte zuerst den Apache Tomcat 6 Artikel für die Basisinstallation [...]

  3. [...] man den OpenBlueDragon Server auf einem Tomcat 6 mit virtuellen Hosts installiert. Bitte zuerst den Apache Tomcat 6 Artikel für die Basisinstallation [...]

  4. Timo says:

    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

  5. sebastian says:

    Hallo,

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

    Danke

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

  7. rolando says:

    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

  8. sebastian says:

    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 !

  9. Rolando says:

    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

  10. Rolando says:

    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

  11. sebastian says:

    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

  12. rolando says:

    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

  13. kurt says:

    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)

  14. Mr. Nobody says:

    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.

  15. bourbaki says:

    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.

  16. sebastian says:

    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

  17. bourbaki says:

    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?

  18. sebastian says:

    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

  19. Stefan says:

    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?

  20. sebastian says:

    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

  21. KrisBelucci says:

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

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

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

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

  25. sebastian says:

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

  26. Azhar says:

    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

  27. Parames says:

    this works great on ubuntu too

    thanks Man

  28. Damien says:

    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 ! :)

  29. sebastian says:

    Thank you !

    I removed the extra space from the example.

  30. Khedr says:

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

Trackback URL for this entry