Installing Tomcat
How do I install Tomact on Linux?
To the best of my knowledge, no RPM package is provided for installing Tomcat, so you have to install Tomcat manually. First, you need to install Java SDK (Java Virtual Machine), which is required to run Tomcat, on your Linux machine, whose RPM is available from Sun's site. Visit there, and donload Java (Java SDK) rpm package. Then, unpack the archive, and install it via 'rpm -ivh ...'.
Download the Tomcat source codes,
- Install Java (JDK6)
- download the rpm
- set JAVA_HOME variable via /etc/profile.d/java.sh
# set the java environment export JAVA_HOME=/usr/java/jdk1.6.0_02 export PATH=$JAVA_HOME/bin:$PATH
- Install Apache (2.2.x) via RPM
- > sudo chkconfig httpd on
- > sudo service httpd start
- Install Tomcat
- just copy the binary build folder somewhere you want, e.g., /usr/local/tomcat
- then create a user, named tomcat for running the Tomcat server.
> tar xvfz apache-tomcat-(version).tar.gz # cp -rp apache-tomcat-(version) /usr/local/tomcat # adduser -s /sbin/nologin -g tomcat tomcat # cd /usr/local # chgrp -R tomcat tomcat # cd tomcat # chown tomcat -R temp webapps work logs conf # chmod g+r -R conf
- then create a user, named tomcat for running the Tomcat server.
- just copy the binary build folder somewhere you want, e.g., /usr/local/tomcat
How do I use both Apache and Tomcat web servers?
Confirm the following settings are contained in your Apache's httpd.conf or other setting files:
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
If not exist, add these lines.
Then, set the following proxy setting:
ProxyPass /myapp ajp://localhost:8009/myapp ProxyPassReverseCookiePath / /
This setting will redirect a request for http://(your domain)/myapp to http://localhost:8080/myapp, which is provided by your Tomcat. Then, the Tomcat's response will be bypassed to the Apache web server.
How do I use both of Apach and Tomcat? (Obsolete)
This article becomes obsolete because Apache 2.2.x or later has an integrated support for ProxyPass? (see above).
Apache is said to be more efficient than Tomcat for running CGI programs and providing static web pages. In addition, there are several cases that you may want to use Apache as a primary web server rather than Tomcat. For example, you may not be able to open port 8080, which is the default port number of Tomcat, for some security reasons. Or, there may be a lot of existing web contents provided through Apache. In these cases, you can use JK, which redirects user requests on Apache to the Tomcat server. By using JK2, you can assign an arbitrary URL path, say /myapp, to one of the web applications managed by the Tomcat.
To install JK2, you need httpd-devel rpm package.
> rpm -ivh httpd-devel
Then,
- Build JK2 (tomcat-apache connector) from source: http://tomcat.apache.org/connectors-doc/
- Install httpd-devel with yum
- JK2 build process
in jk2-src > cd native; > ./configure --with-apxs=/usr/sbin/apxs > make > sudo make install
- JK2 Configuration
- /etc/httpd/conf.d/workers.property
worker.list=ajp13 worker.ajp13.port=8009 worker.ajp13.host=localhost worker.ajp13.type=ajp13
- /etc/httpd/conf.d/tomcat.conf
LoadModule jk_module modules/mod_jk.so JkWorkersFile conf.d/workers.properties JkLogFile /var/log/httpd/jk.log JkLogLevel error JkOptions +ForwardKeySize JkMount /myapp ajp13 JkMount /myapp/* ajp13
- /etc/httpd/conf.d/workers.property
Repeat the above JkMount? properties if you have other web applications.
How do I automatically start Tomcat on start up ?
- Here is a Tomcat Startup script (/etc/init.d/tomcat).
- > sudo chkconfig tomcat on
- edit JAVA_HOME and CATALINA_HOME environment variables in the script file according to your environment.
/etc/init.d/tomcat
#!/bin/sh
#
# Startup script for the tomcat
#
# chkconfig: 345 80 15
# description: Tomcat is a Servlet+JSP Engine.
# Source function library.
. /etc/rc.d/init.d/functions
export JAVA_HOME=/usr/java/jdk1.6.0_02
export CATALINA_HOME=/usr/local/tomcat
envsetup="export JAVA_HOME=$JAVA_HOME; export CATALINA_HOME=$CATALINA_HOME;"
prog=tomcat
lockfile=/var/lock/subsys/tomcat
RETVAL=0
start(){
echo -n $"Starting $prog: "
daemon --user "tomcat" "$envsetup $CATALINA_HOME/bin/startup.sh"
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
daemon --user "tomcat" "$envsetup $CATALINA_HOME/bin/shutdown.sh -force"
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile}
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $prog {start|stop|restart}"
exit 1
esac
exit $RETVAL
Now, you can start/stop/restart from the following command:
> sudo /etc/init.d/tomcat start > sudo /etc/init.d/tomcat restart > sudo /etc/init.d/tomcat stop
How do I deploy war?
Use tomcat manager http://localhost:8080/manager/html .
If you Tomcat web server is behind a firewall, use port forwarding:
> ssh -L18080:localhost:8080 (your server)
which redirects port accesses to 18080 on your machine to the remote 8080 port. So, you can access your Tomcat manager on the remote machine from http://localhost:18080.
How do I deploy war file from a command-line?
Use Ant http://ant.apache.org, which is similar to GNU Make, and group a sequence of operations for creating war file and deploying Tomcat. Ant is avaiable as an RPM package.
> sudo yum install ant
Ant loads build.xml file from the working directory. Following shows an example of build.xml:
<project name="myapp" default="compile" basedir=".">
<description>
my web application build file
</description>
<property name="conf" value="build.properties" />
<property name="conf.static" value="build.static.properties" />
<!-- load user defined properties -->
<property file="${conf}" />
<property file="${conf.static}" />
<!-- set classpath -->
<path id="project.classpath">
<pathelement path="${java.classpath}/" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${webapp.lib}">
<include name="**/*.jar" />
</fileset>
<pathelement path="${src}" />
<pathelement path="${test}" />
<pathelement path="${build}" />
</path>
<path id="ant.classpath">
<pathelement path="lib/catalina-ant.jar" />
</path>
<target name="compile"
description="Compile src to bin"
depends="confirm-build-properties">
<mkdir dir="${build}" />
<javac srcdir="${src}:${test}"
destdir="${build}"
includes="**"
debug="on"
debuglevel="lines,vars,source"
source="1.5">
<classpath refid="project.classpath" />
</javac>
</target>
<target name="confirm-build-properties"
description="test the existence of build.properties file">
<available file="build.properties" property="build.properties.present" />
<fail unless="build.properties.present">
build.properties not found. Copy build.properties.sample file
as build.properties, then edit the file.</fail>
</target>
<target name="test"
depends="compile,confirm-build-properties"
description="performe JUnit tests">
<java classpathref="project.classpath"
classname="org.junit.runner.JUnitCore"
fork="true">
<classpath path="${test}" />
<arg value="${junit.testsuite}" />
</java>
</target>
<target name="clean">
<!-- Delete the bin directory tree -->
<delete file="${webapp.war}" />
<delete>
<fileset dir="${build}" includes="**/*.class" />
</delete>
</target>
<target name="war"
depends="compile"
description="create a war (web application archive) file">
<war destfile="${webapp.war}" webxml="${webapp.inf}/web.xml">
<fileset dir="${webapp.dir}" />
<classes dir="${build}" />
<metainf file="${webapp.context}" />
<lib dir="${lib}">
<include name="**/*.jar" />
</lib>
</war>
</target>
<target name="reload">
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
<classpath refid="ant.classpath" />
</taskdef>
<reload url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="${webapp.path}" />
</target>
<target name="undeploy">
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask">
<classpath refid="ant.classpath" />
</taskdef>
<undeploy url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="${webapp.path}" />
</target>
<target name="deploy"
depends="war"
description="deplay .war file into TOMCAT_HOME/${tomcat.deploy.dir}">
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask">
<classpath refid="ant.classpath" />
</taskdef>
<deploy url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="${webapp.path}"
update="true"
war="${webapp.war}" />
</target>
<target name="list">
<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
<classpath refid="ant.classpath" />
</taskdef>
<list url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}" />
</target>
<target name="install">
<taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
<classpath refid="ant.classpath" />
</taskdef>
<install url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="${webapp.path}"
config="${webapp.context}" />
</target>
</project>
Create a build.property file to describe your Tomcat installation folder.
> cp build.properties.sample build.properties > vi build.properties
build.properties
#webapp.name=myapp # change the following line to alter the deployment target #webapp.path=/myapp # set the following property to alter the context.xml file #webapp.context=/somewhere/context.xml # You have to set the tomcat installation folder and the manager password TOMCAT_HOME=C:/Program Files/Apache Software Foundation/Tomcat 6.0 tomcat.deploy.dir=webapps tomcat.base.url=http://localhost:8080 tomcat.manager.username=admin tomcat.manager.password=
Run the following command:
> ant deploy
which will compile and deploy your application to the Tomcat web server specified in your build.properties file.
Note that to run the above build script, you need catalina-ant.jar file in your web application's lib folder.
Test the URL http://localhost:8080/myapp, and http://(your-server address)/myapp if you set up the JK2.
I prepared an web application example with these build.xml files etc. at SVN repository http://www.xerial.org/svn/project/sandbox/trunk/webapp-blanc


