How to Install Tomcat on Ubuntu Linux

web desk 3345988 640

This post shows students and new users how to install Apache Tomcat on Ubuntu Linux. Apache Tomcat is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket developed by the Apache Software Foundation.

Although not as popular as Apache2 or Nginx HTTP servers, Tomcat is still important to some projects. Tomcat works best when rendering web pages the include Java server page coding and Java Servlet. These languages are still required by some other protocols used by Java developers.

Tomcat requires Java SE 8 or later to be installed on the system. We showed you how to install OpenJDK on Ubuntu which will be needed to run Tomcat.

Also, for students and new users learning Linux, the easiest place to start learning is on Ubuntu Linux. Ubuntu is the modern, open source Linux operating system for desktop, servers and other devices.

To get started with installing and configure Tomcat on Ubuntu, follow the steps below.

How to install OpenJDK on Ubuntu Linux

Tomcat requires Java JDK to be installed in order to function. You can either install Oracle Java JDK or its open source alternative called OpenJDK.

Fore more detailed installation of Java, read our post here.

You can also run the commands below to install the default OpenJDK on Ubuntu.

sudo apt update
sudo apt install default-jdk

Once the OpenJDK is installed, verify it by checking the Java version:

java -version

The command should output something similar to the lines below:

openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

Java should be installed and ready to use

How to create Tomcat user and group

To properly run Tomcat, you’ll want to run Tomcat as it own user without root privileges. To do that, create a new user and group that will run the Tomcat service.

First, create a new tomcat group called tomcat. Linux systems usually create groups based on the account name.

sudo groupadd tomcat

Next, create a new tomcat user called tomcat and make the user a member of the tomcat group above. Well also want to make /opt/tomcat the home folder for the tomcat user.

To do that, run the commands below

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

We’re ready to download Tomcat package.

How to download Tomcat file

At this stage, we’ve install OpenJDK, created Tomcat account and we’re ready to download and configure Tomcat.

At the time of this writing the latest version of the 10 series was 10.0.12.

You can get the latest from the link below. If you find a newer release at the link below, then download it instead.

Download Tomcat package

cd /tmp
wget ftp://apache.cs.utah.edu/apache.org/tomcat/tomcat-10/v10.0.12/bin/apache-tomcat-10.0.12.tar.gz

After downloading, create a Tomcat home folder at /opt/tomcat. Then extract the downloaded folder into /opt/tomcat.

To do that, run the commands below:

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

Next, give the tomcat user control of the entire directory and make all the scripts in the bin location executable.

sudo chown -R tomcat: /opt/tomcat
sudo sh -c 'chmod +x /opt/tomcat/bin/*.sh'

How to configure Tomcat service

Now that the package is extracted, run the commands to open Tomcat configuration file for its default user

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

Then create an account with password for the user and save by copying and pasting the lines below into the file, just before the </tomcat-users>

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

Save the file and exit.

Next, run the commands below to create a server account for Tomcat

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

then copy and paste the lines below into the file and save

[Unit]
Description=Tomcat servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"

Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

Save and exit.

After that, run the commands below to reload systemd profiles and enable tomcat service.

sudo systemctl daemon-reload
sudo systemctl start tomcat.service
sudo systemctl enable tomcat.service

To check if Tomcat is running or not, run the commands below:

sudo systemctl status tomcat.service

You should get similar screen as below;

tomcat.service - Tomcat servlet container
     Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-10-06 15:10:27 CDT; 24s ago
   Main PID: 5778 (java)
      Tasks: 19 (limit: 4651)
     Memory: 115.4M
     CGroup: /system.slice/tomcat.service
             └─5778 /usr/lib/jvm/default-java/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties>

Oct 06 15:10:27 ubuntu2004 systemd[1]: Starting Tomcat servlet container...
Oct 06 15:10:27 ubuntu2004 startup.sh[5771]: Tomcat started.
Oct 06 15:10:27 ubuntu2004 systemd[1]: Started Tomcat servlet container.

You should see Tomcat service running. Now, open your browser and browse to the local server IP or hostname

http://localhost:8080

and you should see Tomcat default page.

install tomcat on ubuntu linux

Click on the Manager App to logon to the backend page.

install tomcat on ubuntu app manager

By default, Tomcat restricts access to the Manager and Host Manager apps to connections coming the local server only.

If you want to access the Tomcat server remotely, you’ll want to whitelist the remote IP address to be allowed. To change the IP address restrictions on these, open the appropriate context.xml files.

For the Manager app, type:

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

For the Host Manager app, type:

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

Inside, comment out the IP address restriction to allow connections from anywhere. Alternatively, if you would like to allow access only to connections coming from your own IP address, you can add your public IP address to the list:

context.xml files for Tomcat webapps should look similar to the one below:

<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|new_public_ip_address_here" />-->
</Context>

Save and close the files and you’re done!

Restart Tomcat service by running the commands below.

sudo systemctl restart tomcat

That should do it.

Conclusion:

This post showed you how to install Tomcat on Ubuntu Linux. If you find any error above or have somethin to add, please use the comment form below.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.