8.26.2008

Installing Tomcat 6.x on CentOS 5


UPDATE: This post was revised to work with java6u10 and now includes instructions for automated startup config.
FYI: if you used this post before 11/06, there have been some major changes to the start up script. This new version is the most basic and easiest. ty.

This is a quick and dirty guide for installing Apache Tomcat 6.0.18 on CentOS5. It is based on detailed instructions for CentOS 4 and tomcat 6.0.13 here. If you get no luck, please check out this link for comprehensive explanation and adopt it to your platform.
Install pre-requisite
1. Download java JDK 6 Update 10 and Java Runtime Environment (JRE) 6 Update 10 bin (NOT RPM)

2. Open terminal, su to root and move downloaded files to /root directory
$ su root
Password:
# mv [.....]
3. Create java environment
# mkdir /usr/java
# cd /usr/java
4. Execute downloaded jdk and jre bins.
# sh /root/jre-6u10-linux-i586.bin
# sh /root/jdk-6u10-linux-i586.bin
5. verify install
# ls
you should see that your /usr/java directory now contains jdk1.6.0_10 and jre1.6.0_10 directories
Install overview
1. Download and extract Apache Ant (apache-ant-1.7.1-bin.tar.gz) to /usr/share
# cd /usr/share
# tar -xzf apache-ant-1.7.1-bin.tar.gz
2. Download and extract Apache Tomcat (apache-tomcat-6.0.18.tar.gz) to /usr/share
# tar -xzf apache-tomcat-6.0.18.tar.gz
3. Enable ant link
# ln -s /usr/share/apache-ant-1.7.1/bin/ant /usr/bin
4. Set up JAVA_HOME $env in catalina.sh
# cd /usr/share/apache-tomcat-6.0.18/bin
# vi catalina.sh
5. After the first line, add following
JAVA_HOME=/usr/java/jdk1.6.0_10
6. Test config with Tomcat
# cd /usr/share/apache-tomcat-6.0.18/bin
# ./startup.sh
7. Check logs for errors
# less /usr/share/apache-tomcat-6.0.18/logs/catalina.out
8. Fix errors if any. Go to http://localhost:8080 for Tomcat's web interface.
Automate start up
1. Create script in /etc/init.d for automated start up/shutdown
# cd /etc/init.d
# vi tomcat

2. Place following in the file
#!/bin/bash
# chkconfig: 234 20 80
# description: Tomcat Server basic start/shutdown script
# processname: tomcat
JAVA_HOME=/usr/java/jdk1.6.0_10
export JAVA_HOME
TOMCAT_HOME=/usr/share/apache-tomcat-6.0.18/bin
START_TOMCAT=/usr/share/apache-tomcat-6.0.18/bin/startup.sh
STOP_TOMCAT=/usr/share/apache-tomcat-6.0.18/bin/shutdown.sh
start() {
echo -n "Starting tomcat: "
cd $TOMCAT_HOME
${START_TOMCAT}
echo "done."
}
stop() {
echo -n "Shutting down tomcat: "
cd $TOMCAT_HOME
${STOP_TOMCAT}
echo "done."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 10
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac
exit 0

2. Change permissions
# chmod 755 tomcat
3. Add script to system services
# chkconfig --add tomcat
4. Verify modifications (this script uses levels 2-4)
# chkconfig --level 234 tomcat on
# chkconfig --list tomcat
you should see that service using levels 2, 3 and 4:
tomcat 0:off 1:off 2:on 3:on 4:on 5:off 6:off
5. Test script start up/shutdown
# service tomcat start
# service tomcat stop
So, at this point, tomcat service will start automatically upon reboot. G'luck (:

62 comments:

Anonymous said...

thanks. works.

Anonymous said...

Worked first time - nice clear instructions.

Awesome!!!

Anonymous said...

Great instructions! How do you ensure that tomcat starts up automatically upon a reboot? Thanks!

skyMyrka said...

thanks (:

sorry, i've been too busy to edit the post for the start up script. it's a bit tricky, but the instructions are here
-- just make sure you've modified path/file names.

Anonymous said...

Thanks a lot for the link! I got it all done, but I'm having problems with starting tomcat as a service -
when I call the modified Tomcat5.sh script I get the following errors:
05/11/2008 09:45:05 6131 jsvc error: Cannot set group id for user 'tomcat'
05/11/2008 09:45:05 6130 jsvc error: Error validating user 'tomcat'

Any idea? User tomcat exists. When I run it as root, I get no error message, but the service is still not working.

Any help would be greatly appreciated.

Thanks!

skyMyrka said...

updated post with startup/shutdown script generation. Should all be well.

Anonymous said...

Thank you much for this..was having a frustrating time

Anonymous said...

This is a fantastic post, thanks buckets! FYI, still works great with java 6u12, which is the current version as of this post.

skyMyrka said...

thanks, Roger (:
yea, this works with all the other versions (just be sure to modify version numbers, etc..)


Also, got some emails about start up not running. Look into your levels guys. This one is for 2, 3, 4. Could differ on your system.

Anonymous said...

Hi again! I actually ran into the startup not working. I think (I'm by no means an experienced Linux administrator) that it has something to do with the tomcat service being started by a non-root user.

After following your instructions, I had a discussion with experienced sysadmins and they suggested I not start the tomcat service with the root user for security reasons. After a quick search, I found this post quite useful for setting that part up:

http://linux-sxs.org/internet_serving/c140.html#NON-ROOT

After yet a bit more search, I found how to get the automatic startup of the tomcat service running again at reboot in the following:

http://www.linuxquestions.org/questions/linux-security-4/boot-script-execute-command-as-non-root-user-124673/

It basically involves adding one line to /etc/rc.local.

I hope someone more knowledgeable than myself can make sense of all this. All I can say is that your post added to the two ones cited above got everything working the way I wanted.

Thanks again, I hope this latest post is useful to others.

skyMyrka said...

Hey Roger:
This script can run for non-root users (it does for me).
But, yes, you add script to script to /etc/init.d (see step 1 of automating start up section in the post) before running chkconfig if you're having problems. Also, check your levels. Turn them all on, see if it runs, then shut off the ones you don't need.

Anonymous said...

Wow, really simple instruction. Thnx, man.

Marcos Orallo said...

Thank you for the straightforward guide, it has been really useful!

Just two notes:
- I think you just need the JDK, not JRE
- You can use the RPM (-rpm.bin) package from SUN, and it will set env variables for you, so you don't have to modify catalina.sh

skyMyrka said...

Thanks, Morgg:

Generally, if running Tomcat under ver5.5 full JDK is a requirement, but onwards you can use either JRE or a JDK. Depends on what you want to do w/your JSPs, so choose wisely (:

Chris said...

Perfect!

Thank you very much, much appreciated...

Anonymous said...

Hey - Great post - thanks! I have been going crazy though..I am stuck on this error:

[root@localhost init.d]# ls -ltr tomcat
-rwxr-xr-x 1 root root 758 May 3 23:22 tomcat
[root@localhost init.d]# chmod 755 /etc/init.d/tomcat
[root@localhost init.d]# chkconfig --add tomcat
service tomcat does not support chkconfig
[root@localhost init.d]#

I cannot add tomcat to chkconfig. I have no idea what the problem is and cannot find any documentation explaining this error. This is the top portion of my tomcat file:

#!/bin/bash

# tomcat Startup Script for Tomcat

# chkconfig: - 85 15

# description Tomcat Server basic start/shutdown script

# processname:tomcat
# pidfile: /var/run/tomcat.pid

# Source function library.
. /etc/rc.d/init.d/functions


JAVA_HOME=/usr/java/default

export JAVA_HOME

TOMCAT_HOME=/usr/share/apache-tomcat-6.0.18/bin
START_TOMCAT=/usr/share/apache-tomcat-6.0.18/bin/startup.sh
STOP_TOMCAT=/usr/share/apache-tomcat-6.0.18/bin/shutdown.sh
start() {
echo -n "Starting tomcat: "
"tomcat" 52L, 758C
... etc


Any ideas what I need to do to be able to add tomcat to chkconfig?

Thanks

John

skyMyrka said...

Hey John:
#1 suspect is always make sure the first line of your script is
#!/bin/bashCheck paths for init.d and stuff like that

I have another suggestion tho
Use my automated start up directions above (remove all the stuff you've added before tho).
Get it to run, and then replace my version of the script with yours.

Yon said...

Great!!!
Worked for CentOS 5.3 with java 1.6 update 14 after other suggestions did not

Anonymous said...

Changing the startup script to the following runs as non root user tomcat.

echo -n "Starting tomcat: "
su -p -s /bin/sh tomcat /usr/share/apache-tomcat-6.0.20/bin/startup.sh
echo "done."

Anonymous said...

Forgot to include the shutdown script.

stop() {
echo -n "Shutting down tomcat: "
su -p -s /bin/sh tomcat /usr/share/apache-tomcat-6.0.20/bin/shutdown.sh
echo "done."

Anonymous said...

thanks dude.

that helped a ton.

java is a pain in the ass if u ask me.

not to mention slow. :)

Anonymous said...

wow...nice post...works fine.thank you very much...solve half of my problem.

btw...im newbi in linux.
can anyone help solve the other half of my problem...pls..pls.

i would like to make "Apache,Tomcat with mod_jk installation and configuration on centos5"

does anyone has a detail,ste-by-step,good guide for me???

i found some guidelines but some instructions confius me...such a noob

need help
zul
bighorn5971@yahoo.com

Anonymous said...

Thanks a lot. It works well.

:)

chetu said...

Cool nice post


cheers folks

Anonymous said...

awesome, thanks.

Strange that something so easy should be so hard to find...

(I'm not really a sysadmin)

Professor Wang said...

Thanks, this is the best post about Tomcat installation I have ever seen. Thank you sooooo much. You saved my time!

Anonymous said...

Yeah, that worked very well.

I didn't bother installing the JRE, only installed the JDK and everything seems to work well.

My centos 5.3 comes with java 1.4.2 already installed and that remains the default version (displayed when I type java -version).

Is there anyway to get the new JDK to be listed by alternatives --display java?

Idea factory worker said...

Should we be using 'server' jvm instead of client?

When tomcat starts up it loads the client JVM. I know when installing tomcat on windows as a service the recommendation is to go into the configuration and change the jvm dll from the one in the client directory to the server directory.

Apparently the server JVM hotspot optimization takes a little longer to start up but when it does it produces faster code - which is great for a JVM that runs for months at a time. The client JVM, on the other hand, is meant for fast startup - like when running applets that are starting and stopping all the time for which which quick start up is more important than more optimized execution.

My tomcat lib path includes this:
java.library.path: /usr/java/jdk1.6.0_16/jre/lib/i386/client:/
so I should probably change that 'client' to 'sever' I'm guessing

Jackson Thompson said...

I made a few changes to the script to make it write to the screen like a normal rc script:

#!/bin/bash
# chkconfig: 234 20 80
# description: Tomcat Server basic start/shutdown script
# processname: tomcat

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0
prog="tomcat"

runlevel=$(set -- $(runlevel); eval "echo \$$#" )

JAVA_HOME=/etc/alternatives/java_sdk
export JAVA_HOME
TOMCAT_HOME=/opt/tomcat/bin
START_TOMCAT=$TOMCAT_HOME/startup.sh
STOP_TOMCAT=$TOMCAT_HOME/shutdown.sh

start()
{
echo -n $"Starting $prog: "
cd $TOMCAT_HOME
${START_TOMCAT} && success || failure
RETVAL=$?
echo
}

stop() {
echo -n "Stopping $prog: "
cd $TOMCAT_HOME
${STOP_TOMCAT} && success || failure
RETVAL=$?
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 10
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac
exit $RETVAL

You will also need to comment out some echos in catalina.sh, in my version they start at line 209:

#if [ $have_tty -eq 1 ]; then
# echo "Using CATALINA_BASE: $CATALINA_BASE"
# echo "Using CATALINA_HOME: $CATALINA_HOME"
# echo "Using CATALINA_TMPDIR: $CATALINA_TMPDIR"
# if [ "$1" = "debug" -o "$1" = "javac" ] ; then
# echo "Using JAVA_HOME: $JAVA_HOME"
# else
# echo "Using JRE_HOME: $JRE_HOME"
# fi
#fi

But thanks the instructions worked great.

skyMyrka said...

jackson, that was sweet
thx!!

Mac said...

Hi,
I have configured logrotate which creates log file every day.
but I think this needs to be service restarted every time. can we do some thing like httpd service we do reload.
for eg. /etc/init.d/httpd reload
for tomcat /etc/init.d/tomcat reload

Please suggest.

Anonymous said...

Thanks!
This is a great resource for a first timer like me.

Anonymous said...

This is great info. very useful...

Anonymous said...

great stuff!! the easiest instructions out there!!

BTW, you really dont need to install both jdk and jre. I suggest you install jdk if you are using jsps though.

You dont really need Ant installed. Just java jdk and tomcat and you are good to go!

Anonymous said...

This post is really effective and impressive.

First try and it works!

Thanks for this.

Reywo

Anonymous said...

This helped me a lot! Thank you :)

Anonymous said...

Please help:

I unpacked apache-tomcat-6.0.24. Followed your instructions to a tee.

Not starting up! I checked the catalina logs and this is what it said.

What does this mean and what do I have to do to fix it??

--------------------------------

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/catalina/s
tartup/Bootstrap
Caused by: java.lang.ClassNotFoundException: org.apache.catalina.startup.Bootstr
ap
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: org.apache.catalina.startup.Bootstrap. Program w
ill exit.

skyMyrka said...

spidey:
make sure you're downloading the correct package for tomcat.

get binary release (tar file), not source distribution. start over.

Anonymous said...

Thanks :)

Set the right permissions on dir
apache-tomcat if user isn't root...

Anonymous said...

Just wanted to say thank you for this tutorial. Worked great, 1st time though.

vIKAS said...

Those looking for the prerequisite downloads can find it here.

Will save your few mins of google-ing :-)


- Java Platform Standard Edition (Java SE) 6 Update 10 http://java.sun.com/products/archive/j2se/6u10/index.html
- Tomcat from http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz
- Apache Ant from http://archive.apache.org/dist/ant/binaries/apache-ant-1.7.1-bin.tar.gz

Anonymous said...

I was able to get things up and running in no time with this. I really appreciate you putting together a guide that is so clean and concise.

Anonymous said...

[root@localhost bin]# ./startup.sh
Using CATALINA_BASE: /usr/share/apache-tomcat-6.0.26-src
Using CATALINA_HOME: /usr/share/apache-tomcat-6.0.26-src
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-6.0.26-src/temp
Using JRE_HOME: /usr/java/jdk1.6.0_20
Using CLASSPATH: /usr/share/apache-tomcat-6.0.26-src/bin/bootstrap.jar
touch: ne peut faire un touch sur `/usr/share/apache-tomcat-6.0.26-src/logs/catalina.out': Aucun fichier ou répertoire de ce type
/usr/share/apache-tomcat-6.0.26-src/bin/catalina.sh: line 334: /usr/share/apache-tomcat-6.0.26-src/logs/catalina.out: Aucun fichier ou répertoire de ce type


Please whats the probleme
i can't find the logs directorie

skyMyrka said...

1. verify that directory /usr/share/apache-tomcat-6.0.26-src/logs/exist
2. if it doesn't, create it
3. if it does, verify you installed correct distribution

Anonymous said...

after check log for errors the shell shows

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/catalina/startup/Bootstrap
Caused by: java.lang.ClassNotFoundException: org.apache.catalina.startup.Bootstrap
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/catalina/startup/Bootstrap
Caused by: java.lang.ClassNotFoundException: org.apache.catalina.startup.Bootstrap
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/catalina/startup/Bootstrap
Caused by: java.lang.ClassNotFoundException: org.apache.catalina.startup.Bootstrap
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
~
~


what may I do? please help.

skyMyrka said...

you downloaded the wrong package for java. __follow___ instructions, start over... Get binary distribution not the source code distribution

svenan said...

great great grea!!!

dorothy said...

Thanks for this. It has been very helpful to me!

whitetr6 said...

awesome. a rare post that works start to finish. thanks!!

Anonymous said...

Thanks a lot

jit said...

Its really useful, Thanks a lot,
jitendra

Troy said...

Excellent job, worked the first time, well done!

skyMyrka said...

wow. this post is almost 4yrs old and ppl are still using it (:

maybe i should update it or smth.

Prashant Redkar said...

Hey its help me lot :) thanks lot

verofairy said...

please could you help me... I do everything as this post said, but it doesn´t work anyway...!!

When I put: #service tomcat start
shows this message: No such file or directory , but the file is into init.d, I tried the command #ls , and I can see it.

could anybody help me... I'm beginner and I don´t know how to solve this problem..

I have done some tries but, shows me another error like : sintax errors in the tomcat file.

please, please help me

skyMyrka said...

under "Automate start up" make sure you've followed through steps 2, 3 and 4.

Anonymous said...

Amazing and Fast solution

Beta said...

I want to install tomcat on my CentOS, thanks already to guide how to install.

branlr said...

Very Very good instructions!

Highly recommended for CentOS 5.7/RHEL.

The jpackage rpm, for me, failed to stop service, performed worse.

This works brilliantly.

Just use updated version numbers for all the files you download, and follow the directions exactly.

Also, for me, I could not find .bin's of the jdk and jre, just tarballs--but extracting in the usr/java/ directory worked great!

Anonymous said...

very clear instruction..
great work..
makes installation very easy

Kumar said...

everything worked for me till i enter this command.
When I put: #service tomcat start
shows this message: No such file or directory , but the file is into init.d, I tried the command #ls , and I can see tomcat file.
and also executed steps 2,3,4 under "Automate start up" without error. Can anybody help ? why i am getting the above error

Marwan said...

Great Work Bro.
Thanks for sharing.

Marwan
zamanteman.blogspot.com