Tuesday, November 29, 2016

Apache Tomcat 8 Installation and Configuration in CentOS 6.8


I had to work recently on a project where i got to install Java 1.8 and Apache Tomcat 8 in CentOS 6.8 x64 bit. Below are step by step instructions for full configuration of Apache Tomcat and adding it as init.d service. Hope you enjoy this tutorial.

1.Install Java 1.8
#cd /opt/
#wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-linux-x64.tar.gz"
#tar xzf jdk-8u111-linux-x64.tar.gz
# cd /opt/jdk1.8.0_111/
# alternatives --install /usr/bin/java java /opt/jdk1.8.0_111/bin/java 2
# alternatives --config java
This will display programs which provide 'java'. Below is example:
  Selection    Command
-----------------------------------------------
*  1           /opt/jdk1.7.0_71/bin/java
 + 2           /opt/jdk1.8.0_45/bin/java
    3           /opt/jdk1.8.0_91/bin/java
    4           /opt/jdk1.8.0_111/bin/java
Enter to keep the current selection[+], or type selection number: 4

# alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_111/bin/jar 2
# alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_111/bin/javac 2
# alternatives --set jar /opt/jdk1.8.0_111/bin/jar
# alternatives --set javac /opt/jdk1.8.0_111/bin/javac

Verify java version
[root@centos]# java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)


Configuring Environment Variables
Most of Java based application’s uses environment variables to work. Set the Java environment variables using following commands
•Setup JAVA_HOME Variable
# export JAVA_HOME=/opt/jdk1.8.0_111
•Setup JRE_HOME Variable
# export JRE_HOME=/opt/jdk1.8.0_111/jre
•Setup PATH Variable
# export PATH=$PATH:/opt/jdk1.8.0_111/bin:/opt/jdk1.8.0_111/jre/bin
#vi /etc/environment
JAVA_HOME=/opt/jdk1.8.0_111|
JRE_HOME=/opt/jdk1.8.0_111/jre
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/opt/jdk1.8.0_111/bin:/opt/jdk1.8.0_111/jre/bin


2. Tomcat 8 Intallation:
#cd /tmp
#wget http://mirror.ventraip.net.au/apache/tomcat/tomcat-8/v8.0.39/bin/apache-tomcat-8.0.39.tar.gz
#tar zxvf apache-tomcat-8.0.39.tar.gz
#ls –l
#mv apache-tomcat-8.0.39 /opt/
#cd /opt/apache-tomcat-8.0.39/bin
#./startup.sh  (to start tomcat 8)
#./shutdown.sh  (to stop tomcat 8)

#netstat -antp | grep 8080
tcp        0      0 :::8080                     :::*                        LISTEN      4180/java

Go to browser and type:
http://localhost:8080


You need to setup user login to access “Server status”, “Manager App” and “Host Manager”.
#vi /opt/apache-tomcat-8.0.39/conf/tomcat-users.xml
<user username="tomcat" password="P@ssw0rd" roles="manager-gui,manager-status,manager-script,admin-gui"/>

#cd /opt/apache-tomcat-8.0.39/bin
#./shutdown.sh
#./startup.sh

Now you can login with the user above to access.
(Note: you cannot specify “admin” user as it’ll be locked out due to security settings)

Adding tomcat as init.d service (In this case, there is no dedicated ‘tomcat’ user but it’s built under ‘root’)
Here, user ‘tomcatuser’ is allowed to start/stop/restart (in addition to root)

#vi /etc/init.d/tomcat
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
TOMCAT_HOME=/opt/apache-tomcat-8.0.39
TOMCAT_USER=tomcatuser
case $1 in
start)
/usr/bin/sudo $TOMCAT_HOME/bin/startup.sh
;;
stop)
/usr/bin/sudo $TOMCAT_HOME/bin/shutdown.sh
;;
restart)
/usr/bin/sudo $TOMCAT_HOME/bin/shutdown.sh
/usr/bin/sudo $TOMCAT_HOME/bin/startup.sh
;;
esac
exit 0

#chmod 755 tomcat
# chkconfig --add tomcat
# chkconfig --level 345 tomcat on
# chkconfig --list tomcat

To start tomcat during boot: add below
#vi /etc/rc.local
/etc/init.d/tomcat start

#service tomcat start/stop/restart

#vi /etc/sudoers
tomcatuser ALL = /sbin/service httpd24-httpd restart, /etc/init.d/httpd24-httpd restart, /bin/cat /var/log/httpd24/access_log, /bin/cat /var/log/httpd24/error_log, /etc/init.d/tomcat start, /etc/init.d/tomcat stop, /etc/init.d/tomcat restart, /bin/sh /opt/apache-tomcat-8.0.39/bin/shutdown.sh, /bin/sh /opt/apache-tomcat-8.0.39/bin/startup.sh

#Start as user ‘tomcatuser’
$sudo /etc/init.d/tomcat start/stop/restart

#Lastly reboot the server and check it’s loading at startup
#netstat –antp | grep 8080

#Also check PATH is loading correctly after restart
#echo $PATH





No comments:

Post a Comment