How to Install Tomcat on Linux

0
1409
How to Install Tomcat on Linux

The Apache Tomcat software is developed in an open and participatory environment and released under the Apache License version 2. The Apache Tomcat project is intended to be a collaboration of the best-of-breed developers from around the world. We invite you to participate in this open development project. To learn more about getting involved, click here or keep reading.

Apache Tomcat software powers numerous large-scale, mission-critical web applications across a diverse range of industries and organizations. Some of these users and their stories are listed on the PoweredBy wiki page.

Apache Tomcat, Tomcat, Apache, the Apache feather, and the Apache Tomcat project logo are trademarks of the Apache Software Foundation.

Components

Tomcat 4.x was released with Catalina (a servlet container), Coyote (an HTTP connector) and Jasper (a JSP engine).

Catalina

Catalina is Tomcat’s servlet container. Catalina implements Sun Microsystems‘s specifications for servlet and JavaServer Pages (JSP). In Tomcat, a Realm element represents a “database” of usernames, passwords, and roles (similar to Unix groups) assigned to those users. Different implementations of Realm allow Catalina to be integrated into environments where such authentication information is already being created and maintained, and then use that information to implement Container Managed Security as described in the Servlet Specification.

Coyote

Coyote is a Connector component for Tomcat that supports the HTTP 1.1 protocol as a web server. This allows Catalina, nominally a Java Servlet or JSP container, to also act as a plain web server that serves local files as HTTP documents.

Coyote listens for incoming connections to the server on a specific TCP port and forwards the request to the Tomcat Engine to process the request and send back a response to the requesting client. Another Coyote Connector, Coyote JK, listens similarly but instead forwards its requests to another web server, such as Apache, using the JK protocol. This usually offers better performance.

Jasper

Jasper is Tomcat’s JSP Engine. Jasper parses JSP files to compile them into Java code as servlets (that can be handled by Catalina). At runtime, Jasper detects changes to JSP files and recompiles them.

As of version 5, Tomcat uses Jasper 2, which is an implementation of the Sun Microsystems‘s JSP 2.0 specification. From Jasper to Jasper 2, important features were added:

  • JSP Tag library pooling – Each tag markup in JSP file is handled by a tag handler class. Tag handler class objects can be pooled and reused in the whole JSP servlet.
  • Background JSP compilation – While recompiling modified JSP Java code, the older version is still available for server requests. The older JSP servlet is deleted once the new JSP servlet has finished being recompiled.
  • Recompile JSP when included page changes – Pages can be inserted and included into a JSP at runtime. The JSP will not only be recompiled with JSP file changes but also with included page changes.
  • JDT Java compiler – Jasper 2 can use the Eclipse JDT (Java Development Tools) Java compiler instead of Ant and javac.

Three new components were added with the release of Tomcat 7:

Cluster

This component has been added to manage large applications. It is used for load balancing that can be achieved through many techniques. Clustering support currently requires the JDK version 1.5 or higher.

High availability

A high-availability feature has been added to facilitate the scheduling of system upgrades (e.g. new releases, change requests) without affecting the live environment. This is done by dispatching live traffic requests to a temporary server on a different port while the main server is upgraded on the main port. It is very useful in handling user requests on high-traffic web applications.

Web application

It has also added user- as well as system-based web applications enhancement to add support for deployment across the variety of environments. It also tries to manage sessions as well as applications across the network.

Tomcat is building additional components. A number of additional components may be used with Apache Tomcat. These components may be built by users should they need them or they can be downloaded from one of the mirrors.

Lets see how to install tomcat on Linux.

Step 1: Update your CentOS system

First things first, you need to update the system to the latest stable status:

# yum install epel-release
# yum update -y

Step 2: Install Java

You need to install Java SE 7.0 or later before Apache Tomcat can run properly. Here, I will install OpenJDK Runtime Environment 1.8.0 using YUM:

# yum install java-1.8.0-openjdk.x86_64

Now, you can confirm your installation with:

# java -version

openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

Step 3: Create a dedicated user for Apache Tomcat

For security purposes, you need to create a dedicated non-root user “tomcat” who belongs to the “tomcat” group:

# groupadd tomcat
# mkdir /opt/tomcat
# useradd -s /bin/nologin -g tomcat -d /opt/tomcat tomcat

Here, you created a user “tomcat” who belongs to the group “tomcat”. You cannot use this user account to log into the system. The home directory is /opt/tomcat, which is where the Apache Tomcat program will reside.

Step 4: Download and install the latest Apache Tomcat

You can always find the latest stable version of Apache Tomcat 8 from its official download page, which is 8.0.48 as of writing.

Under the “Binary Distributions” section and then the “Core” list, use the link pointing to the “tar.gz” archive to compose a wget command:

# wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.48/bin/apache-tomcat-8.0.48.tar.gz

# tar -zxvf apache-tomcat-8.0.48.tar.gz -C /opt/tomcat --strip-components=1

Step 5: Setup proper permissions

Before you can run Apache Tomcat, you need to setup proper permissions for several directories:

# cd /opt/tomcat
# chgrp -R tomcat conf
# chmod g+rwx conf
# chmod g+r conf/*
# chown -R tomcat logs/ temp/ webapps/ work/
# chgrp -R tomcat bin
# chgrp -R tomcat lib
# chmod g+rwx bin
# chmod g+r bin/*

Step 6: Setup a Systemd unit file for Apache Tomcat

As a matter of convenience, you should setup a Systemd unit file for Apache Tomcat:

# vi /etc/systemd/system/tomcat.service

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

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

User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target

Step 7: Start and test Apache Tomcat

Now, start the Apache Tomcat service and set it run on system boot:

# systemctl start tomcat.service
# systemctl enable tomcat.service

Then, you can test your installation of Apache Tomcat by visiting the following URL from a web browser:

http://[tomcat-server-ip]:8080

If all good, you will see the default Apache Tomcat front page.

Step 8: Configure the Apache Tomcat web management interface

In order to use the “Manager App” and the “Host manager” in the Apache Tomcat web interface, you need to create an admin user for your Apache Tomcat server:

# vim /opt/tomcat/conf/tomcat-users.xml

Within the </tomcat-users …>…</tomcat-users> segment, insert a line to define a admin user:

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

Remember to replace “username” and “password” with your own ones, the less common the better.

Restart Apache Tomcat to put your modifications into effect:

# systemctl restart tomcat.service

Refresh the Apache Tomcat front page from your web browser. Log in the “Manager App” and the “Host manager” using the credentials you had setup earlier.

To Access Manager URL:
http://Tomcat-server-ip:8080/manager

To check the status use:
http://Tomcat-server-ip:8080/manager/status/all?XML=true

The Apache Tomcat setup is complete. You can now use it to deploy your own applications.

NO COMMENTS