• Get In Touch
May 30, 2016

Install and Configure Tomcat 8 on Centos-7

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

Introduction

Apache Tomcat is an open-source web server and servlet container that is used to serve Java applications.

It is developed by the Apache Software Foundation, written in Java and released under Apache License 2.0.

It is a top level project of the Apache foundation. Apache Tomcat currently implements Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket.

Apache tomcat 8 has upgraded some features. Some of them are listed below:

  1. Tomcat 8 requires JAVA 7 or Higher to work.
  2. Tomcat 8 supports Java Servlet 3.1
  3. Tomcat 8 supports JavaServer Pages 2.3
  4. Tomcat 8 supports Java Unified Expression Language 3.0
  5. Tomcat 8 supports Java WebSocket 1.0

In this tutorial, we’ll learn how to install and configure latest release of Apache Tomcat 8 on CentOS 7 server.

Requirements

A server running CentOS v. 7.
A static IP Address for your server.
A non-root user account with sudo privilege set up on your server.

Installing Java

Before installing Tomcat, you will need to install Java Development Kit (JDK) on your system, so any Java web application code can be executed.

To install OpenJDK 7 JDK using yum, run this command:

sudo yum install java-1.7.0-openjdk-devel

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

java -version

The output should be similar to what is displayed below:

    java version "1.7.0_99"
    OpenJDK Runtime Environment (rhel-2.6.5.0.el7_2-x86_64 u99-b00)
    OpenJDK 64-Bit Server VM (build 24.95-b01, mixed mode)

Create Tomcat User

Before proceeding with the Tomcat installation. First create a separate system user and group which will run the Tomcat server:

First, create a new tomcat group:

sudo groupadd tomcat

Then create a new tomcat user with a home directory of /opt/tomcat and group tomcat by running the following command:

sudo useradd -M -s /bin/nologin -g tomcat -d /opt/tomcat tomcat

Download and Install Apache Tomcat

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

cd /tmp
sudo wget http://mirror.fibergrid.in/apache/tomcat/tomcat-8/v8.0.33/bin/apache-tomcat-8.0.33.tar.gz

Now, extract the contents of the Tomcat archive you just downloaded to /opt and rename apache-tomcat-8.0.33 to tomcat.
To do this, run the following command:

cd /opt
sudo tar -xvf /tmp/apache-tomcat-8.0.33.tar.gz
sudo mv apache-tomcat-8.0.33 tomcat

Next, setup proper ownership using the following commands:

sudo chown -R tomcat:tomcat /opt/tomcat

Create a systemd Service File

Now, you will need to create a systemd file to run Tomcat as a service.

You can create this file by running the following command:

sudo nano /etc/systemd/system/tomcat8.service

Add the following content:

    [Unit]
    Description=Apache Tomcat8
    After=syslog.target network.target

    [Service]

    Type=forking
    User=tomcat
    Group=tomcat

    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

    [Install]
    WantedBy=multi-user.target

Save and close the file then run the following commands to start the Tomcat service and enable Tomcat service to start on boot:

sudo systemctl daemon-reload
sudo systemctl start tomcat8
sudo systemctl enable tomcat8

Test Apache Tomcat

By default tomcat runs on port 8080, So you will need to open port 8080 in your firewall to access tomcat from network.

You can allow port 8080 through firewall by running the following command:

sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

After that, you can access apache tomcat by typing URL http://server-ip-address:8080. You should see the default Tomcat splash page as below:

Update Tomcat Port

Tomcat uses, by default, port number 8080 on your system. It is very important to rememeber that you would have a port number conflict if there is another service running on the same port on your system. So, to get around this you will need to change the tomcat port from 8080 to something else.

You can change the port number for your tomcat server by changing in the configuration file.

You can do this by editing server.xml file located under /opt/tomcat/conf directory.

sudo nano /opt/tomcat/conf/server.xml

Change port number from 8080 to 8081 as below:

    <!-- A "Connector" represents an endpoint by which requests are received
             and responses are returned. Documentation at :
             Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
             Java AJP  Connector: /docs/config/ajp.html
             APR (HTTP/AJP) Connector: /docs/apr.html
             Define a non-SSL/TLS HTTP/1.1 Connector on port 8081
        -->

        <!-- A "Connector" using the shared thread pool-->

Save and exit the file and restart Tomcat8 service.

sudo systemctl restart tomcat8.service

Next, open port 8081 in firewall to access tomcat from network.

You can allow port 8081 through firewall by running the following command:

sudo firewall-cmd --permanent --add-port=8081/tcp
sudo firewall-cmd --reload

Configure Apache Tomcat

By default, you can not access admin and other sections like Server Status, Manager App and Host Manager.

To access all these sections, you will need to add user accounts for admins and managers.

You can do this by editing tomcat-users.xml file:

sudo nano /opt/tomcat/conf/tomcat-users.xml

Find the section “ and add the following lines before that:


Save and close the file, restart tomcat service to take new changes into effect.

sudo systemctl restart tomcat8.service

Finally, Tomcat is setup and running. You can access the admin and other sections by typing URL http://server-ip-address:8081 in your web browser:

After clicking Manager App, you will be asked to enter the username and password, you just created above, after login you will see below interface:

Tomcat Web Application Manager

You can manage your Java applications using the Tomcat Web Application Manager. It is also used to Start, Stop, Reload, Deploy, and Undeploy. You can also diagnostics on your apps using Tomcat Web Application Manager.

Server Status

Tomcat Host Manager

Conclusion

I hope you now have enough knowledge to install and configure Tomcat 8 on your server.

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

Share this Article!

Related Posts

Node.js Authentication – A Complete Guide with Passport and JWT

Node.js Authentication – A Complete Guide with Passport and JWT

Truth be told, it’s difficult for a web application that doesn’t have some kind of identification, even if you don’t see it as a security measure in and of itself. The Internet is a kind of lawless land, and even on free services like Google’s, authentication ensures that abuses will be avoided or at least […]

Node.js and MongoDB: How to Connect MongoDB With Node

Node.js and MongoDB: How to Connect MongoDB With Node

MongoDB is a document-oriented NoSQL database, which was born in 2007 in California as a service to be used within a larger project, but which soon became an independent and open-source product. It stores documents in JSON, a format based on JavaScript and simpler than XML, but still with good expressiveness. It is the dominant […]

Using MySQL with Node.js: A Complete Tutorial

Using MySQL with Node.js: A Complete Tutorial

Although data persistence is almost always a fundamental element of applications, Node.js has no native integration with databases. Everything is delegated to third-party libraries to be included manually, in addition to the standard APIs. Although MongoDB and other non-relational databases are the most common choice with Node because if you need to scale an application, […]

Node.Js Vs Django: Which Is the Best for Your Project

Node.Js Vs Django: Which Is the Best for Your Project

Django and NodeJs are two powerful technologies for web development, both have great functionality, versatile applications, and a great user interface. Both are open source and can be used for free. But which one fits your project best? NodeJs is based on JavaScript, while Django is written in Python. These are two equally popular technologies […]

Nodejs Vs PHP:  Which Works Best?

Nodejs Vs PHP: Which Works Best?

Before getting into the “battle” between Node.js and PHP we need to understand why the issue is still ongoing. It all started with the increased demand for smartphone applications, their success forcing developers to adapt to new back-end technologies that could handle a multitude of simultaneous requests. JavaScript has always been identified as a client-side […]