on Install  Tomcat 8 on CentOS 7

This tutorial covers the steps on how to install the latest Java JDK 8 and Tomcat 8 on CentOS 7 machine.

The instructions below are assuming you're logged in as root, if you are logged in as another user remember to prepend sudo to the commands shown below.

Install the necessary packages

First of all update your server and install the necessary packages using the following commands:

yum -y update  
yum -y install curl libcap-devel  
yum -y groupinstall "Development tools"  

Download/Install Java JDK 8

We'll download and install the latest Java JDK 8, from the Oracle webite. Download the rpm package according to your architecture:

# 64 Bit machines
cd /tmp && curl -LO 'http://download.oracle.com/otn-pub/java/jdk/8u25-b17/jdk-8u25-linux-x64.rpm' \
    -H 'Cookie: oraclelicense=accept-securebackup-cookie' 

# 32 Bit machines
cd /tmp && curl -LO 'http://download.oracle.com/otn-pub/java/jdk/8u25-b17/jdk-8u25-linux-i586.rpm' \
     -H 'Cookie: oraclelicense=accept-securebackup-cookie' 

and install it with the following command:

rpm -i /tmp/jdk-*.rpm  

Run the following commands to set the default Java version:

alternatives --install /usr/bin/java java /usr/java/jdk1.8.0_25/jre/bin/java 1
alternatives --install /usr/bin/jar jar /usr/java/jdk1.8.0_25/bin/jar 1
alternatives --install /usr/bin/javac javac /usr/java/jdk1.8.0_25/bin/javac 1
alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.8.0_25/jre/bin/javaws 1
alternatives --set java /usr/java/jdk1.8.0_25/jre/bin/java
alternatives --set javaws /usr/java/jdk1.8.0_25/jre/bin/javaws
alternatives --set javac /usr/java/jdk1.8.0_25/bin/javac
alternatives --set jar /usr/java/jdk1.8.0_25/bin/jar

Finally, to verify if the Java installation was successful, run:

java -version  

The output should be similar to what is displayed below

java version "1.8.0_25"  
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)  
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)  

Download/Install Tomcat 8

We'll install and run Tomcat as a daemon using the jsvc tool from the commons-daemon project.

To download the latest version of Tomcat 8 from apache.org run:

cd /tmp && curl -LO http://www.eu.apache.org/dist/tomcat/tomcat-8/v8.0.14/bin/apache-tomcat-8.0.14.tar.gz  

Extract the tomcat tar file to the /opt directory using:

mkdir -p /opt/tomcat  
tar -zxf /tmp/apache-tomcat-8.0.14.tar.gz --strip 1 -C /opt/tomcat  

Build Jsvc

Jsvc is a set of libraries and applications for making Java applications run on UNIX more easily and we'll use it to start/stop the Tomcat instance. The building process is pretty straightforward:

cd /opt/tomcat/bin  
tar xvfz commons-daemon-native.tar.gz  
cd commons-daemon-*-native-src/unix  
./configure --with-java=/usr/java/latest
make  
cp jsvc ../..  

Create tomcat user

The following commands will create a dedicated user who will run the Tomcat instance and change the ownership of all files in the /opt/tomcat directory to the newly created tomcat user.

useradd -r -s /sbin/nologin tomcat  
chown -R tomcat: /opt/tomcat  

Create a systemd service

Create a new file named tomcat.service:

vim /usr/lib/systemd/system/tomcat.service  

and add the following lines to it:

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking
PIDFile=/var/run/tomcat.pid
Environment=CATALINA_PID=/var/run/tomcat.pid
Environment=JAVA_HOME=/usr/java/default
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment=CATALINA_OPTS=

ExecStart=/opt/tomcat/bin/jsvc \
            -Dcatalina.home=${CATALINA_HOME} \
            -Dcatalina.base=${CATALINA_BASE} \
            -cp ${CATALINA_HOME}/bin/commons-daemon.jar:${CATALINA_HOME}/bin/bootstrap.jar:${CATALINA_HOME}/bin/tomcat-juli.jar \
            -user tomcat \
            -java-home ${JAVA_HOME} \
            -pidfile /var/run/tomcat.pid \
            -errfile SYSLOG \
            -outfile SYSLOG \
            $CATALINA_OPTS \
            org.apache.catalina.startup.Bootstrap

ExecStop=/opt/tomcat/bin/jsvc \
            -pidfile /var/run/tomcat.pid \
            -stop \
            org.apache.catalina.startup.Bootstrap

[Install]
WantedBy=multi-user.target

You can change the line with CATALINA_OPTS to suit your needs.

To enable the Tomcat service to start on boot run:

systemctl enable tomcat.service  

Now, you can use the following commands to start, stop, restart and check the status the Tomcat service:

systemctl start tomcat  
systemctl stop tomcat  
systemctl restart tomcat  
systemctl status tomcat  

Final steps

If you need to open port 8080 in the firewall, run:

firewall-cmd --zone=public --add-port=8080/tcp --permanent  
firewall-cmd --reload  

That's it, Tomcat installation is complete. To open Tomcat, point your browser at: http://<ip_address>:8080.