Rabu, 30 Januari 2019

Install Tomcat 8 on a CentOS 6


In the following tutorial you will learn how to install and set-up Apache Tomcat 8 on your CentOS 6

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 your CentOS 6 based Linux Server is up-to-date by running:

# yum update


INSTALL JAVA 8


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

# wget --no-cookies \
--no-check-certificate \
--header "Cookie: oraclelicense=accept-securebackup-cookie" \
"https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.rpm" \
-O /tmp/jdk-8u201-linux-x64.rpm

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

# rpm -Uvh /tmp/jdk-8u201-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_201-amd64/jre/bin/java 20000
# alternatives --install /usr/bin/jar jar /usr/java/jdk1.8.0_201-amd64/bin/jar 20000
# alternatives --install /usr/bin/javac javac /usr/java/jdk1.8.0_201-amd64/bin/javac 20000
# alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.8.0_201-amd64/jre/bin/javaws 20000
# alternatives --set java /usr/java/jdk1.8.0_201-amd64/jre/bin/java
# alternatives --set javaws /usr/java/jdk1.8.0_201-amd64/jre/bin/javaws
# alternatives --set javac /usr/java/jdk1.8.0_201-amd64/bin/javac
# alternatives --set jar /usr/java/jdk1.8.0_201-amd64/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 tomcat8

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

# wget https://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.37/bin/apache-tomcat-8.5.37.tar.gz -P /tmp

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

# tar zxf /tmp/apache-tomcat-8.5.37.tar.gz -C /opt

make symbolic link

# ln -s /opt/apache-tomcat-8.5.37 /opt/tomcat8

change permission

# chown -hR tomcat8: /opt/tomcat8 /opt/apache-tomcat-8.5.37
# cd /opt/tomcat8/bin
# chmod +x *.sh


START THE TOMCAT 8 SERVICE

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

#!/bin/sh
### BEGIN INIT INFO
# chkconfig: 345 84 16
# description: Tomcat Jakarta JSP Server
# Provides: Tomcat
### END INIT INFO

JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export JAVA_HOME

## IF NEEDED 
#JAVA_OPTS="-Dfile.encoding=UTF-8 \
#  -Dnet.sf.ehcache.skipUpdateCheck=true \
#  -XX:+UseConcMarkSweepGC \
#  -XX:+CMSClassUnloadingEnabled \
#  -XX:+UseParNewGC \
#  -XX:MaxPermSize=128m \
#  -Xms512m -Xmx512m"
# export JAVA_OPTS

PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/opt/tomcat8

STARTSCRIPT="/opt/tomcat8/bin/startup.sh"
STOPSCRIPT="/opt/tomcat8/bin/shutdown.sh"
RUNAS=tomcat8

PIDFILE=/opt/tomcat8/tomcat8.pid
LOGFILE=/opt/tomcat8/tomcat8.log

start() {

  TOMCATLIVE=$(pgrep -u tomcat8 -f bootstrap.jar | wc -l)
  if [ "$TOMCATLIVE" -eq 0 ]; then
    echo 'Starting Jakarta Tomact service …' >&2
    local CMD="$STARTSCRIPT &> \"$LOGFILE\" & echo \$!"
    su -l $RUNAS -c "$CMD" > "$PIDFILE"
  sleep 2
  PID=$(cat $PIDFILE)

    if pgrep -u $RUNAS -f bootstrap.jar > /dev/null
    then
      echo "Jakarta Tomcat is now running, the PID is $PID"
    else
      echo ''
      echo "Error! Could not start Jakarta Tomcat!"
    fi

  else
    echo 'Service Jakarta Tomcat all ready running' >&2
    return 1
  fi
  
}

stop() {

  TOMCATLIVE=$(pgrep -u tomcat8 -f bootstrap.jar | wc -l)
  if [ "$TOMCATLIVE" -eq 0 ]; then
    echo 'Service Jakarta Tomcat not running' >&2
    return 1
  fi
  echo 'Stopping Jakarta Tomcat service …' >&2
  su -l $RUNAS -c $STOPSCRIPT
  rm -f "$PIDFILE"
  echo 'Service Jakarta Tomcat stopped' >&2

}

uninstall() {
  echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
  local SURE
  read SURE
  if [ "$SURE" = "yes" ]; then
    stop
    rm -f "$PIDFILE"
    echo "Notice: log file was not removed: $LOGFILE" >&2
    update-rc.d -f tomcat8 remove
    rm -fv "$0"
  fi
}

status() {

TOMCATLIVE=$(pgrep -u tomcat8 -f bootstrap.jar | wc -l)
TOMCATPIDLIVE=$(pgrep -u tomcat8 -f bootstrap.jar )
if [ "$TOMCATLIVE" -eq 0 ]; then
  echo -n "Jakarta Tomcat Stopped"
  echo
else
  echo "Jakarta Tomcat Running, the PID is $TOMCATPIDLIVE "
fi

}


case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  uninstall)
    uninstall
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|uninstall}"
esac

make the script executable using chmod

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

Add the Tomcat 8 service to system startup:

# chkconfig --add tomcat8
# chkconfig tomcat8 on

Start the Tomcat 8 server using:

# service tomcat8 start

Configuring Firewall

# iptables -I INPUT -p tcp --destination-port 8080 -j ACCEPT
# service iptables save
# ip6tables -I INPUT -p tcp --destination-port 8080 -j ACCEPT
# service ip6tables save

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

Tidak ada komentar: