1. Install OpenJDK Java 7 runtime:1. Installation von OpenJDK Java 7 runtime:
aptitude install openjdk-7-jre
2. Download Tomcat
Download Tomcat 8 fromvon der Tomcat 8 Download pageSeite.
For example:Zum Beispiel:
wget http://ftp.fau.de/apache/tomcat/tomcat-8/v8.0.5/bin/apache-tomcat-8.0.5.tar.gz
3. Install Tomcat3. Installation von Tomcat
tar -xzvf apache-tomcat-8.0.5.tar.gz mv apache-tomcat-8.0.5 /opt/tomcat
4. Create tomcat user and group4. Tomcat Benutzer und Gruppe anlegen
groupadd tomcat useradd -g tomcat -d /opt/tomcat tomcat usermod -G www-data tomcat chown tomcat:tomcat /opt/tomcat -R
This create a new user „tomcat“ and a group „tomcat“. It set the home directory for this user to „/opt/tomcat“ and join the „tomcat“ user the „www-data“ group. This is necessary to access the virtual hosts. Finally give the „/opt/tomcat“ directory to the new „tomcat“ user.Dies legt einen neuen Benutzer „tomcat“ und eine neue Gruppe „tomcat“ an. Außerdem wird das Home-Directory dieses Benutzers auf „/opt/tomcat“ gesetzt. Der Benutzer wird außerdem in die Gruppe „www-data“ verschoben. Dies ist notwendig um Zugriff auf die virtuellen Hosts des Apaches zu haben. Zum Schluss werden dem neuen Benutzer „tomcat“ noch die Besitzrechte für den Ordner „/opt/tomcat“ übertragen.
5. Create Init-Script5. Init-Skript erstellen
vim /etc/init.d/tomcat
#!/bin/sh # # /etc/init.d/tomcat -- startup script for the Tomcat 8 servlet engine # # Modified init-Script from Ubuntu Tomcat init-script # # 2014 - Sebastian Mogilowski - http://www.mogilowski.net # ### BEGIN INIT INFO # Provides: tomcat # Required-Start: $local_fs $remote_fs $network # Required-Stop: $local_fs $remote_fs $network # Should-Start: $named # Should-Stop: $named # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start Tomcat. # Description: Start the Tomcat servlet engine. ### END INIT INFO set -e PATH=/bin:/usr/bin:/sbin:/usr/sbin:/opt/tomcat NAME=tomcat DESC="Tomcat servlet engine" DEFAULT=/etc/default/$NAME JVM_TMP=/tmp/tomcat-tmp if [ `id -u` -ne 0 ]; then echo "You need root privileges to run this script" exit 1 fi # Make sure tomcat is started with system locale if [ -r /etc/default/locale ]; then . /etc/default/locale export LANG fi . /lib/lsb/init-functions if [ -r /etc/default/rcS ]; then . /etc/default/rcS fi # The following variables can be overwritten in $DEFAULT # Run Tomcat 8 as this user ID and group ID TOMCAT_USER=tomcat TOMCAT_GROUP=tomcat # The first existing directory is used for JAVA_HOME (if JAVA_HOME is not # defined in $DEFAULT) JDK_DIRS="/usr/lib/jvm/java-7-openjdk-amd64 /usr/lib/jvm/java-7-openjdk-common" # Look for the right JVM to use for jdir in $JDK_DIRS; do if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then JAVA_HOME="$jdir" fi done export JAVA_HOME # Directory where the Tomcat binary distribution resides CATALINA_HOME=/opt/$NAME # Directory for per-instance configuration files and webapps CATALINA_BASE=/opt/$NAME # Use the Java security manager? (yes/no) TOMCAT_SECURITY=no # Default Java options # Set java.awt.headless=true if JAVA_OPTS is not set so the # Xalan XSL transformer can work without X11 display on JDK 1.4+ # It also looks like the default heap size of 64M is not enough for most cases # so the maximum heap size is set to 128M if [ -z "$JAVA_OPTS" ]; then JAVA_OPTS="-Djava.awt.headless=true -Xmx128M" fi # End of variables that can be overwritten in $DEFAULT # overwrite settings from default file if [ -f "$DEFAULT" ]; then . "$DEFAULT" fi if [ ! -f "$CATALINA_HOME/bin/bootstrap.jar" ]; then log_failure_msg "$NAME is not installed" exit 1 fi POLICY_CACHE="$CATALINA_BASE/work/catalina.policy" if [ -z "$CATALINA_TMPDIR" ]; then CATALINA_TMPDIR="$JVM_TMP" fi # Set the JSP compiler if set in the tomcat.default file if [ -n "$JSP_COMPILER" ]; then JAVA_OPTS="$JAVA_OPTS -Dbuild.compiler=\"$JSP_COMPILER\"" fi SECURITY="" if [ "$TOMCAT_SECURITY" = "yes" ]; then SECURITY="-security" fi # Define other required variables CATALINA_PID="/var/run/$NAME.pid" CATALINA_SH="$CATALINA_HOME/bin/catalina.sh" # Look for Java Secure Sockets Extension (JSSE) JARs if [ -z "${JSSE_HOME}" -a -r "${JAVA_HOME}/jre/lib/jsse.jar" ]; then JSSE_HOME="${JAVA_HOME}/jre/" fi catalina_sh() { # Escape any double quotes in the value of JAVA_OPTS JAVA_OPTS="$(echo $JAVA_OPTS | sed 's/\"/\\\"/g')" AUTHBIND_COMMAND="" if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true" AUTHBIND_COMMAND="/usr/bin/authbind --deep /bin/bash -c " fi # Define the command to run Tomcat's catalina.sh as a daemon # set -a tells sh to export assigned variables to spawned shells. TOMCAT_SH="set -a; JAVA_HOME=\"$JAVA_HOME\"; source \"$DEFAULT\"; \ CATALINA_HOME=\"$CATALINA_HOME\"; \ CATALINA_BASE=\"$CATALINA_BASE\"; \ JAVA_OPTS=\"$JAVA_OPTS\"; \ CATALINA_PID=\"$CATALINA_PID\"; \ CATALINA_TMPDIR=\"$CATALINA_TMPDIR\"; \ LANG=\"$LANG\"; JSSE_HOME=\"$JSSE_HOME\"; \ cd \"$CATALINA_BASE\"; \ \"$CATALINA_SH\" $@" if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then TOMCAT_SH="'$TOMCAT_SH'" fi # Run the catalina.sh script as a daemon set +e touch "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out chown $TOMCAT_USER "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out start-stop-daemon --start -b -u "$TOMCAT_USER" -g "$TOMCAT_GROUP" \ -c "$TOMCAT_USER" -d "$CATALINA_TMPDIR" \ -x /bin/bash -- -c "$AUTHBIND_COMMAND $TOMCAT_SH" status="$?" set +a -e return $status } case "$1" in start) if [ -z "$JAVA_HOME" ]; then log_failure_msg "no JDK found - please set JAVA_HOME" exit 1 fi if [ ! -d "$CATALINA_BASE/conf" ]; then log_failure_msg "invalid CATALINA_BASE: $CATALINA_BASE" exit 1 fi log_daemon_msg "Starting $DESC" "$NAME" if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \ --user $TOMCAT_USER --exec "$JAVA_HOME/bin/java" \ >/dev/null; then # Regenerate POLICY_CACHE file umask 022 echo "// AUTO-GENERATED FILE from /opt/tomcat/" \ > "$POLICY_CACHE" echo "" >> "$POLICY_CACHE" cat $CATALINA_BASE/conf/*.policy \ >> "$POLICY_CACHE" # Remove / recreate JVM_TMP directory rm -rf "$JVM_TMP" mkdir -p "$JVM_TMP" || { log_failure_msg "could not create JVM temporary directory" exit 1 } chown $TOMCAT_USER "$JVM_TMP" catalina_sh start $SECURITY sleep 5 if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \ --user $TOMCAT_USER --exec "$JAVA_HOME/bin/java" \ >/dev/null; then if [ -f "$CATALINA_PID" ]; then rm -f "$CATALINA_PID" fi log_end_msg 1 else log_end_msg 0 fi else log_progress_msg "(already running)" log_end_msg 0 fi ;; stop) log_daemon_msg "Stopping $DESC" "$NAME" set +e if [ -f "$CATALINA_PID" ]; then start-stop-daemon --stop --pidfile "$CATALINA_PID" \ --user "$TOMCAT_USER" \ --retry=TERM/20/KILL/5 >/dev/null if [ $? -eq 1 ]; then log_progress_msg "$DESC is not running but pid file exists, cleaning up" elif [ $? -eq 3 ]; then PID="`cat $CATALINA_PID`" log_failure_msg "Failed to stop $NAME (pid $PID)" exit 1 fi rm -f "$CATALINA_PID" rm -rf "$JVM_TMP" else log_progress_msg "(not running)" fi log_end_msg 0 set -e ;; status) set +e start-stop-daemon --test --start --pidfile "$CATALINA_PID" \ --user $TOMCAT_USER --exec "$JAVA_HOME/bin/java" \ >/dev/null 2>&1 if [ "$?" = "0" ]; then if [ -f "$CATALINA_PID" ]; then log_success_msg "$DESC is not running, but pid file exists." exit 1 else log_success_msg "$DESC is not running." exit 3 fi else log_success_msg "$DESC is running with pid `cat $CATALINA_PID`" fi set -e ;; restart|force-reload) if [ -f "$CATALINA_PID" ]; then $0 stop sleep 1 fi $0 start ;; try-restart) if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \ --user $TOMCAT_USER --exec "$JAVA_HOME/bin/java" \ >/dev/null; then $0 start fi ;; *) log_success_msg "Usage: $0 {start|stop|restart|try-restart|force-reload|status}" exit 1 ;; esac exit 0
Download the init-script:Herrunterladen des Init-Skripts: [download#21#size#nohits]
chmod +x /etc/init.d/tomcat update-rc.d tomcat defaults
6. Activate the Tomcat manager6. 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="manager-gui"/> <role rolename="admin"/> <role rolename="admin-gui"/> <user username="YOUR_USERNAME" password="YOUR_PASSWORD" roles="admin,admin-gui,manager,manager-gui"/> </tomcat-users>
6. Start Tomcat
/etc/init.d/tomcat start
Now you can access the Tomcat manager with http://SERVER:8080/manager.Nun kann man den Tomcat Manager mit http://SERVER:8080/manager aufrufen.
7. Install Apache2 connector7. 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)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
7.1 Apache worker7.1 Apache worker
Nun muss man noch die Einstellungen des Workers anpassen:Now confirate the Apache worker:
vim /etc/libapache2-mod-jk/workers.properties
Die folgenden beiden Werte müssen angepasst werden:You have to adjust the following values:
workers.tomcat_home=/opt/tomcat workers.java_home=/usr/lib/jvm/java-7-openjdk-amd64
Anschließend muss der Apache Webserver sowie dier Tomcatserver neu gestartet werden:You have to restart Apache Webserver and Tomcatserver to apply the changes:
/etc/init.d/apache2 stop /etc/init.d/tomcat restart /etc/init.d/apache2 start
8. Create a new VirtualHost8. Neuen Virtuellen Host anlegen
Nun soll ein neuer VirtualHost angelegt werden. Dieser muss im Apache und im Tomcat angelegt werden.Creating a new VirtualHost: (In Apache AND Tomcat)
8.1 Create directories8.1 Verzeichnisse anlegen
mkdir /var/www/vhost1 mkdir /var/www/vhost1/htdocs mkdir /var/www/vhost1/logs
vim /etc/apache2/sites-available/vhost1
8.2 Apache
<virtualhost www.testsrv.local> JkMount /*.jsp ajp13_worker 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/*“.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
8.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>
Note: Make sure you use a uppercase „H“, „C“ and „V“ for „Host“, „Content“ and „Value“. This is importand since Tomcat 7.Bemerkung: Bitte sicherstellen, dass das „H“, „C“ und „V“ von „Host“, „Content“ und „Value“ groß geschrieben sind. Dies ist wichtig seit Version 7 von Tomcat.
/etc/init.d/tomcat restart
Note: You can add additional domains with:Bemerkung: Man kann weitere Domains mit:
<Alias>additionaldomain.com</Alias>
hinzufügen.
9. Create a Testpage9. 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>
Now you can test your configuration with http://www.testsrv.local/test.jspNun kann man durch den Aufruf von http://www.testsrv.local/test.jsp die Konfiguration/Installation testen.
Ruslan
6 Jun. 2014Great guide, thanks for posting. I have installed Apache Tomcat 8 on my Centos machine combining yours and the guide below.
http://www.rosehosting.com/blog/how-to-install-tomcat-8-on-a-centos-6-vps/
Waldemar
17 Nov. 2014Hi,
Danke für das Tutorial. Ich haben nur ein Problem mit dem vorletzten Schritt. Nach dem ich die server.xml angepasst habe wird tomcat nicht neu gestartet, es wird überhaupt nicht mehr gestartet. Ohne Fehlermeldung.
Danke
sebastian
17 Nov. 2014Dann wirst du wohl einen Fehler in der server.xml haben. Nur ohne die zu sehen und ohne die Fehlermeldung(en) kann man da nix genaues dazu sagen.
Waldemar
19 Nov. 2014Danke, die server.xml war kaputt. Ich habe aber dennoch eine Frage. Wie kann ich diesen Tutorial auf meinen miet-Server der auf debian 7 läuft so anwenden das ich die .jsp unter /.jsp ansprechen kann? Also hinter dem Port 80.
Waldemar
19 Nov. 2014Ooo da wurden wohl Teile des Texts verschluckt. Gemeint habe ich:
Wie kann ich diesen Tutorial auf meinen miet-Server der auf debian 7 läuft so anwenden, dass ich die .jsp unter Hostname/beispiel.jsp ansprechen kann? Also hinter dem Port 80.
Claud
7 Dez. 2014Thanks for this article, but the init script does not work for me as I expected on my CentOS 7. I was looking for a solution on the Internet and found that I will need to use a systemd unit script instead. I found this one and it might be very helpful for the others.
https://www.rosehosting.com/blog/installing-tomcat-8-on-a-centos-7-linux-vps/
Thanks.
Pingback: Install Apache Tomcat 8 on Debian 7 (Wheezy) with virtual hosts and Apache2 integration | Sebastian Mogilowskis Blog – yeees.in
Mike
2 Jun. 2015Nice article. Thanks, it worked just fine on my wheezy. If you’re looking on how to setup Tomcat 8 with reverse proxy on an Ubuntu 14.04 based VPS then this article is definitely for you https://vpsineu.com/blog/how-to-set-up-tomcat-8-with-nginx-reverse-proxy-on-an-ubuntu-14-04-vps/
It works like a charm
Pingback: Instalar tomcat 8 en Debian 7 Wheezy | elpesodeloslunes
Vlad
9 Aug. 2016Danke, das ist ein toller Tutorial. Ich habe aber ein Problem beim Aufrufen der test.jsp. Als Antwort bekomme ich ein „Internal Server Error“