This post shows students and new users how to install Apache Tomcat on Ubuntu Linux. Apache Tomcat is an open source implementation of 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 for some projects. Tomcat works best when rendering web pages that include Java server page encoding and Java Servlet. These languages ​​are still required for some other protocols used by Java developers.

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

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

To start installing and configuring Tomcat on Ubuntu, follow the steps below.

How to install OpenJDK on Ubuntu Linux

Tomcat requires Java JDK to be installed to work. You can install Oracle Java JDK or its open source alternative called OpenJDK.

For a more detailed Java installation, read our post here.

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

sudo apt update
sudo apt install default-jdk

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

java -version

The command should generate something similar to the following lines:

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 must be installed and ready to use

YOU CAN ALSO READ:   How to install MySQL Workbench on Ubuntu Linux

How to create a Tomcat user and group

To successfully run Tomcat, you will want to run Tomcat as your 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 cat. Linux systems often create groups based on the name of the account.

sudo groupadd tomcat

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

To do that, run the following commands

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

We are ready to download the Tomcat package.

How to download the Tomcat file

At this stage, we install OpenJDK, create the Tomcat account, and are ready to download and configure Tomcat.

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

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

Download the 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 in / opt / tomcat.

To do that, run the following commands:

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 the Tomcat service

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

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

Then create an account with password for the user and save it by copying and pasting the lines below in the file, just before the

<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 close.

YOU CAN ALSO READ:   How to install Magento on Ubuntu Linux with Nginx

Then run the following commands to create a server account for Tomcat

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

then copy paste below lines in 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 following commands to reload the systemd profiles and enable the 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 following commands:

sudo systemctl status tomcat.service

You should get a screen similar to the following;

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 the Tomcat service running. Now, open your browser and find the local server IP or hostname

http://localhost:8080

and you should see the default Tomcat page.

install tomcat on ubuntu linux

Click on the Admin app to log into the backend page.

install tomcat in ubuntu application manager

By default, Tomcat restricts access to the Manager and Host Manager applications to connections that come only from the local server.

If you want to access the Tomcat server remotely, you will want to whitelist the remote IP address that will be allowed. To change the IP address restrictions on these, open the context.xml records.

For the Manager application, type:

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

For the Host Manager application, type:

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

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

YOU CAN ALSO READ:   How to install Webmin on Ubuntu Linux

context.xml files for Tomcat Web applications it should look similar to the one shown 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 the Tomcat service by running the following commands.

sudo systemctl restart tomcat

You should do that.

Conclusion:

In this tutorial we have seen how to install Tomcat on Ubuntu Linux. If you find any errors above or have something to add, use the comment form below.

Write A Comment