How to install Tomcat 8 on a CentOS 6 VPS

Leave reply

how-to-install-tomcat-8-on-centos-6-vpsIn the following tutorial you will learn how to install and set-up Apache Tomcat 8 on your CentOS 6 VPS.

What is Tomcat?

Apache Tomcat (before known as Jakarta Tomcat) is an application server developed by the Apache Software Foundation that executes Java servlets and renders Web pages that include Java Server Page coding.

UPDATE THE SYSTEM

Make sure you are in a screen session and your CentOS 6 based Linux Virtual Server is up-to-date by running:

## screen -U -S tomcat8-screen
## yum update

INSTALL JAVA 8

Download the latest JAVA 8 from here or use the following command to download JAVA JDK 8u5:

for 32bit systems use:

## wget --no-cookies \
--no-check-certificate \
--header "Cookie: oraclelicense=accept-securebackup-cookie" \
"http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-i586.rpm" \
-O /opt/jdk-8-linux-i586.rpm

for 64bit systems use:

## wget --no-cookies \
--no-check-certificate \
--header "Cookie: oraclelicense=accept-securebackup-cookie" \
"http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-x64.rpm" \
-O /opt/jdk-8-linux-x64.rpm

once the JAVA package has been downloaded, install it using rpm as follows:

for 32bit systems use:

## rpm -Uvh /opt/jdk-8-linux-i586.rpm

for 64bit systems use:

## rpm -Uvh /opt/jdk-8-linux-x64.rpm

CONFIGURE JAVA

configure the newly installed JAVA package using alternatives as in:

## alternatives --install /usr/bin/java java /usr/java/jdk1.8.0_05/jre/bin/java 20000
## alternatives --install /usr/bin/jar jar /usr/java/jdk1.8.0_05/bin/jar 20000
## alternatives --install /usr/bin/javac javac /usr/java/jdk1.8.0_05/bin/javac 20000
## alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.8.0_05/jre/bin/javaws 20000
## alternatives --set java /usr/java/jdk1.8.0_05/jre/bin/java
## alternatives --set javaws /usr/java/jdk1.8.0_05/jre/bin/javaws
## alternatives --set javac /usr/java/jdk1.8.0_05/bin/javac
## alternatives --set jar /usr/java/jdk1.8.0_05/bin/jar

check the JAVA version running on your system:

## java -version

INSTALL TOMCAT 8

Create a separate user which will run the Tomcat server:

## useradd -r tomcat8 --shell /bin/false

Download the latest Tomcat 8 version from here or use the following command to download Tomcat 8.0.5

## wget http://mirrors.koehn.com/apache/tomcat/tomcat-8/v8.0.5/bin/apache-tomcat-8.0.5.tar.gz -P /tmp

Extract the contents of the Tomcat archive to /opt using the following command:

## tar -zxf /tmp/apache-tomcat-8.0.5.tar.gz -C /opt
## ln -s /opt/apache-tomcat-8.0.5 /opt/tomcat-latest
## chown -hR tomcat8: /opt/tomcat-latest /opt/apache-tomcat-8.0.5

START THE TOMCAT 8 SERVICE

Create the following init script in /etc/init.d/tomcat8

#!/bin/bash
#
# tomcat8
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat8
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: Tomcat 8
# Short-Description: start and stop tomcat
### END INIT INFO

## Source function library.
#. /etc/rc.d/init.d/functions
export JAVA_HOME=/usr/java/default
export JAVA_OPTS="-Dfile.encoding=UTF-8 \
  -Dnet.sf.ehcache.skipUpdateCheck=true \
  -XX:+UseConcMarkSweepGC \
  -XX:+CMSClassUnloadingEnabled \
  -XX:+UseParNewGC \
  -XX:MaxPermSize=128m \
  -Xms512m -Xmx512m"
export PATH=$JAVA_HOME/bin:$PATH
TOMCAT_HOME=/opt/tomcat-latest
TOMCAT_USER=tomcat8
SHUTDOWN_WAIT=20

tomcat_pid() {
  echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ] 
  then
    echo "Tomcat is already running (pid: $pid)"
  else
    # Start tomcat
    echo "Starting tomcat"
    ulimit -n 100000
    umask 007
    /bin/su -p -s /bin/sh $TOMCAT_USER $TOMCAT_HOME/bin/startup.sh
  fi


  return 0
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo "Stoping Tomcat"
    /bin/su -p -s /bin/sh $TOMCAT_USER $TOMCAT_HOME/bin/shutdown.sh

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

    if [ $count -gt $kwait ]; then
      echo -n -e "\nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds"
      kill -9 $pid
    fi
  else
    echo "Tomcat is not running"
  fi
 
  return 0
}

case $1 in
start)
  start
;; 
stop)   
  stop
;; 
restart)
  stop
  start
;;
status)
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo "Tomcat is running with pid: $pid"
  else
    echo "Tomcat is not running"
  fi
;; 
esac    
exit 0

make the script executable using chmod

## chmod +x /etc/init.d/tomcat8

Start the Tomcat 8 server using:

## service tomcat8 start

Add the Tomcat 8 service to system startup:

## chkconfig tomcat8 on

Access your newly installed Tomcat at http://YOUR_IP:8080


Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install Tomcat 8 for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.


50% OFF Coupon

EXCLUSIVE EMAIL UPDATES AND “MEMBERS-ONLY” CONTENT


7 Responses to “How to install Tomcat 8 on a CentOS 6 VPS”

  1. Tom

    Great tutorial for both beginners and advanced users.

    Reply
  2. bish

    That alternatives config looks reeeeally wrong. Are you sure it’s okay?

    Reply
    • admin

      Why do you think that the alternatives commands are wrong? Did you experience an error while running one of them?

      Reply
  3. ASK_COL

    Following
    start() {
    pid=$(tomcat_pid)
    if [ -n “$pid” ]
    then
    echo “Tomcat is already running (pid: $pid)”
    else
    # Start tomcat
    echo “Starting tomcat”
    ulimit -n 100000
    umask 007
    /bin/su -p -s /bin/sh $TOMCAT_USER $TOMCAT_HOME/bin/startup.sh
    fi

    Please tell me more about these 2 functions:
    ulimit -n 100000
    umask 007

    I have error using non root user (My root password is fine, but when I submit my root passwork Not work)
    $ service tomcat start
    Starting tomcat
    /etc/init.d/tomcat: line 44: ulimit: open files: cannot modify limit: Operation not permitted
    Password:
    /bin/su: incorrect password
    $

    Trying with root user
    # service tomcat start
    Starting tomcat
    Using CATALINA_BASE: /usr/local/tomcat
    Using CATALINA_HOME: /usr/local/tomcat
    Using CATALINA_TMPDIR: /usr/local/tomcat/temp
    Using JRE_HOME: /opt/jdk
    Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
    touch: cannot touch `/usr/local/tomcat/logs/catalina.out’: Permission denied
    /usr/local/tomcat/bin/catalina.sh: line 401: /usr/local/tomcat/logs/catalina.out: Permission denied
    [~]#

    Reply
    • admin

      Please check the permissions of the files and make sure that the tomcat8 user is their owner. Thanks.

      Reply
  4. Suresh

    I followed the above steps. When I am logged in using putty on the server, I am able to run GET http://localhost:8080 and see the html. I can also see that the tomcat8 process has started. However, when I access using IPAddress:8080 on the browser, I get the message This Page is Not Available. Any pointers will help. Thanks.

    Reply

Leave a Reply