How to Install Tomcat on Ubuntu Linux [Step

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

The Apache Tomcat Java servlet container is a free, open-source application for writing and running web applications. Installing the latest version of Tomcat on Ubuntu Linux requires no more effort than opening a terminal window and typing “sudo apt install apache2” into the command line.

The “install tomcat on linux” is a tutorial for installing Tomcat on Ubuntu Linux. This tutorial will help you install the latest version of Tomcat on your system.

How to Install Tomcat on Ubuntu Linux [Step

If you need to execute Java-based web applications, Apache Tomcat is the way to go. Apache Tomcat is a lightweight open-source version of the Jakarta EE platform for serving Java-based web services. Installing Tomcat on Ubuntu is also a good place to start.

From Java Servlets to JavaServer Pages to Java Expression Language, Apache Tomcat offers a variety of approaches to apply Java-based technologies. You will learn how to install Apache Tomcat 10.0 on an Ubuntu Linux system in this article.

Prerequisites

This article will be a step-by-step guide. Make sure you have the following items in order to follow along:

  • A Linux system running Ubuntu 14.04.4 LTS or above. With a public IP address of 52.15.72.168, this tutorial utilizes Ubuntu 18.04.5 LTS.

All versions of Windows, Linux, and macOS are supported by Apache Tomcat.

Related: [Step-by-Step] How to Install Ubuntu 20.04

Installing the Java Runtime Environment (JRE)

The runtime environment is the foundation of all Java programs. The purpose of Apache Tomcat is to serve Java-based applications. However, the host must be able to execute Java applets before it may do so. The JRE is the tool that allows you to do so.

To install the JRE on your Ubuntu system, follow these steps:

1. Log in to your Ubuntu system via SSH.

Setting up SSH in Linux (A Windows Guy in a Linux World)

2. Use the sudo apt update command to check that Ubuntu has all of the most recent packages installed.

# Updating Package Repositories with the Most Up-to-Date Version # sudo apt update sudo apt update sudo apt update sudo apt update sudo apt update sudo apt update sudo apt update sudo apt update sudo apt update sudo

Updating the Ubuntu machine's package repositories Updating the Ubuntu machine’s package repositories

Although the root user is used in this article, it is usually advisable to use a less-privileged account that belongs to the sudoers group.

3. Finally, use the apt program to install the OpenJDK package. The only free and open-source JRE that works properly with Tomcat is OpenJDK.

A Java Standard Edition Runtime Environment (JRE) version 8 or later is required for Apache Tomcat.

# Installing the Java Runtime Environment (JRE) sudo apt install default-jdk

Installing the Java Runtime EnvironmentInstalling the Java Runtime Environment

4. Run the java —version command to see what JRE version you have installed. If the installation is successful, the command result will show openjdk 11.0.11.

evaluating the Java version —version java

Checking the Java version that is installed Checking the Java version that is installed

The most recent version of OpenJDK available at the time of writing is 11.0.11, although your version may vary.

Creating a User and Group in Apache Tomcat

To access different system resources, Tomcat, like all other Linux services, must operate under a particular user account. You could run Tomcat as the God-like root account, which has complete system capabilities, but this gives Tomcat much too many powers and might be dangerous.

Create an Apache Tomcat user and group under which the server will operate to reduce Tomcat’s unwanted permissions.

While you’re still connected to your Ubuntu computer, do the following:

1. First, use the groupadd command to establish a new group for Tomcat. This group is required to safeguard the Tomcat configuration files stored at /opt/tomcat/conf by granting only reading and execution access.

2. Next, create the Apache Tomcat user with the “>useradd command.

  • -s – Set the shell to /bin/false to prevent this user from logging in interactively. You don’t want this user to be used as a login user.
  • -g – Assign the new Tomcat user to the previously defined Tomcat group. useradd -s /bin/false -g useradd -s /bin/false -g tomcat tomcat sudo useradd -s /bin/false -g tomcat tomcat tomcat tomcat tomcat to

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

3. Finally, use the id command to confirm that the tomcat user and group exist, as shown below. The id command shows the user ID, group ID, and group membership of a user account.

In your situation, the UID, GID, and group numbers may vary, but they should all have the (tomcat) name as shown below.

Check the user's informationCheck the user’s information

Apache Tomcat Installation

It’s time to install Apache Tomcat now that you’ve installed the JRE and generated a user account and group for Tomcat to utilize.

1. First, create a directory for Apache Tomcat to be installed using the “>mkdir command. Navigate into that directory with the cd command, as shown below. Although you can install tomcat in any directory, it is recommended that the software is installed under opt directory.

# Creating and shifting to the tomcat directory under opt. mkdir /opt/tomcat cd /opt/tomcat

2. Now, use the wget command to get the most recent version of Tomcat. The program below will download Apache Tomcat 10.0.6.

On the official site, you may find the most recent version of Apache Tomcat.

wget https://mirrors.estointernet.in/apache/tomcat/tomcat-10/v10.0.6/bin/apache-tomcat-10.0.6.tar.gz/apache-tomcat-10.0.6.tar.gz/apache-tomcat-10.0.6.tar.gz/apache-tomcat-10.0.6.tar.gz/apache-tomcat-10.0.6.

Apache Tomcat 10 is available for download.Apache Tomcat 10 is available for download.

After a successful download, use the tar command to extract the Apache Tomcat package to the previously established /opt/tomcat directory.

  • -x — Extract an archive’s whole contents or a single or several files.
  • -z – Use GZip to decompress the archive.
  • -v – Displays all files as they were extracted using the tar command.
  • -f — The file to extract is specified.
  • —strip-components=number – Remove the provided number of leading components from each extracted file (e.g., supplying a 1 value from directory/file would result in only the file).
  • -C – Extract the files to the given location.

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

The Apache Tomcat archive is being extracted.The Apache Tomcat archive is being extracted.

Permissions for Apache Tomcat Users and Groups are being updated.

It is critical to execute Apache Tomcat using a non-root user account after it has been installed, as previously specified. In the steps that follow, you’ll upgrade Apache Tomcat to operate as the tomcat user and group that you previously defined.

1. Use the cd command to get to the /opt/tomcat directory.

2. Using the chgrp command, modify the group membership for the /opt/tomcat directory and its child folders. Because the Tomcat configuration files will be maintained by this group when the Tomcat service begins, you’re updating the group here.

# Recursively change the /opt/tomcat directory group membership to tomcat (-R) chgrp –R tomcat /opt/tomcat sudo chgrp –R tomcat /opt/tomcat sudo chgrp – # Run tomcat ls -lh to see whether the group has been changed.

3. Next, use the chmod command to give the tomcat group read and execute access to the /opt/tomcat/conf directory and any files contained inside it. Tomcat needs the /opt/tomcat/conf directory to conduct a variety of tasks, like specifying the Java Servlet to use, user information, and so on.

Because an attacker can’t update the Tomcat configuration, deploy new web apps, or modify existing web applications if they compromise the Tomcat process, it’s critical to have the group tomcat with just read and execute access on the configuration files.

# Assign sudo chmod -R g+r conf On conf, read permission to the tomcat group. chmod -R g+x conf # sudo chmod -R g+x conf Assign On conf, provide permission to the tomcat group.

4. Finally, use the chown command to make the tomcat user the owner of the webapps, work, temp, and logs folders in the /opt/tomcat directory. By making the Tomcat service the owner of these folders, the Tomcat service is able to read, write, and execute all files in those directories.

# Give tomcat ownership of the whole directory. # You’re working in the current directory, as indicated by the. sudo chown -R webapps/ work/ temp/ logs/ sudo chown -R webapps/ work/ temp/ logs/ sudo chown –

Make a Systemd Service File for Tomcat.

Although Apache Tomcat may be started, it will not keep running if you exit the command-line session or restart the server. This is where systemd enters the picture! You must setup Apache Tomcat as a systemd service in order for it to operate in the background and launch on startup.

1. Use the update-java-alternatives command to get the java installation path, which is also known as the JAVA HOME environment variable. Before you build a systemd service, you must first find the JRE installation directory.

update-java-alternatives -l sudo

The OpenJDK JRE is installed in the /usr/lib/jvm/* folder on the tutorial’s Ubuntu system, as seen in the accompanying picture.

Getting to the JRE's installation directoryGetting to the JRE’s installation directory

2. Using your chosen editor, create the file tomcat.service in the /etc/systemd/system directory. The service is started in the background after each reboot when it is created in /etc/systemd/system.

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

3. Paste the contents of the tomcat.service file into the tomcat.service file. Update the JAVA HOME variable to use the path /usr/lib/jvm/java-1.11.0-openjdk-amd64 before saving the file.

There are three parts in a typical service file. A unit containing a service description and the target type, such as a network. After the Network is up, target, which means execute Services. Following that, the Service section comprises a number of environment variables that must be configured as the installation directory or child directories’ paths. Finally, Install is configured as a multi-user application. The term “target” refers to a multiuser mode that is not networked.

[Unit] Apache Tomcat is a web server written in Java. After=network.target [Service] # Type=forking informs systemd that the process is still continuing despite the fact that the parent has departed. Type=forking Environment=JAVA HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64 # Change the JAVA HOME Environment=CATALINA PID=/opt/tomcat/temp/tomcat.pid to /opt/tomcat/temp/tomcat.pid. Environment=CATALINA HOME=/opt/tomcat Environment=CATALINA BASE=/opt/tomcat ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/opt/tomcat/bin/shutdown.sh User=tomcat Group=tomcat Restart=always [Install] WantedBy=multi-user.target

4. Finally, execute the daemon-reload command to notify the systemd daemon that a new service has been created.

systemctl daemon-reload sudo

5. Finally, as stated below, start and activate the Apache Tomcat service. When the tomcat service is enabled, the process may start automatically, such as after a reboot.

# Start the Tomcat service using sudo systemctl start tomcat sudo systemctl tomcat enable # Allow the Tomcat service to run.

Setting up the Tomcat service and turning it onSetting up the Tomcat service and turning it on

6. Use the systemctl status command to verify that the Apache Tomcat service is up and functioning.

# Check the status of the Tomcat service using sudo systemctl status tomcat

service(running) verifies that Tomcat has been launched successfully in the screenshot below.

Verifying the Tomcat service's statusVerifying the Tomcat service’s status

Web Content Verification using Apache Tomcat

How can you tell whether Apache Tomcat is up and running? A default web page is offered after the service has been launched to check that the server is up and functioning.

To access the Apache Tomcat default web interface, start a web browser and go to 52.15.72.168:8080, which will take you to a page that looks like the one below.

Using a web browser to access Apache Tomcat.Using a web browser to access Apache Tomcat.

Using Apache Tomcat to Run Your First Application

Although Apache Tomcat is up and running, the default web page isn’t really helpful. Continue reading to discover how to build and operate a simple Java web page.

1. In the /opt/tomcat/webapps/ROOT/ directory, create the file ata.jsp. By default, Tomcat searches for files in the /opt/tomcat/webapps/ROOT/ directory when serving files.

sudo touch ata.jsp /opt/tomcat/webapps/ROOT/

2. Paste the text below into the ata.jsp file you just made and save it. This JSP file is an HTML file with Java code inserted in it that shows some sample text in the HTML page’s body.

<html> <head> <title>ATA post:TomcatServer</title> </head> <body> <Start of Java code> <% out.println(“Hello World! I am running my first JSP Application”); out.println(“ATA is an Awesome site.”); %> <End of Java code> </body> </html>

3. Finally, set the tomcat user as the owner of the ata.jsp file and restart the tomcat service. Setting tomcat as the owner and granting full rights to the Tomcat service enables it to start and access the files.

/opt/tomcat/webapps/ROOT/ata.jsp sudo chown tomcat sudo systemctl tomcat restart

4. Now open your preferred browser and go to the JSP page you just made. The IP address of this tutorial’s web server is 52.15.72.168, as shown below.

Using the web console to verify the application.Using the web console to verify the application.

Conclusion

You learnt how to install Apache Tomcat on an Ubuntu server in this article. It’s simple and fast to deploy Java apps on Apache Tomcat!

Apache Tomcat is the most extensively used open-source web application server for Java developers. Which Java application are you planning to deploy and administer now that you’ve installed Apache Tomcat?

The “ubuntu install tomcat9 apt-get” is a step by step guide for installing Tomcat on Ubuntu Linux. The article also includes the commands to run.

Related Tags

  • install tomcat 8 on ubuntu
  • install tomcat on ubuntu 20
  • install tomcat 9 on linux
  • install tomcat 9 on ubuntu
  • install tomcat 10 on ubuntu