Apache Tomcat – Linux Hint https://linuxhint.com Exploring and Master Linux Ecosystem Fri, 12 Feb 2021 01:15:40 +0000 en-US hourly 1 https://wordpress.org/?v=5.6.2 How to install and configure Apache Tomcat on Fedora Linux https://linuxhint.com/install-apache-tomcat-fedora-linux/ Thu, 11 Feb 2021 07:01:36 +0000 https://linuxhint.com/?p=89576 Apache Tomcat is one of the most widely used web application servers in the world. It is an open-source project of Apache Software Foundation. It is written in Java. It is used for implementing servlet containers and Java Server Pages(JSP) in Java.

Earlier, Tomcat required a high level of expertise for configuring and administering its services, as only advanced users and developers were able to work it out. With Tomcat’s GUI installer, it has become just a matter of a few commands to administer the server as a system service.

What will we cover

This tutorial will show you how to install apache Tomcat and use it to deploy a basic JSP program. Tomcat requires JRE (Java Runtime Environment) for running java web applications. In case if you are developing a Java application, you will need a full JDK application installed. For this, we will cover the guide only with the JRE only.

Prerequisites

You need to be familiar with the Java and basic Linux command to understand this tutorial better. We assume that you have already installed the JRE (Java Runtime Environment) on your system. You also need to have root privileges for installing Apache Tomcat.

Downloading Tomcat

1. To download the Apache Tomcat, visit the Apache Tomcat home page, where you will see different available versions. Alternatively, you can also use the wget command to get the file. For this guide, we are using Tomcat 9.

# wget https://mirrors.estointernet.in/apache/tomcat/tomcat-9/v9.0.43/bin/apache-tomcat-9.0.43.tar.gz

2. If you prefer, you can download Tomcat from the homepage. This is shown below:

Extracting The Binary Archive

1. Once the archive binary file is downloaded, you need to copy it to the directory where you want to install the Tomcat server and extract the file there. For example, we will extract the Tomcat tar file into /opt/tomcat. For this, we first need to create a directory ‘tomcat’ inside /opt. Use the following command to create a directory.

# mkdir /opt/tomcat
# tar xzf apache-tomcat-9.0.43.tar.gz -C /opt/tomcat

Creating a user and group for Tomcat

We will create a non-root user and group for running the Apache Tomcat server. Use the command below for creating the user and group.

# useradd -r tomcat

The above command will also add a ‘tomcat’ group.

Now we will change the ownership of the tomcat directory to the Tomcat user with the command:

# chown -R tomcat:tomcat /opt/tomcat

Setting Environment Variables

Tomcat requires certain environment variables to be set for running the startup scripts. Let’s see those variables:

a. CATALINA_HOME: The location of this environment variable is the root directory of Tomcat’s “binary” distribution. In our case, this root directory is /opt/tomcat/apache-tomcat-9.0.43

b. JRE_HOME or JAVA_HOME: These environment variables specify the location of Java Runtime Environment and a JDK location, respectively. If you are specifying both JRE_HOME and JAVA_HOME, then JRE_HOME will be used by default.

To set these variables, open the following file:

# vi /etc/profile

Now insert the following lines at the end of this file:

export JRE_HOME=/usr/java/jre1.8.0_281-amd64/bin/java
export CATALINA_HOME=/opt/tomcat/apache-tomcat-9.0.43

Now save the file and run the below command to apply these changes:

# . /etc/profile

To check if these variables are correctly set, check if the output of the below command is the same as the value for JRE_HOME and CATALINA_HOME:

# echo $JRE_HOME
# echo $CATALINA_HOME

See the below pictures for reference:

Creating Tomcat service

Now we will create a simple systemd unit file to define our Tomcat service. Create the service with the following instructions:

1. Create a file tomcat.service:

# vim /etc/systemd/system/tomcat.service

Now put the following content inside it:

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

[Service]
Type=forking
User=tomcat
Group=tomcat

Environment=CATALINA_PID=/opt/tomcat/apache-tomcat-9.0.43/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat/apache-tomcat-9.0.43
Environment=CATALINA_BASE=/opt/tomcat/apache-tomcat-9.0.43

ExecStart=/opt/tomcat/apache-tomcat-9.0.43/bin/catalina.sh start
ExecStop=/opt/tomcat/apache-tomcat-9.0.43/bin/catalina.sh stop

RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target

Note: Please replace the bolded text with the path of your Tomcat installation.

Now save the file and reload the systemd configuration with the following command
to apply the changes

# systemctl daemon-reload

We are now ready to use the tomcat service. Start the service and enable it to persist the reboot.

# systemctl start tomcat.service
# systemctl enable tomcat.service

Check the status of service; it should show an active running status:

# systemctl status tomcat.service

All the above steps are shown below:

Accessing Tomcat in Browser

Now we are ready to test if our tomcat server is correctly installed or not. To check this, open your web browser and browse the addresses:

http://localohost:8080
or
http://system_IP_addr:8080 (To see your system IP, use the ip addr command.)

You would see the default homepage of Apache Tomcat. The following screenshot shows the tomcat homepage:

Deploying a simple JSP application

Now we will deploy a basic JSP application with a Tomcat server.

1. Create a basic JSP application called ‘test.jsp’ inside the directory “/opt/tomcat/apache-tomcat-9.0.43/webapps/ROOT/”:

# nano /opt/tomcat/apache-tomcat-9.0.43/webapps/ROOT/test.jsp

Note: Again, replace the bolded text with the path of your Tomcat installation.

2. Put the following content inside it:

<html>
<head><title> JSP Page</title></head>
<body>
This is a JSP Page from LinuxHint!<br/>
<%
out.println("Your System IP address is: " + request.getRemoteAddr());
%>
</body>
</html>

3. Now again, open the web browser and browse the following address:

http://localhost:8080/test.jsp

This time you should see the following web page:

Conclusion

This tutorial shows how we can install Apache Tomcat from an archive binary file on Fedora Linux. We have learned to install a JSP application with tomcat.

]]>
How to Install Apache Tomcat Server on Ubuntu 20.04 https://linuxhint.com/install_apache_tomcat_server_ubuntu/ Wed, 17 Jun 2020 11:37:51 +0000 https://linuxhint.com/?p=61618

Introduction


Apache Tomcat can run various Java technologies, and runs the JavaServer (JSP), JavaServlet, and Java Expression languages. This article will show you how to install Apache Tomcat Server and set up the Web Application Manager on Ubuntu 20.04 in 11 easy steps. Apache Tomcat Server can be installed right from Ubuntu’s software repository, which contains the latest, most stable version of the Tomcat server.

Step 1: Update APT

First, as always, update your APT.

$ sudo apt update

Step 2: Check for Tomcat in Repository

Check for the Tomcat server package in the repository. The repository will show you all the latest packages available for download.

$ sudo apt-cache search tomcat

Step 3: Download Tomcat

Download the tomcat9 package and the tomcat9 admin package and its dependencies with the following terminal command.

$ sudo apt install tomcat9 tomcat9-admin

Step 4: Install Apache Tomcat Server

When the download is finished, it will install the Apache Tomcat Server, which will start up automatically. For verification, type the following ss command, which will show you the 8080 open port number, the default open port reserved for Apache Tomcat Server.

$ ss -ltn

Step 5: Change Tomcat Settings

When the Ubuntu OS reboots, the Apache Tomcat Server will start automatically. This program setting can be changed by entering one of the following two commands.

$ sudo systemctl enable tomcat9

OR

$ sudo systemctl disable tomcat9

Step 6: Allow Traffic to Port 8080

If firewall ports, specifically UFW, are active on your system, devices that want to connect with the Apache Tomcat Server will have difficulty connecting. To allow traffic from any source to port 8080 of the Tomcat Server, type in the following command.

$ sudo ufw allow from any to any port 8080 proto tcp

Step 7: Test Tomcat Server

Now, you may test your Tomcat server. When Tomcat starts running, you can test the program in a web browser. Using the system’s loopback address, you can connect to the Tomcat server by specifying the port with the address in the URL search bar, as follows:

http://127.0.0.1:8080

Tomcat is up and running if you see this page, “it works!”.

Step 8: Create User

You should create a user for the Web Application Manager in the Tomcat Server. A new user account must be set up to use the Apache Tomcat Web Application Manager.

Open the tomcat-users.xml file in the Tomcat directory with your favorite text editor.

$ sudo nano /etc/tomcat9/tomcat-users.xml

Step 9: Add Tagged Lines

When the file is opened, add these three tagged lines in the file above the “<” tag. Here, new user with the name “tomcat” has been created with a password set as “pass.” Add your own values as substitution for the username and password.

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

Save the file and close it. Now, we can move on to accessing the manager application area.

Step 10: Restart Tomcat

Restart the Tomcat Server with the following command.

$ sudo systemctl restart tomcat9

Step 11: Access Tomcat Application Manager

In order to access the Tomcat server web application manager, enter the URL:  http://127.0.0.1:8080/manager/html into the web browser URL search bar and press enter. Next, enter the credentials that you gave to the new user in Tomcat Server. You should now see the Web Application Manager Window.

Conclusion

The Apache Tomcat Server is a great software program for deploying a Java HTTP server. Setting up Apache Tomcat Server on Ubuntu 20.04 is very easy and does not require complex commands.

]]>