Linux, Windows, Virtualization, OpenSource & Blogging

Install Apache Tomcat 8.5 on CentOS 7 / RHEL 7

3

Apache Tomcat is an open source web server and servlet container developed by the Apache Software Foundation (ASF). Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications from Oracle and provides a “pure Java” HTTP web server environment for running the Java codes.

Apache Tomcat includes tools for configuration and management, but it can also be configured by editing XML configuration files.

Here is the step by step guide to install Apache Tomcat 8.5 on RHEL 7 / CentOS 7.

Requirement

Open a terminal and switch to root user.

su -

Tomcat requires having java installed on your machine. You can either have Oracle JDK or OpenJDK.

READ: How to Install Oracle JAVA 8 on CentOS 7 / RHEL 7

For this post, I am using OpenJDK.

yum install -y java-1.8.0 wget

You can also verify it, by issuing the following command.

java -version

Output:

oopenjdk version "1.8.0_141"
OpenJDK Runtime Environment (build 1.8.0_141-b16)
OpenJDK 64-Bit Server VM (build 25.141-b16, mixed mode)

Create Tomcat Service Account

For best practice, Tomcat should not be run as root user. So, create a normal user for running the Tomcat service.

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

Download & Setup Apache Tomcat

Download the latest version of the Apache Tomcat from the website and save it to your home directory.

wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz

Download the MD5 file.

wget --no-check-certificate https://www.apache.org/dist/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz.md5

Verify the md5 check sum of the downloaded Tomcat archive with check sum provided by Apache Tomcat.

# cat apache-tomcat-8.5.20.tar.gz.md5
6cdfbb0c75e1c0beb4dc7603e6a44ec7 *apache-tomcat-8.5.20.tar.gz

# md5sum apache-tomcat-8.5.20.tar.gz
6cdfbb0c75e1c0beb4dc7603e6a44ec7 apache-tomcat-8.5.20.tar.gz

If both matches then extract the Tomcat archive and move it to your desired (/opt/tomcat) directory.

tar -zxvf apache-tomcat-*.tar.gz
mv apache-tomcat-8.5.20/* /opt/tomcat/

Change the ownership of the directory so that tomcat user can write files in it.

chown -R tomcat:tomcat /opt/tomcat/

Controlling Apache Tomcat

Manual

Apache Tomcat can be started and stopped by the script which comes with the package. Start the Apache Tomcat.

cd /opt/tomcat/bin/
sh startup.sh

You will get the following output.

Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Tomcat started.

Stop the Apache Tomcat.

cd /opt/tomcat/bin/
sh shutdown.sh

Systemd

We can also configure systemd to start the Tomcat service. Skip the below step in case you do not want to use systemd for managing Tomcat service.

Create a tomcat systemd service file. Green ones depend on the environment, so change them accordingly.

vi /etc/systemd/system/tomcat.service

Add below information to Tomcat systemd service file.

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

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-1.b16.el7_3.x86_64/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1G -Djava.net.preferIPv4Stack=true'
Environment='JAVA_OPTS=-Djava.awt.headless=true'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
SuccessExitStatus=143

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Reload systemd daemon.

systemctl daemon-reload

To start the Tomcat service; run:

systemctl start tomcat

Check the status of Tomcat, run:

systemctl status tomcat

Enable the auto start of Tomcat service on system start, run:

systemctl enable tomcat

Verify Apache Tomcat

By default, Tomcat runs on port no 8080. Use netstat command to check whether the service is listening on port 8080 or not.

netstat -antup | grep 8080

Output:

tcp6       0      0 :::8080                 :::*                    LISTEN      2428/java

Firewall

You may need to allow port 8080 in the firewall so that we can access Tomcat from external networks.

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

Configure Apache Tomcat Web UI

Tomcat can be managed through the Web Manager. With Web Manager, we can

  • Deploy new application, deploy
  • Deploy new application on specified context
  • List the active or in active applications
  • Start and stop the web applications

Also, Tomcat has Host Manager to manage its virtual hosts.

Both The Web and Host Manager is password protected, requires user name and password to access. Only the user with the “manager-gui” and “admin-gui” role is allowed to access.

These users and roles are defined in tomcat-users.xml.

vi /opt/tomcat/conf/tomcat-users.xml

Place the following two lines just above the last line.

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

For security reason, Web and Host Manager is accessible only from localhost, i.e., from the server itself.

If you want to access Web and Host manager from remote systems, then you need to add your source network in allow list. To do that, edit the below two files.

nano /opt/tomcat/webapps/manager/META-INF/context.xml

nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

Update the below line on both files with source IP from which you’re accessing the Web and Host Manager. .* will allow everyone to have access to Web and Host manager.

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|.*" />

Restart the Tomcat.

systemctl restart tomcat

Access Tomcat

Open the web browser and point to

http://ipaddress:8080

You should now get the Tomcat’s default page.

Install Apache Tomcat 8.5 on CentOS 7 - Apache Tomcat Default Page
Install Apache Tomcat 8.5 on CentOS 7 – Apache Tomcat Default Page

Web Manager: – Login Required. Username: admin, Password: tomcat

Install Apache Tomcat 8.5 on CentOS 7 - Apache Tomcat Web Manager Login
Install Apache Tomcat 8.5 on CentOS 7 – Apache Tomcat Web Manager Login

Here, you can deploy a new application, deploy an application on specified context, start, stop, reload, and un-deploy an application.

Install Apache Tomcat 8.5 on CentOS 7 - Apache Tomcat Web Manager
Install Apache Tomcat 8.5 on CentOS 7 – Apache Tomcat Web Manager

Also, you can check the Tomcat server status.

Install Apache Tomcat 8.5 on CentOS 7 - Apache Tomcat Server Status
Install Apache Tomcat 8.5 on CentOS 7 – Apache Tomcat Server Status

Host Manager: – Login Required. Username: admin, Password: tomcat

Here, you can manage virtual hosts of Tomcat.

Install Apache Tomcat 8.5 on CentOS 7 - Apache Tomcat Host Manager
Install Apache Tomcat 8.5 on CentOS 7 – Apache Tomcat Host Manager

That’s All.

POSTS YOU MAY LIKE -:)

You might also like
  • Thanks for the tutorial

    • Raj

      Welcome

  • Roberto Aleggri

    Thanks for the tutorial.

Install Apache Tomcat 8.5 on CentOS 7 / RHEL 7

3