How to Install and Configure Tomcat 8 on Ubuntu 16.04

Table of Contents

Introduction

Tomcat is a widely used open source application and web server developed by the Apache Software Foundation. Tomcat is lightweight, agile, simple to use, and has a very large ecosystem of add-ons. Tomcat is used to serve Java applications and provides a Java HTTP web server environment in which Java code can run. The main goal of the Tomcat server is to provide a good web environment to run Java applications in comparison to other web servers.

This article will walk you through the installation and configuration of Tomcat 8 on a server running Ubuntu 16.04.

Requirements

  • A server running Ubuntu 16.04.
  • A non-root user with sudo privileges configured.

Installing Java

Before you begin you will need to update your apt-get package lists. You can update these with the following command:

sudo apt-get update

Java Development Kit (JDK) can be used to run Java web application, you can install Java Development Kit (JDK) package with the following command:

sudo apt-get install default-jdk

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

java -version

You should see the following output:

java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

Create Tomcat User

It is recommend that Tomcat run as an unprivileged user for security purposes. You will need to create a new user and group that will run the Tomcat service.

To create a tomcat group, run:

sudo groupadd tomcat

Next, create a new tomcat user and make this user member of the tomcat group with home directory /opt/tomcat:

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

Installing Tomcat

Once everything is up-to-date, download the latest version of Tomcat from the Tomcat 8 Downloads page.

Use the wget command to download the Tomcat source:

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

Extract the Tomcat source to your home directory:

sudo tar -xzvf apache-tomcat-8.5.5.tar.gz

Move the extracted content, apache-tomcat-8.5.5, to /opt:

sudo mv apache-tomcat-8.5.5 /opt/tomcat

Next you will need to give proper permission to the tomcat user to access to the Tomcat installation.

sudo chgrp -R tomcat /opt/tomcat
sudo chown -R tomcat /opt/tomcat
sudo chmod -R 755 /opt/tomcat

Create a systemd Service File

Now you will need to create a systemd service file to manage the Tomcat process so you can run Tomcat as a service.

Create the systemd service file, tomcat.service, inside the /etc/systemd/system/ directory.

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

Add the following content:

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

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-8-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=15
Restart=always

[Install]
WantedBy=multi-user.target

Save the file and reload the systemd daemon with the following command:

sudo systemctl daemon-reload

Now start the Tomcat service and check the status:

sudo systemctl start tomcat
sudo systemctl status tomcat

Output:

● tomcat.service - Apache Tomcat Web Server
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2016-10-28 15:32:57 IST; 5s ago
   Process: 10040 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
   Main PID: 10050 (java)
   CGroup: /system.slice/tomcat.service
       └─10050 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.log

Oct 28 15:32:57 Node1 systemd[1]: Starting Apache Tomcat Web Server...
Oct 28 15:32:57 Node1 systemd[1]: Started Apache Tomcat Web Server.

Configure the Tomcat service to start during boot:

sudo systemctl enable tomcat

Allow Tomcat Through the Firewall

By default Tomcat runs on port 8080, so you will need to allow this port through the firewall.

You can do this by running the following command:

sudo ufw allow 8080

Once you are finished, you can proceed to test Tomcat.

Testing Tomcat

It is time to test Tomcat. Open your web browser and type the URL http://your-server-ip:8080 and you should see the Tomcat default splash page:

Tomcat test page

Configure Tomcat Web Management Console

You will also need to add a set of login credentials to access the Manager App that comes with Tomcat.

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

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

Add a user who can access the manager-gui and admin-gui. To do this, add the following line above </tomcat-users>:

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

Save and close the file.

You will also need to edit context.xml file, if you want to access Tomcat from a remote machine:

To allow access to Manager App open the following file:

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

Add / Edit your remote machine IP address as shown below:

allow="192\.168\.1\.220|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

To allow access to Host Manager App open the following file:

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

Add / Edit your remote machine IP address as shown below:

allow="192\.168\.1\.220|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

Now save and close the file and restart the Tomcat service:

sudo systemctl restart tomcat

Access Tomcat Web Management Console

Once everything is configured, it is time to access the Tomcat web management console.

To access the Tomcat Manager App, open your web browser and type the URL http://your-server-ip/manager/html. There will be an authentication form that pops up. Input your username and the password that you already set up in tomcat-users.xml:

Tomcat Manager Login Page

Tomcat Manager Login

Tomcat Manager Home Page

Tomcat Manager Home

To access the Tomcat Host Manager App, open your web browser and type the URL http://your-server-ip/host-manager/html. There will be an authentication form pop up, input your username and password that you already setup in tomcat-users.xml:

Tomcat Host Manager Login Page

Tomcat Host Manager Login

Tomcat Host Manager Home Page

Tomcat Host Manager Home

Summary

Congratulations! You have successfully installed Apache Tomcat on your server. You can now deploy Java based applications on the server. Please visit the official Apache Tomcat website for additional help and useful information.

  • bonjour a tous, j'esaais d'installer apache tomcat sur ubuntu 17.04 et je rencontres les erreurs suivantes quand je demarres tomcat: ● tomcat.service - Apache Tomcat Web Server Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled) Active: activating (auto-restart) (Result: exit-code) since Mon 2017-07-17 08:30:17 CEST; 8s ago Process: 4601 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=203/EXEC)

    juil. 17 08:30:17 harvey-H34 systemd[1]: tomcat.service: Control process exited, code=exited status=203 juil. 17 08:30:17 harvey-H34 systemd[1]: Failed to start Apache Tomcat Web Server. juil. 17 08:30:17 harvey-H34 systemd[1]: tomcat.service: Unit entered failed state. juil. 17 08:30:17 harvey-H34 systemd[1]: tomcat.service: Failed with result 'exit-code'. svp pouvez vous m'aidez a finaliser cette installation vue que je suis pratiquement néophite sous linux. merci pour votre attention

  • Hi, Nice tutorial ,I have some errors ,sudo chown -R tomcat /opt/tomcat for this command gives invalid user error and for sudo systemctl start tomcat command getting sudo systemctl start tomcat Job for tomcat.service failed because the control process exited with error code. See "systemctl status tomcat.service" and "journalctl -xe" for details. uxovm@UXO:~$ ^C uxovm@UXO:~$ sudo systemctl status ● UXO State: degraded Jobs: 0 queued Failed: 1 units Since: Thu 2017-08-03 06:35:52 UTC; 5h 0min ago CGroup: / ├─init.scope │ └─1 /sbin/init ├─system.slice │ ├─avahi-daemon.service │ │ ├─20824 avahi-daemon: running [UXO.local │ │ └─20827 avahi-daemon: chroot helpe │ ├─mdadm.service │ │ └─1262 /sbin/mdadm --monitor --pid-file /run/mdadm/monitor.pid -- │ ├─walinuxagent.service │ │ ├─1179 /usr/bin/python3 -u /usr/sbin/waagent -daemon │ │ └─1371 python3 -u /usr/sbin/waagent -run-exthandlers │ ├─dbus.service │ │ └─1140 /usr/bin/dbus-daemon --system --address=systemd: --nofork │ ├─ModemManager.service │ │ └─23207 /usr/sbin/ModemManager │ ├─cron.service │ │ └─1196 /usr/sbin/cron -f

    uxovm@UXO:~$ systemctl status ● UXO State: degraded Jobs: 0 queued Failed: 1 units Since: Thu 2017-08-03 06:35:52 UTC; 5h 0min ago CGroup: / ├─init.scope │ └─1 /sbin/init ├─system.slice │ ├─avahi-daemon.service │ │ ├─20824 avahi-daemon: running [UXO.local │ │ └─20827 avahi-daemon: chroot helpe │ ├─mdadm.service │ │ └─1262 /sbin/mdadm --monitor --pid-file /run/mdadm/monitor.pid -- │ ├─walinuxagent.service │ │ ├─1179 /usr/bin/python3 -u /usr/sbin/waagent -daemon │ │ └─1371 python3 -u /usr/sbin/waagent -run-exthandlers │ ├─dbus.service │ │ └─1140 /usr/bin/dbus-daemon --system --address=systemd: --nofork │ ├─ModemManager.service │ │ └─23207 /usr/sbin/ModemManager │ ├─cron.service │ │ └─1196 /usr/sbin/cron -f │ ├─lvm2-lvmetad.service

    uxovm@UXO:~$ journalctl -xe -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit tomcat.service has begun starting up. Aug 03 11:42:24 UXO systemd[48985]: tomcat.service: Failed at step USER spawning -- Subject: Process /opt/tomcat/bin/startup.sh could not be executed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- The process /opt/tomcat/bin/startup.sh could not be executed and failed. -- -- The error number returned by this process is 3. Aug 03 11:42:24 UXO systemd[1]: tomcat.service: Control process exited, code=exi Aug 03 11:42:24 UXO systemd[1]: Failed to start Apache Tomcat Web Server. -- Subject: Unit tomcat.service has failed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit tomcat.service has failed. -- -- The result is failed. Aug 03 11:42:24 UXO systemd[1]: tomcat.service: Unit entered failed state. Aug 03 11:42:24 UXO systemd[1]: tomcat.service: Failed with result 'exit-code'.

    please help me,Thanks.

Log In, Add a Comment