How To Install Tomcat 9 on Ubuntu 18

In this tutorial, we’ll explain how to install tomcat 9 on Ubuntu 18. Tomcat is an Open Source container (Java servlet) which is developed by the Apache Software Foundation.

Tomcat provides a “pure Java“ HTTP web server environment for running Java codes. Tomcat also implements Java servlet, JavaServer Pages (JSP), Java EL ad WebSocket.

How To Install Tomcat 9 on Ubuntu 18

Pre-requisites:

Update the Ubuntu 18 repository index

$ sudo apt update

Tomcat requires Java JDK to run, so it should be installed on the machine. Either you can install Oracle JDK or OpenJDK.

Check the Java Version on the machine and if it is not installed then you can install it with the following steps.

$ java - version

Apache Tomcat 9 version support Java and later version

Steps to install Java Version 9 on Ubuntu 18 / Linux

In this tutorial, we will show you how to install OpenJDK.

### OpenJDK 8 ###

$ sudo apt install -y openjdk-8-jdk wget

### OpenJDK 10 ###

$ sudo apt install -y default-jdk wget

Once Java package is installed on Ubuntu verify he Java version.

$ java -version

Output:

openjdk version "1.8.0_181"

OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.18.04.1-b13)

OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

Now create tomcat user on Ubuntu it is best practice to run tomcat service with a non-root user.

Steps to create Tomcat Service user Account

As per best practice, you should NOT run Tomcat as a privileged user (root). So, create a user with low-privilege for running the Tomcat service.

$ sudo groupadd tomcat

$ sudo mkdir /opt/tomcat

$ sudo useradd -g tomcat -d /opt/tomcat -s /bin/nologin tomcat

Steps to download & configure Apache Tomcat for Ubuntu 18

The browser and Download Apache Tomcat 9

Terminal

### Apache Tomcat 9.0 ###

$ cd /opt/tomcat

$ wget http://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.11/bin/apache-tomcat-9.0.11.tar.gz

Extract download tomcat 9

$ sudo tar -zxvf apache-tomcat-*.tar.gz

Change the ownership of the extracted tomcat directory and allow tomcat user to write files to it.

$ sudo chown -R tomcat:tomcat /opt/tomcat/

$ sudo update-java-alternatives -l


#Output:
java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64

The above output shows Java 1.8 version is installed on the machine.

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

Add the below details to the tomcat system service file.

[Unit]

Description=Apache Tomcat 9.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

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.
$ sudo systemctl daemon-reload
Command to started tomcat service in Ubuntu
$ sudo systemctl start tomcat
Command to check the status of Tomcat in Ubuntu
$ sudo systemctl status tomcat
Command to Auto Start Tomcat service on boot in Ubuntu 18
$ sudo systemctl enable tomcat
Command to verify Apache Tomcat and the port on which service is running
$ sudo netstat -antup | grep 8080

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

Allow Tomcat port in Firewall, so that users can access the application from the external network.

$ sudo ufw allow 8080

Now, Configure Tomcat Web UI (User Interface)

In Tomcat we have web-manager and Host Manager for managing Tomcat and both are password-protected, and it requires a username and password to access.

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

Add below lines in tomcat-users.xml

rolename="admin-gui,manager-gui"/>

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

To access web and host managers from the remote system, add your source network in the allow list.

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

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

Update the below line in “tomcat-users.xml” with source IP from which you’re accessing the web and host Manager. .*

…

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

…

There is an option to allow your network only instead of single. For example: To allow the 192.168.0.0/24 network to only follow the below step.

…

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

…

Restart the Tomcat service on Ubuntu 18

$ sudo systemctl restart tomcat

Access Tomcat on the web browser:

http://ip-address:8080

Once you access tomcat on the web browser you will get the Tomcat default page, This confirms your tomcat is properly installed on the machine.

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

In this article, you learned how to install Tomcat 9 on Ubuntu 18.


thanks for reading this article, you’ll also like to read below articles.

Steps to Manage logs in Linux using Logrotate

Steps to Install Python in Ubuntu

How to install Jenkins on Linux

Steps to install Tomcat on Linux