How to install Tomcat 8 on Ubuntu Server 16.04

Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, released by the Apache Software Foundation. This tutorial covers the basic installation and some configuration of the latest release of Tomcat 8 on your Ubuntu 16.04 server.

 First step will be to install apt-get-repository by below command

~$ sudo apt-get install software-properties-common

 let us update
~$ sudo apt-get update
The second step is about installing Java. If you want to install Oracle Java then execute the below commands
~$ sudo apt-add-repository ppa:webupd8team/java
~$ sudo apt-get update
~$ sudo apt-get install oracle-java8-installer

Setting as default JDK

~$ sudo apt-get install oracle-java8-set-default

Setting the path globally:

type :

~$ sudo nano /etc/bash.bashrc

and add this line at the end of page.

export JAVA_HOME=/usr/lib/jvm/java-8-oracle

loading bash.bash.bashrc

~$ source /etc/bash.bashrc

The third step is creating tomcat group and user.

~$ sudo groupadd tomcat

~$ sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Forth Step is about installing Tomcat

Go to  https://tomcat.apache.org/ click the version you want to download

Then click the mirror if you want to change the download server and click on tar.gz then copy the link address. Here it is http://ftp.itu.edu.tr/Mirror/Apache/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz

Run the below command

~$  wget http://ftp.itu.edu.tr/Mirror/Apache/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz

~$ sudo mkdir /opt/tomcat

~$ sudo tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat

The Fifth step is updating the permission

~$ cd /opt/tomcat

~$ sudo chgrp -R tomcat /opt/tomcat

Next, give the tomcat group read access to the conf directory and all of its contents, and execute access to the directory itself:

~$ sudo chmod -R g+r  conf

~$ sudo chmod g+x  conf

Make the tomcat user the owner of the webapps, work, temp and logs directories:

~$ sudo chown -R tomcat  webapps/ work/ temp/ logs/

The sixth step is about creating a systemd service file

Run the below command first to know where Java is installed

~$ sudo update-java-alternatives -l

It will show something like this

java-8-oracle 1081 /usr/lib/jvm/java-8-oracle

So the java is installed under /usr/lib/jvm/java-8-oracle

~$ echo $JAVA_HOME

If these both are not same then you need to command

~$ sudo nano /etc/bash.bashrc

and add you need to add

export JAVA_HOME=/usr/lib/jvm/java-8-oracle

Let us create the systemd service file

~$ sudo nano /etc/systemd/system/tomcat.service

and let us add

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

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-8-oracle/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=/opt/tomcat/bin/shutdown.sh

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

[Install]
WantedBy=multi-user.target

Save and exit

Reload the systemd daemon:

~$ sudo systemctl daemon-reload

Start the Tomcat service :

~$ sudo systemctl start tomcat

Let’s enable the service on boot

~$ sudo systemctl enable tomcat

Step Seven:

If firewall is enabled the allow traffic to 8080 port which tomcat server use by typing:

~$ sudo ufw allow 8080

Let us test the Tomcat server on a browser:

http:// [server_name_or_IP] : 8080

Step Eight:

Configure tomcat web management interface

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

Now let us add this

<?xml version='1.0' encoding='utf-8'?>

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

Generate a username which it is admin here and generate a proper password

We also need to configure context.xml file under

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

In both file it should be

<Context antiResourceLocking="false" privileged="true" >
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
</Context>

Restart the service by typing:

~$ sudo systemctl start tomcat

Congradulation for the installation of Apache Tomcat!

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.