Linux, Windows, Virtualization, OpenSource & Blogging

How to install Tomcat 8.5 on Debian 9 / Ubuntu 16.04 / Linux Mint 18

0

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

In Tomcat, you can deploy any application by using a war file through the Web Manager Application.

Here is the guide to install Apache Tomcat 8.5 on Debian 9 / Ubuntu 16.04.

Requirement

First switch to the root user.

su -

OR

sudo su -

Tomcat requires Java JDK to be installed on the machine. You can either install Oracle JDK or OpenJDK.

READ: How to Install Oracle JAVA 8 on Debian 9
READ: How to Install Oracle JAVA 8 on Ubuntu 16.04 / LinuxMint 18

For this demo, I am going with OpenJDK.

apt-get -y install openjdk-8-jdk

Once Java is installed, you can verify the Java version by using the following command.

java -version

Output:

openjdk version "1.8.0_141"
OpenJDK Runtime Environment (build 1.8.0_141-8u141-b15-1~deb9u1-b15)
OpenJDK 64-Bit Server VM (build 25.141-b15, mixed mode

Create Tomcat Service Account

For best practice, Tomcat should never be run as privileged user (root). So, create a low-privilege user for running the Tomcat service.

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

Download & Configure Apache Tomcat

You can download the latest version of the Apache Tomcat from the official website.

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 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 the both value matches then extract the tomcat on 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 extracted directory so that tomcat user can write files in it.

chown -R tomcat:tomcat /opt/tomcat/

Controlling Apache Tomcat

Manual

You can start and stop the Tomcat using the script which comes along with the package.

To start Tomcat service, go to the Tomcat directory and run:

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

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.

To stop Tomcat service, run:

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 are depends on the environment, so change them accordingly.

nano /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-amd64/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 boot:

systemctl enable tomcat

Verify Apache Tomcat

By default, Tomcat runs on port 8080. Use can use the netstat command to check the port status.

netstat -antup | grep 8080

Output:

tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      12224/java

READ: netstat command not found – Quick Fix

Firewall

You may need to allow Tomcat server requests in the firewall so that we can access the application from the external network.

ufw allow 8080

Configure Apache Tomcat Web UI

Tomcat comes with the web-manager and Host Manager for managing Tomcat. Both Host Manager and Web Manager are password protected, and it requires a user name and password to access.

Only the user with the manager-gui and admin-gui role is allowed to access web manager and host manager respectively. Those two roles are defined in tomcat-users.xml file.

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

Place the following two lines (role and user definition) just above the last line.

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

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

If you want to access managers from the remote system 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 your accessing the Web and Host Manager. .* will allow everyone to have access to managers.

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

Restart the Tomcat service.

systemctl restart tomcat

Access Tomcat

Open the web browser and point it to,

http://ipaddress:8080

You would get the Tomcat default page; this confirms that Tomcat is successfully installed.

Install Tomcat 8.5 on Debian 9 - Tomcat 8 default page
Install Tomcat 8.5 on Debian 9 – Tomcat 8 default page

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

Install Tomcat 8.5 on Debian 9 - Login for web manager
Install Tomcat 8.5 on Debian 9 – Login for web manager

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

Install Tomcat 8.5 on Debian 9 - Tomcat Web Manager
Install Tomcat 8.5 on Debian 9 – Tomcat Web Manager

Also, you can check the server status.

Install Tomcat 8.5 on Debian 9 - Tomcat Server Status
Install Tomcat 8.5 on Debian 9 – Tomcat Server Status

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

Here, you can manage virtual hosts of Tomcat.

Install Tomcat 8.5 on Debian 9 - Tomcat Host Manager
Install Tomcat 8.5 on Debian 9 – Tomcat Host Manager

That’s All.

POSTS YOU MAY LIKE -:)

You might also like

How to install Tomcat 8.5 on Debian 9 / Ubuntu 16.04 / Linux Mint 18

0