How to Install Tomcat

How to Install Tomcat

Tomcat is web server and servlet container which is used to run java applications, i will be showing the steps to install tomcat and java in an production system. i will be installing Tomcat 9 and OpenJDK 1.8 (Java 8). Most Steps are common for Redhat and Debian based systems.

Java 8 Installation

For Redhat based Distribution

yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64

For Debian and Ubuntu based Distribution

sudo apt install openjdk-8-jdk

Get the Java Installed Version

java -version
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)

We need to set JAVA_HOME environment variable so run and get the JAVA_HOME Path

sudo update-alternatives --config java

Edit file vim /etc/environment add JAVA_HOME environment variable

JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.111-1.b15.el7_2.x86_64/jre"

Logout and Login, run env, Check for JAVA_HOME in the output.

Tomcat 9 Installation

Create a tomcat group

groupadd tomcat

Create a tomcat user with /opt/tomcat as its home directory

mkdir -p /opt/tomcat
useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Change working directory and download the latest tomcat from tomcat website

cd /opt
wget http://mirrors.wuchna.com/apachemirror/tomcat/tomcat-9/v9.0.12/bin/apache-tomcat-9.0.12.tar.gz

Extract the Tomcat archive

tar -xzvf apache-tomcat-9.0.12.tar.gz

Move Contents to /opt/tomcat

mv apache-tomcat/* /opt/tomcat

Change ownership and user to tomcat

chown -R tomcat:tomcat /opt/tomcat

Create Systemd Service for Tomcat Service

vim /etc/systemd/system/tomcat.service

Reload systemd manager configuration.

systemctl daemon-reload

Start Tomcat Process.

systemctl start tomcat

Enable Tomcat to Start at System Boot.

systemctl enable tomcat

Run netstat -plntu and check port 8080 is in LISTEN State by a java program. Now open http://server-ip-addres:8080 you should see tomcat homepage

Configure Tomcat Manager Users

Restart Tomcat Process.

systemctl restart tomcat

Make sure all the Files in /opt/tomcat belong to Tomcat user and group, Run this Command whenever you new deploy WAR in /opt/tomcat/webapps

chown -R tomcat:tomcat /opt/tomcat