Install Tomcat in Ubuntu 16.04 [5 Easy Steps] – Beginner Tutorial

Introduction

Here in his tutorial, let us see how to install Tomcat in Ubuntu 16.04 server.

What is Apache Tomcat?

Apache Tomcat is a web server developed by Apache foundation. This is also open source web server and Servlet container.

The Tomcat supports java EE specifications.

Requirements

Install Java on Ubuntu 16.04

We are not going very deep in installing java in this tutorial. You can check this install Java Ubuntu detailed tutorial.

Follow this simple commands to install Java on Ubuntu 16.04 server.

First, update the server repository index.

$ sudo apt-get update

Then, install java using this simple command.

$ sudo apt-get install default-jdk

After that, we have to create a Tom Cat user to run tomcat service.

Install Tomcat on Ubuntu

To install the latest version of Tomcat on Ubuntu server, we have to download the Latest release from the tomcat page itself.

Now, switch to tmp directory and download the file. The tmp directory is used to download the zipped file and we will remove the zip file after installing the extracted software.

$ cd /tmp

Then, use the curl command to download the file from tomcat website.

$ curl -O http://apache.mirrors.ionfish.org/tomcat/tomcat-8/v8.5.5/bin/apache-tomcat-8.5.5.tar.gz

Tomcat will be installed on /opt/tomcat location. First create the file and extract the file to the /opt/tomcat using the below command.

$ sudo mkdir /opt/tomcat
$ sudo tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1

After that, we have to setup the permission for the Tomcat

Permission Configuration

Move to the tomcat folder using the below command.

$ cd /opt/tomcat

Now, it’s time to give group ownership to the tomcat for the file so that it can read and write.

$ sudo chgrp -R tomcat /opt/tomcat

To enable read access, use the below command.

$ sudo chmod -R g+r conf
$ sudo chmod g+x conf

Then, you have to make the tomcat owner for the work, temp, logs and webapps directories.

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

Thats all. You have setup all the required permission for the tomcat. Now, let us create the Systemd file for tomcat.

Systemd File Creation for Tomcat

In order for the tomcat to run as a service, we need to create the systemd file.

First, we have to find the Java installation location. The Java installation location is called as “Java_Home”. 

To find Java Home, use the below command.

$ sudo update-java-alternatives -l

You will get output similar to this.

$ java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64

For your server, the java home might be different. Make sure to run the above command to fine the location.

After that, Open the Tomcat Systemd file using the below command.

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

The file will open and you have to make changes in the Java Home Value to match up with your system java installation location.

/etc/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat Web Application Container
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_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

After that, save and close the file.

$ sudo systemctl daemon-reload

Then, start the tomcat service using the below command.

$ sudo systemctl start tomcat

Now, you can ensure the status of the service by executing the below command.

$ sudo systemctl status tomcat

Tomcat Web Management Interface

Tomcat has the manager web app. We have to create login access to use the web app. To do that, follow the below step. First, we will edit tomcat-users.xml file.

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

After that, we have to create user which can access the manager-gui and admin gui. You can do that by using below configuration.

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

Then, save and close the file.

The current version of Tomcat, restrict the access to the manager and host manager app from the local system itself.

remember, we need to access tomcat manager from remote server. We will remove the restriction by Configuring the IP connection.

We will do this by making changes in context.xml file.

You can open manage app xml file using the below command.

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

Then, you can open the host manager app xml file using the below command.

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

Now, you have to comment out the IP address restriction using the below command.

context.xml files for Tomcat webapps
<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>

The above changes will let you access tomcat manager from anywhere.

Now, save and close the file. Then, restart the tomcat using the below command.

$ sudo systemctl restart tomcat

Testing the Tomcat Installation

Now, it’s time to test what we have done.

To access the Apache web management interface, visit the below address in browser

http://domain_or_IP:8080

To look Tomcat manage app, visit here.

http://domain_or_IP:8080/manager/html

To check the host manage app, visit the below link

http://domain_or_IP:8080/host-manager/html/

Conclusion

If you can see the concerned pages, then, you have installed the Tomcat successfully.

If you have any questions, let us know that in command. Also, make sure to subscribe for the upcoming tutorials. Learn cloud with Serverclear.com

LIKE THIS ARTICLE?

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Leave a Comment