Installing tomcat6 on CentOS

Make sure you replace software versions used in this blog with the one that you download.

Installing JAVA

Before installing tomcat you need JDK and JRE 6 installed on your server. You can download them from following links with wget in your home (~) directory. Make sure you download the .bin file and not the rpm.bin.

JDK

JRE

Download JDK

wget jdk-6u21-linux-x64.bin?AuthParam=1302867934….bin

Download JRE

wget jre-6u18-linux-x64.bin?AuthParam=130286…bin

Most probably the downloaded file will have URL parameters attached to them, you can rename these files using the mv command

mv jdk-6u21-linux-x64.bin?AuthParam=1302867934….bin jdk-6u21-linux-x64.bin mv jre-6u18-linux-x64.bin?AuthParam=130286…bin jre-6u18-linux-x64.bin

now create a directory for installing JDK

sudo mkdir /user/java cd /usr/java

Execute the JDK installation file

sudo sh ~/jdk-6u21-linux-x64.bin

Press Enter to continue. Now install the JRE

sudo sh ~/jre-6u18-linux-x64.bin

A license agreement will be displayed press SPACE to continue through the license and at the end enter YES to proceed with the installation

Installing ANT

You can download the tar.gz file under the Current Release of Ant section from the following link

ANT

Download ant under the /usr/share directory

cd /usr/share sudo wget http://mirror.nyi.net/apache//ant/binaries/apache-ant-1.8.2-bin.tar.gz

Extract the downloaded file

sudo tar -xzf apache-ant-1.8.2-bin.tar.gz

Create symbolic link for ant so other applications can find it

sudo ln -s /usr/share/apache-ant-1.8.2/bin/ant /usr/bin

Installing Tomcat

You can download the latest core version (.tar.gz) under the Binary Distributions section from the following link

Tomcat6

Download tomcat under /usr/share directory

cd /usr/share sudo wget http://apache.mirrors.redwire.net/tomcat/tomcat-6/v6.0.32/bin/apache-tomcat-6.0.32.tar.gz

Extract the downloaded file

sudo tar -xzf apache-tomcat-6.0.32.tar.gz

Now we need to set JAVA_HOME for tomcat by editing catalina.sh file

cd /usr/share/apache-tomcat-6.0.32/bin sudo vi catalina.sh

above command will open catalina.sh in the vi editor. Press i key to enter the insert mode and just below the line #!/bin/sh enter the JAVA_HOME path.

JAVA_HOME=/usr/java/jdk1.6.0_21

Press ESC to leave the insert mode and enter :wq to save and quit the file. Now test the tomcat installation

cd /usr/share/apache-tomcat-6.0.32/bin/ sudo ./startup.sh

If the installation was successful you should see something like this

Using CATALINA_BASE:   /usr/share/apache-tomcat-6.0.32 Using CATALINA_HOME:   /usr/share/apache-tomcat-6.0.32 Using CATALINA_TMPDIR: /usr/share/apache-tomcat-6.0.32/temp Using JRE_HOME:        /usr/java/jdk1.6.0_21 Using CLASSPATH:       /usr/share/apache-tomcat-6.0.32/bin/bootstrap.jar

Create tomcat as a Service

Create a new file tomcat under /etc/init.d directory

cd /etc/init.d/ sudo vi tomcat

above command will open a vi editor, press i key to enter the insert mode and copy paste in the following

#!/bin/bash
# chkconfig: 234 20 80
# description: Tomcat Server basic start/shutdown script
# processname: tomcat
JAVA_HOME=/usr/java/jdk1.6.0_21
export JAVA_HOME
TOMCAT_HOME=/usr/share/apache-tomcat-6.0.32/bin
START_TOMCAT=/usr/share/apache-tomcat-6.0.32/bin/startup.sh
STOP_TOMCAT=/usr/share/apache-tomcat-6.0.32/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

press ESC to leave the insert mode and enter :wq to save and quit the file.

Change the permission on the file using chmod

sudo chmod 755 tomcat

Add script to start with system services

sudo /sbin/chkconfig --add tomcat sudo /sbin/chkconfig --level 234 tomcat on

Now the service will start when the server is started. Execute following command to test your scripts.

sudo /sbin/service tomcat stop sudo /sbin/service tomcat start

And that’s about it for installing tomcat on CentOS

Important: Once again make sure you replace software versions used in this blog with the ones that you download.