Linux Help Desk

Linux Help Desk

Saturday, October 24, 2015

How To Install Apache Tomcat 7 in CentOS 6

To install Apache Tomcat on CentOS, make sure you have the latest version of java installed on your system. Otherwise just download the Java RPM or BIN from the following link:
http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u25-download-346242.html

Mininumun JDK 6 is required to install Apache Tomcat 7

Check which version of java is installed by default, on your system 
[root@server ~] # java -version

Create a directory java under /usr
[root@server ~] # mkdir /usr/java

Go to the download location where you downloaded the JDK file.
[root@server ~] # cd Downloads 

[root@server Downloads] # cp jdk-6u25-linux-i586-rpm.bin /usr/java

[root@server Downloads] # cd /usr/java

[root@server java] # chmod 775 jdk-6u25-linux-i586-rpm.bin 

[root@server java] # ./jdk-6u25-linux-i586-rpm.bin

It will self extract and install java on your system.

Now check the java version 
[root@server java] # java -version

If it still remains the same then execute the following
[root@server java] # ln -sf /usr/java/jdk1.6.0_25/bin/java   /usr/bin/java 
(Here s is for softlink, f is forcefully)

Now verify the java version, it should now point out to the new version.

Create a Tomcat system user account responsible for running tomcat as running it with root is not a good practice and it will create a entry to break into the system. 
[root@server java] # useradd -r tomcat

Download the tomcat package from the apache tomcat website using the following link
http://apache.petsads.us/tomcat/tomcat-7/v7.0.40/bin/apache-tomcat-7.0.40.tar.gz

After downloading the package copy the package to the /usr/local directory.
[root@server java] # cd /root/Downloads

[root@server Downloads] # cp apache-tomcat-7.0.40.tar.gz  /usr/local

[root@server Downloads] # cd /usr/local

[root@server local] # tar -xvzf apache-tomcat-7.0.40.tar.gz

[root@server local] # mv apache-tomcat-7.0.40 apache-tomcat7

[root@server local] # chown -R tomcat.tomcat apache-tomcat7

Making the Tomcat script.
[root@server ~] # vim /etc/init.d/tomcat

#!/bin/bash
#
# tomcat       Starts Tomcat Java server.
#
#
# chkconfig: 345 88 12
# description: Tomcat is the server for
# Java servlet applications.
### BEGIN INIT INFO
# Provides: $tomcat
### END INIT INFO

JAVA_HOME=
/usr/java/jdk1.6.0_25
 export JAVA_HOME 
PATH=$JAVA_HOME/bin:$PATH 
export PATH 
TOMCAT_HOME=/usr/local/apache-tomcat7
START_TOMCAT=/usr/local/tomcat7/bin/startup.sh
STOP_TOMCAT=/usr/local/tomcat7/bin/shutdown.sh

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

[ -f $START_TOMCAT ] || exit 0
[ -f $STOP_TOMCAT ] || exit 0

RETVAL=0

umask 077

start() {
        echo -n $"Starting Tomcat Java server: "
        daemon su -c $START_TOMCAT tomcat
        echo
        return $RETVAL
}
stop() {
        echo -n $"Shutting down Tomcat Java server: "
        daemon su -c $STOP_TOMCAT tomcat
        echo
        return $RETVAL
}
restart() {
        stop
        start
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
        restart
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac
exit $?
 

Change the permission on the tomcat script that we just created.
[root@server ~] # chmod 755 /etc/init.d/tomcat

Add the tomcat to the chkconfig
[root@server ~] # chkconfig --add tomcat

[root@server ~] # chkconfig tomcat on

Test the script by starting, stopping and restarting the tomcat service.
[root@server init.d] # service tomcat start

[root@server ~] # service tomcat stop

[root@server ~] # service tomcat restart

Check for errors in the file /usr/local/apache-tomcat-7/logs/catalina.out
[root@server bin] # cat /usr/local/apache-tomcat7/logs/catalina.out

If you dont have any errors then you can proceed further, or else rectify it first. 

Now open a browser and test the homepage of the tomcat.
[root@server init.d] # firefox http://localhost:8080