[Linux] Installing Tomcat 8 on a CentOS 7

UPDATE SYSTEM

First thing to do is to SSH to your CentOS 7 VPS, fire up a screen session and update your system using yum:

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

You may also want to install a text editor like nano or vim

## yum install vim nano

 

SETUP JAVA

Tomcat 8 requires JAVA 7+ in order to run. We are going to install the latest version of Oracle’s JAVA JDK 8. At the time of writing this article, the latest version of JAVA is 8u25 and can be downloaded and installed using the commands below:

DOWNLOAD JAVA

for 32-bit (x86) systems:

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

for 64-bit (x86_64) systems:

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

INSTALL JAVA

for 32-bit (x86) systems:

## yum install /opt/jdk-8-linux-i586.rpm

for 64-bit (x86_64) systems:

## yum install /opt/jdk-8-linux-x64.rpm

CONFIGURE JAVA

configure the JAVA package using the alternatives command:

## JDK_DIRS=($(ls -d /usr/java/jdk*))
## JDK_VER=${JDK_DIRS[@]:(-1)}

## alternatives --install /usr/bin/java java /usr/java/"${JDK_VER##*/}"/jre/bin/java 20000
## alternatives --install /usr/bin/jar jar /usr/java/"${JDK_VER##*/}"/bin/jar 20000
## alternatives --install /usr/bin/javac javac /usr/java/"${JDK_VER##*/}"/bin/javac 20000
## alternatives --install /usr/bin/javaws javaws /usr/java/"${JDK_VER##*/}"/jre/bin/javaws 20000
## alternatives --set java /usr/java/"${JDK_VER##*/}"/jre/bin/java
## alternatives --set javaws /usr/java/"${JDK_VER##*/}"/jre/bin/javaws
## alternatives --set javac /usr/java/"${JDK_VER##*/}"/bin/javac
## alternatives --set jar /usr/java/"${JDK_VER##*/}"/bin/jar

VERIFY JAVA

You may want to check if JAVA has been properly setup on your CentOS Linux VPS using:

## java -version

 

SETUP TOMCAT

TOMCAT USER

Before proceeding with the Tomcat installation, let’s first create a separate system user which will run the Tomcat server:

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

DOWNLOAD TOMCAT

Next, download the latest version of Tomcat 8 available at http://tomcat.apache.org/download-80.cgi . You can use wget to download it in /tmp, for example:

## wget http://mirror.tcpdiag.net/apache/tomcat/tomcat-8/v8.0.15/bin/apache-tomcat-8.0.15.tar.gz -P /tmp

INSTALL TOMCAT

Extract the contents of the Tomcat archive you just downloaded to /opt, create a symbolic link of tomcat directory to /opt/tomcat-latest and setup proper ownership using the following commands:

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

START TOMCAT

Create the following systemd unit file in /etc/systemd/system/tomcat8.service

[Unit]
Description=Tomcat8
After=network.target

[Service]
Type=forking
User=tomcat8
Group=tomcat8

Environment=CATALINA_PID=/opt/tomcat-latest/tomcat8.pid
Environment=TOMCAT_JAVA_HOME=/usr/java/default
Environment=CATALINA_HOME=/opt/tomcat-latest
Environment=CATALINA_BASE=/opt/tomcat-latest
Environment=CATALINA_OPTS=
Environment="JAVA_OPTS=-Dfile.encoding=UTF-8 -Dnet.sf.ehcache.skipUpdateCheck=true -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+UseParNewGC -XX:MaxPermSize=128m -Xms512m -Xmx512m"

ExecStart=/opt/tomcat-latest/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

[Install]
WantedBy=multi-user.target

With the unit file in place, run the following commands to start the Tomcat servce:

## systemctl daemon-reload
## systemctl restart tomcat8
## systemctl enable tomcat8

ACCESS TOMCAT