How 2 install Tomcat under CentOS

Installing, Configuring, Troubleshooting server daemons such as Web and Mail
yyagol
Posts: 1015
Joined: 2006/06/10 18:27:44
Location: 32 4′N 34 47′E
Contact:

How 2 install Tomcat under CentOS

Post by yyagol » 2007/07/28 09:50:13

I just finished installing an apache-tomcat server. for some reason
the tomcat is not included in the repositories ( some distros did include it witch makes it easy to install via yum )
so i download the stand alone server from http://apache.org and started to install it on a fresh machine .
i came up with this how to because there no such one for CentOS, and Tomcat can be very frustrating to understand.
please let me know if i did any thing wrong for this is my first time playing with Tomcat.


1. Download the java jdk and jre from http://www.java.com and install
in my case i use re-6u2-linux-i586-rpm.bin, jdk-6u2-linux-i586-rpm.bin
[code]# mkdir /usr/java
# cd /usr/java
# sh /root/jre-6u2-linux-i586-rpm.bin
# sh /root/jdk-6u2-linux-i586-rpm.bin[/code]
2. Download apache-ant apache-tomcat from http://www.apache.org

3. Unzip ant and tomcatX to /usr/share
[code]# cd /usr/share
# tar -xzf apache-tomcat-6.0.13.tar.gz
# tar -xzf apache-ant-1.7.0-bin.tar.gz[/code]
4. Create link for ant to work
[code]# ln -s /usr/share/apache-ant-1.7.0/bin/ant /usr/bin/[/code]
11. Create $env for JAVA_HOME , can be done inside catalina.sh
[code]# cd /usr/share/apache-tomcat-6.0.13/bin
# vi catalina.sh[/code]
>> Add at the top >Look at the log file inside tomcat directory> We need a compiler , we will use gcc, install the gcc compiler > Edit the script and copy to /etc/init.d > Test to see if it works > Copy the script Tomcat5.sh to /etc/init.d > Add the script to start at boot > Add his script > Add his script <<

service https
{
disable = no
flags = REUSE
socket_type = stream
wait = no
user = root
redirect = 127.0.0.1 8443
log_on_failure += USERID
}

9 . Restart xinetd

10. Start Tomcat
[code]# service Tomcat5 start [/code]


and thats it you got Tomcat ready to use :lol:
to test Tomcat go to http://localhost
Tomcat come with examples located at the left buttom corner
of that page

K_Frye
Posts: 425
Joined: 2005/07/13 01:48:35
Location: Canada

How 2 install Tomcat under CentOS

Post by K_Frye » 2007/07/30 13:52:35

[quote]
yyagol wrote:
I just finished installing an apache-tomcat server. for some reason
the tomcat is not included in the repositories ( some distros did include it witch makes it easy to install via yum )
so i download the stand alone server from http://apache.org and started to install it on a fresh machine .
i came up with this how to because there no such one for CentOS, and Tomcat can be very frustrating to understand.
please let me know if i did any thing wrong for this is my first time playing with Tomcat.
[/quote]

Looks okay to me.

For future reference, Tomcat is offered through JPackage (http://www.jpackage.org).

JPackage Setup:

http://www.jpackage.org/installation.php (Also read the rebuilding nosrc RPMs: http://www.jpackage.org/nosrc.php)
http://www.jpackage.org/jppfaq.php (FAQ)


The guide below is a little out of date but the basic idea is still the same:

http://www.tummy.com/Community/Articles/tomcat-centos/

yyagol
Posts: 1015
Joined: 2006/06/10 18:27:44
Location: 32 4&#8242;N 34 47&#8242;E
Contact:

Re: How 2 install Tomcat under CentOS

Post by yyagol » 2007/07/31 05:00:49

Thanks ,

I did try with the jpackage but got mixed up with all sort of errors,
i wanted to install the latest apache-tomcat and i needed ANT , when i install
ANT from jpakage i got lots of errors. after debuging this errors it came out that the
packages needs to be from the same source.

yyagol
Posts: 1015
Joined: 2006/06/10 18:27:44
Location: 32 4&#8242;N 34 47&#8242;E
Contact:

Re: How 2 install Tomcat under CentOS

Post by yyagol » 2007/08/21 18:49:00

[quote]8. For Tomcat to work on port 80 and 443, create 2 script[/quote]
Do not use this part
use iptables to redirect the ports, it is not secure to use xinetd.
lets say you're machine ip is 192.168.0.2 and you want to redirect all traffic on port 80 to port 8080
where Tomcat is listening , one can do it in a simple line like this :

[code]
# iptables -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.0.2:8080
[/code]

User avatar
toracat
Forum Moderator
Posts: 7439
Joined: 2006/09/03 16:37:24
Location: California, US
Contact:

Re: How 2 install Tomcat under CentOS

Post by toracat » 2008/04/20 23:03:22

yyagol,

I happened to dig this thread out. Do you think this howto can be put on the CentOS wiki? Maybe with some minor adjustments to include CentOS-5? ???

yyagol
Posts: 1015
Joined: 2006/06/10 18:27:44
Location: 32 4&#8242;N 34 47&#8242;E
Contact:

Re: How 2 install Tomcat under CentOS

Post by yyagol » 2008/04/28 05:11:37

yes ,but i have fixed some issues of the startup script
i found out you can use the deamon function under /etc/rc.d/init.d/functions
so here is a script that can start tomcat without the need to compile jsvc
this part can be skipped
[code]#!/bin/sh
#
# Startup script for Tomcat 6.0.13, the Apache Servlet Engine
#
# chkconfig: - 80 20
# description: Tomcat 6.0.13 is the Apache Servlet Engine
# processname: tomcat
# pidfile: /var/run/tomcat5.pid
# config: /usr/share/tomcat/conf/tomcat6.conf
#

# Source function library.
if [ -x /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi

# For SELinux we need to use 'runuser' not 'su'
if [ -x /sbin/runuser ]
then
SU=runuser
else
SU=su
fi

# Get Tomcat config

TOMCAT_CFG="/usr/share/tomcat/conf/tomcat6.conf"

[ -r "$TOMCAT_CFG" ] && . "${TOMCAT_CFG}"

# Path to the tomcat launch script (direct don't use wrapper)
TOMCAT_SCRIPT=/usr/share/tomcat/bin/catalina.sh

# Tomcat name :)
TOMCAT_PROG=tomcat6

# if TOMCAT_USER is not set, use tomcat5 like Apache HTTP server
if [ -z "$TOMCAT_USER" ]; then
TOMCAT_USER="tomcat"
fi

RETVAL=0

# See how we were called.
start() {
printf "Starting %s: " "$TOMCAT_PROG"

if [ -f /var/lock/subsys/tomcat6 ] ; then
if [ -f /var/run/tomcat5.pid ]; then
read kpid < /var/run/tomcat5.pid
if checkpid $kpid 2>&1; then
printf "process allready running\n"
return -1
else
printf "lock file found but no process running for pid %s, continuing\n" "$kpid"
fi
fi
fi

export CATALINA_PID=/var/run/tomcat6.pid
touch $CATALINA_PID
chown $TOMCAT_USER:$TOMCAT_USER $CATALINA_PID

# $TOMCAT_RELINK_SCRIPT

if [ -x /etc/rc.d/init.d/functions ]; then
daemon --user $TOMCAT_USER $TOMCAT_SCRIPT start
else
$SU - $TOMCAT_USER -c "$TOMCAT_SCRIPT start"
fi

RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/tomcat6
return $RETVAL
}

stop() {
printf "Stopping %s: " "$TOMCAT_PROG"

if [ -f /var/lock/subsys/tomcat6 ] ; then
if [ -x /etc/rc.d/init.d/functions ]; then
daemon --user $TOMCAT_USER $TOMCAT_SCRIPT stop
else
$SU - $TOMCAT_USER -c "$TOMCAT_SCRIPT stop"
fi
RETVAL=$?

if [ $RETVAL = 0 ]; then
count=0;

if [ -f /var/run/tomcat6.pid ]; then

read kpid < /var/run/tomcat6.pid
let kwait=$SHUTDOWN_WAIT

until [ `ps --pid $kpid | grep -c $kpid` = '0' ] || [ $count -gt $kwait ]
do
printf "\nwaiting for processes to exit";
sleep 1
let count=$count+1;
done

if [ $count -gt $kwait ]; then
printf "\nkilling processes which didn't stop after %s seconds" "$SHUTDOWN_WAIT"
kill -9 $kpid
fi

if [ $count -gt 0 ]; then
printf "\n"
fi
fi

rm -f /var/lock/subsys/tomcat5 /var/run/tomcat5.pid
fi

fi
}


# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 2
start
;;
condrestart)
if [ -f /var/run/tomcat6.pid ] ; then
stop
start
fi
;;
*)
printf "Usage: %s {start|stop|restart|condrestart}\n" "$TOMCAT_PROG"
exit 1
esac

exit $RETVAL[/code]

note that i am using a tomcat6.conf file , this file needs to be create and can
have all the startup stuff in it like JAVA_OPTS.
also i did not set a new SELinux policy for tomcat because i used tomcat as home dir context
this is not so good for security , if you can have an explanation on how to deploy a new context
and set the policy to use this context ,i guess this can help .

User avatar
toracat
Forum Moderator
Posts: 7439
Joined: 2006/09/03 16:37:24
Location: California, US
Contact:

Re: How 2 install Tomcat under CentOS

Post by toracat » 2008/04/29 01:24:17

It would be great if you could write up a wiki article for the CentOS community...

javaasad
Posts: 1
Joined: 2008/05/30 13:06:25

Re: How 2 install Tomcat under CentOS

Post by javaasad » 2008/05/30 13:20:45

Great man,

for centos 5 the iptables command should be:

iptables -t nat -A PREROUTING -p tcp --dport 80 -i eth0 -j REDIRECT --to-port 8080

change the "eth" accordingly, but your startup script has errors to me on centos5 could any body tell me the problem. The error is:

touch: cannot touch `/usr/java/tomcat/logs/catalina.out': Permission denied
/usr/local/tomcat/bin/catalina.sh: line 291: /usr/java/tomcat/logs/catalina.out: Permission denied
/usr/local/tomcat/bin/catalina.sh: line 300: /usr/java/tomcat/bin/tomcat.pid: Permission denied [FAILED]

I changed the addresses in the scripts to [MyTomcat6Installation] there is tomcat user and group that owns the installation directory. and a little query too, what is tomcat6.conf, what did you place inside it, please tell us,

Thanks,

Asad Rasheed

yyagol
Posts: 1015
Joined: 2006/06/10 18:27:44
Location: 32 4&#8242;N 34 47&#8242;E
Contact:

Re: How 2 install Tomcat under CentOS

Post by yyagol » 2008/06/01 05:15:16

Thanks for the correct on iptables , as for the problem
[quote]
touch: cannot touch `/usr/java/tomcat/logs/catalina.out': Permission denied
/usr/local/tomcat/bin/catalina.sh: line 291: /usr/java/tomcat/logs/catalina.out: Permission denied
/usr/local/tomcat/bin/catalina.sh: line 300: /usr/java/tomcat/bin/tomcat.pid: Permission denied [FAILED][/quote]
my logs are under the tomcat installation directory $TOMCAT_HOME/logs , and my pid file is under /var/run .

i added this lines under catalina.sh , at the top of the script

[code]JAVA_HOME=/usr/java/jdk1.6.0_05
JAVA_OPTS="$JAVA_OPTS -Xmx800m -Xms800m -Xmn500m -Xss64k"
CATALINA_PID=/var/run/tomcat6.pid[/code]
you can also add the CATALINA_HOME=path to tomcat directory

the JAVA_OPTS is not mandatory , its just GC settings and initial memory for java heap
beside that , all works just like that , under 5.1 and 4.6 , i got it running under production
with this settings . during develop test we reached 1000 concurrent connection , and stopped at that
point of loading , because this was a number we just could not dream of. the GC settings are depend
on the application behavior . the objects we use ,die very yang.

yyagol
Posts: 1015
Joined: 2006/06/10 18:27:44
Location: 32 4&#8242;N 34 47&#8242;E
Contact:

Re: How 2 install Tomcat under CentOS

Post by yyagol » 2008/08/02 06:29:15

JAVA_OPTS is additional settings for java runtime env .
tuning this settings takes time and it depend on your application ,
and the life time of the application objects.
i'll explain this settings in general

[code]JAVA_OPTS="$JAVA_OPTS -Xmx800m -Xms800m -Xmn500m -Xss64k"[/code]
-Xmx = maximum heap size
-Xms = initial heap size
-Xmn = heap size for the young generation (objects)
-Xss = Reduces the default maximum thread stack size

This settings are not part of Tomcat install , but if you leave the default Apache.org settings
most likely you will get exception : java heap size , if your server gets many hits.
to understand more of this settings in details ,read this tutorial from java.sun
http://java.sun.com/performance/reference/whitepapers/tuning.html

Post Reply

Return to “CentOS 4 - Server Support”