How To Install Apache Tomcat 9.0 on CentOS 6 / RHEL 6

5

Apache Tomcat is an open source web server and servlet container developed by the Apache Software Foundation (ASF).

Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications from Oracle and provides a “pure Java” HTTP web server environment for running the Java codes.

Apache Tomcat includes tools for configuration and management, but can also be configured by editing XML configuration files.

Here is the step by step guide to install Apache Tomcat 9.0 on CentOS 6 / RHEL 6.

Install Java JDK

Open a terminal and switch to root user.

Tomcat requires to have java installed on your machine. You can either install Oracle JDK or OpenJDK on your machine.

Here I will use OpenJDK.

yum install -y java-1.8.0 wget

You can also verify Java by issuing the following command.

java -version

Output:

openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

Create Tomcat Service Account

For best practice, Tomcat should never be run as a privileged user (root). So, create a low-privilege user for running the Tomcat service.

groupadd tomcat
mkdir /opt/tomcat
useradd -g tomcat -d /opt/tomcat tomcat

Download & Setup Apache Tomcat

Download the latest version of the Apache Tomcat from the official website.

wget http://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.11/bin/apache-tomcat-9.0.11.tar.gz

Extract the tomcat on to your desired (/opt/tomcat) directory.

sudo tar -zxvf apache-tomcat-*.tar.gz
sudo mv apache-tomcat-*/* /opt/tomcat/
chown -R tomcat:tomcat /opt/tomcat/

Controlling Apache Tomcat

Apache Tomcat can be started and stopped manually by the script which comes with the package. But, here, we will use the init script to handle it.

vi /etc/init.d/tomcat9

Use the below information.

#!/bin/bash
#
# tomcat
#
# chkconfig: 345 96 30
# description:  Start up the Tomcat servlet engine.
#
# processname: java
# pidfile: /var/run/tomcat.pid
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: implementation for Servlet 2.5 and JSP 2.1
## END INIT INFO

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

## tomcat installation directory
PROCESS_NAME=java

CATALINA_HOME="/opt/tomcat/"

## run as a diffent user
TOMCAT_USER=tomcat

##  Path to the pid, runnning info file
pidfile=${PIDFILE-/var/run/${PROCESS_NAME}.pid};
lockfile=${LOCKFILE-/var/lock/subsys/${PROCESS_NAME}};

RETVAL=0

case "$1" in
 start)
        PID=`pidofproc -p ${pidfile} ${PROCESS_NAME}`
        if [[ (-n ${PID}) && ($PID -gt 0) ]]; then
                logger -s "${PROCESS_NAME}(pid ${PID}) is  already running."
                exit;
        fi
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
            logger -s "Starting Tomcat"
            /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/startup.sh -Dprocessname=${PROCESS_NAME}"
            PID=`ps -eaf|grep processname=${PROCESS_NAME}|grep -v grep|awk '{print $2}'`
            RETVAL=$?
            [ $RETVAL = 0 ] && touch ${lockfile}
            [ $RETVAL = 0 ] && echo "${PID}" > ${pidfile}
        fi
        ;;
 stop)
        PID=`pidofproc -p ${pidfile} ${PROCESS_NAME}`
        ## if PID valid run shutdown.sh
        if [[ -z ${PID} ]];then
            logger -s "${PROCESS_NAME} is not running."
            exit;
        fi

        if [[ (${PID} -gt 0) && (-f $CATALINA_HOME/bin/shutdown.sh) ]];
          then
            logger -s "Stopping Tomcat"
            /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/shutdown.sh"
            RETVAL=$?
            [ $RETVAL = 0 ] && rm -f ${lockfile}
            [ $RETVAL = 0 ] && rm -f ${pidfile}
        fi
        ;;
 status)
        status -p ${pidfile} ${PROCESS_NAME}
        RETVAL=$?
        ;;
 restart)
         $0 stop
         $0 start
         ;;
version)
        if [ -f $CATALINA_HOME/bin/version.sh ];
          then
            logger -s "Display Tomcat Version"
            /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/version.sh"
            RETVAL=$?
        fi
        ;;
 *)
         echo $"Usage: $0 {start|stop|restart|status|version}"
        exit 1
        ;;
esac
exit $RETVAL        

Credit: KwangSeob Jeong.

Set the script to be executable.

chmod +x /etc/init.d/tomcat9

Start the service.

service tomcat9 start

You can verify the service running, by default tomcat runs on port no 8080

netstat -antup | grep 8080

Output:

tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN      2476/java

Enable the Tomcat service to start automatically on system startup.

chkconfig --add tomcat9
chkconfig tomcat9 on

Managing the Apache Tomcat

Tomcat can be managed through the web-manager and virtual host manager. Both Web manager and Host manager are password protected and require username and password to access.

The user with manager-gui and admin-gui role have access to web application manager and host-manager respectively. These users and roles are defined in tomcat-users.xml.

vi /opt/tomcat/conf/tomcat-users.xml

Place the following two lines just above the last line.

rolename="admin-gui,manager-gui"/>
<user username="admin" password="password" roles="manager-gui,admin-gui"/>

For security reason, Web Manager and Host Manager are accessible only from the localhost, i.e., from the server itself.

To access web and host managers from the remote system, you would need to add your source network in the allow list. To do that, edit the below two files.

vi /opt/tomcat/webapps/manager/META-INF/context.xml

vi /opt/tomcat/webapps/host-manager/META-INF/context.xml

Update the below line on above files with source IP from which your accessing the web and host Manager. .* will allow everyone to have access to both managers.

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|.*" />

OR

You can also allow part of your network only. For example: To allow 10.10.0.0/24 network only, you can use the below values.

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|10.10.*" />

Restart the Tomcat service.

service tomcat9 restart

Firewall

Allow Tomcat web application requests through the firewall.

iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
/etc/init.d/iptables save

Access Apache Tomcat

Open the web browser and point to

http://ip.add.re.ss:8080

You would get the Tomcat default page.

Install Apache Tomcat 9.0 on CentOS 6 - Apache Tomcat Default Page
Install Apache Tomcat 9.0 on CentOS 6 – Apache Tomcat Default Page

Web Manager: – Login Required. Username: admin, Password: password

Install Apache Tomcat 9.0 on CentOS 6 - Login to Web Manager
Install Apache Tomcat 9.0 on CentOS 6 – Login to Web Manager

Here, you can deploy a new application, deploy a new application on specified context, list the active or inactive applications, start and stop the web applications.

Install Apache Tomcat 9.0 on CentOS 6 - Tomcat Web Application Manager
Install Apache Tomcat 9.0 on CentOS 6 – Tomcat Web Application Manager

Also, you can check the server status.

Install Apache Tomcat 9.0 on CentOS 6 - Tomcat Server Status
Install Apache Tomcat 9.0 on CentOS 6 – Tomcat Server Status

Host Manager: – Login Required. Username: admin, Password: password

Install Apache Tomcat 9.0 on CentOS 6 - Login to Tomcat Host Manager
Install Apache Tomcat 9.0 on CentOS 6 – Login to Tomcat Host Manager

Here, you can handle virtual hosts of Tomcat.

Install Apache Tomcat 9.0 on CentOS 6 - Tomcat Virtual Host Manager
Install Apache Tomcat 9.0 on CentOS 6 – Tomcat Virtual Host Manager

That’s All.

Further Reading

You might also like

How To Install Apache Tomcat 9.0 on CentOS 6 / RHEL 6

5