Kiến Thức Linux Linux Nâng Cao

How to Install and Configure Apache Tomcat 9 on Ubuntu 20.04 LTS

Apache Tomcat is an open-source Java Servlet implementation developed by the Apache Software Foundation. In addition to being a Java Servlet container, Tomcat supports other Java server technologies too, including JavaServer Pages (JSP), Java Expression Language, and Java WebSocket.

The Apache Tomcat provides an HTTP Web Server for Java applications that supports HTTP/2, OpenSSL for JSSE, and TLS virtual hosting.

In this tutorial, we will show you how to install and configure Apache Tomcat 9.0.34 with the Java OpenJDK 11 on the latest version of ubuntu 20.04 Server.

Prerequisites

For this tutorial, we will install the Apache Tomcat on the Ubuntu 20.04 Server with 1 GB of RAM, 25 GB free disk space, and 2 CPUs.

What we will do?

 

  • Install Java OpenJDK
  • Setup JAVA_HOME Environment variable
  • Download and Install Apache Tomcat 9.0.34
  • Setup Apache Tomcat as a Systemd Service
  • Setup Apache Tomcat Authentication
  • Testing

Step 1 – Install Java OpenJDK

First, we will install the Java OpenJDK packages to our Ubuntu 20.04 Server.

By default, the latest Ubuntu 20.04 repository provides multiple Java versions, including the OpenJDK 11.

Update all repository on your system and install Java OpenJDK 11 using the apt command as below.

sudo apt update
sudo apt install default-jdk

Once all installation is completed, check your Java version as below.

java -version

Below is the response you will get.

openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

As can be seen, the Java OpenJDK 11 is now installed on the Ubuntu 20.04 Server.

Step 2 – Setup JAVA_HOME Environment Variable

After installing the java OpenJDK 11, we will setup the JAVA_HOME environment variable on our system.

Check all available Java versions on your system using the following command.

sudo update-alternatives --config java

If you have only the Java OpenJDK on your system, you will get the response below.

There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-11-openjdk-amd64/bin/java
Nothing to configure.

If you’re using multiple Java versions, choose the default java version on your environment.

As can be seen, the Java OpenJDK 11 installation directory at the ‘/usr/lib/jvm/java-11-openjdk-amd64’.

Next, edit the ‘/etc/environment’ configuration using the vim editor.

vim /etc/environment

Paste the following configuration into it.

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Save and close.

Next, edit your ‘~/.bashrc’ config file.

vim ~/.bashrc

Add the following environment variable into it.

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

Save and close.

Now reload your ‘~/.bashrc’ configuration and check the ‘JAVA_HOME’ environment variable.

source ~/.bashrc
echo $JAVA_HOME

Below is the result you will get.

Setup JAVA_HOME environment variable

As a result, th ‘JAVA_HOME’ environment variable has been created, located at the Java OpenJDK 11 installation directory ‘/usr/lib/jvm/java-11-openjdk-amd64’.

Step 3 – Install and Configure Apache Tomcat

In this step, we will download and install the Apache Tomcat 9.0.34. The Apache Tomcat will be running under the user ‘tomcat’ at the ‘/opt/tomcat’ directory.

Create a new user and group named ‘tomcat’ using the command below.

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

Next, go to the ‘/opt/’ directory and download the Apache Tomcat using the wget command below.

cd /opt/
wget -q https://downloads.apache.org/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz

Once it’s downloaded, extract the Apache Tomcat binary file and rename the directory to ‘tomcat’.

tar -xf apache-tomcat-9.0.34.tar.gz
mv apache-tomcat-9.*/ tomcat/

Next, change the ownership of the ‘/opt/tomcat’ directory to the user ‘tomcat’.

chown -R tomcat:tomcat /opt/tomcat
chmod +x /opt/tomcat/bin/*

The Apache Tomcat is downloaded at the ‘/opt/tomcat’ directory.

Download Apache Tomcat

Next, we will configure the Apache Tomcat ‘CATALINA_HOME’ environment variable, which is located at the ‘/opt/tomcat’ directory.

Edit the ‘~/.bashrc’ configuration using vim editor.

vim ~/.bashrc

Paste the following configuration into it.

export CATALINA_HOME=/opt/tomcat

Save and close.

Now reload the ‘~/.bashrc’ config file and check the ‘CATALINA_HOME’ environment variable.

source ~/.bashrc
echo $CATALINA_HOME

You will get the ‘/opt/tomcat’ as a value of the environment variable ‘CATALINA_HOME’.

Now you can start the Apache Tomcat manually using the following command.

$CATALINA_HOME/bin/startup.sh

You will get the Apache Tomcat is started.

Start Apache Tomcat

The Apache Tomcat is up and running on the Ubuntu system, and it’s running on default port ‘8080’. Check it using the following command.

ss -plnt

And you will get the port ‘8080’ that is used by the Java application.

Now stop the Apache Tomcat and change the ownership of the ‘/opt/tomcat’ directory to the ‘tomcat’ user as below.

$CATALINA_HOME/bin/shutdown.sh
chown -hR tomcat:tomcat /opt/tomcat/

As a result, the installation and configuration of Apache Tomcat have been completed.

Stop Apache Tomcat

Step 4 – Setup Apache Tomcat as a Systemd Service

For this tutorial, we will run the Apache Tomcat as a systemd service. And for this step, we will create a new systemd service file named ‘tomcat.service’ for our Apache Tomcat installation.

Go to the ‘/etc/systemd/system’ directory and create a new service file ‘tomcat.service’.

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

Now paste the following configuration into it.

[Unit]
Description=Apache Tomcat 9 Servlet Container
After=syslog.target network.target

[Service]
User=tomcat
Group=tomcat
Type=forking
Environment=CATALINA_PID=/opt/tomcat/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Save and close.

Next, reload the systemd manager.

systemctl daemon-reload

Now start the Apache Tomcat service and add it to the system boot.

systemctl start tomcat
systemctl enable tomcat

Setup Apache Tomcat as a Systemd Service

The Apache Tomcat service is up and running as a systemd service, check it using the following command.

ss -plnt
systemctl status tomcat

Below is the result you will get.

Setup Apache Tomcat as a Systemd Service

As a result, the Apache Tomcat configuration as a systemd service has been completed.

Step 5 – Enable Apache Tomcat Authentication

In this step, we will enable the Apache Tomcat authentication for both the manager and the host-manager.

First, go to the ‘/opt/tomcat/conf’ directory and edit the configuration ‘tomcat-users.xml.

cd /opt/tomcat/conf
vim tomcat-users.xml

Change the username and password with your own and paste the following configuration into it.

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

Save and close.

Setup Apache Tomcat Authentication

Next, go to the ‘/opt/tomcat/webapps/manager/META-INF/’ directory and edit the Apche Tomcat mnager configuration ‘context.xml’.

cd /opt/tomcat/webapps/manager/META-INF/
vim context.xml

Comment the following line below.

<!--  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

Save and close.

Setup Apache Tomcat Authentication

Next, go to the ‘/opt/tomcat/webapps/host-manager/META-INF/’ directory nd edit the Apache Tomcat hos- manager configuration ‘context.xml’.

cd /opt/tomcat/webapps/host-manager/META-INF/
vim context.xml

Comment the following line below.

<!--  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

Save and close.

Setup Apache Tomcat Authentication

Now restart the Apache Tomcat service using the command below.

systemctl restart tomcat

As a result, the Apache Tomcat authentication for manager and host-manager has been enabled.

Setup Apache Tomcat Authentication

Step 6 – Testing

Open your web browser and type the server IP address with port 8080. ANd you will see the Apache Tomcat default index page that informs that the installation is successful.

http://10.5.5.34:8080/

Apache Tomcat Home Page

Next, add the ‘/manager/html’ path on the  URL and you will be prompted for the Tomcat manager authentication.

http://10.5.5.34:8080/manager/html

Type your username and password, and you will get the Apache Tomcat Manager Dashboard.

Apache Tomcat Web Application Dashboard

Next, go to the Apache Tomcat virtualhost manager ‘/host-manager/html’.

http://10.5.5.34:8080/host-manager/html

And you will get the Apache Tomcat virtual host manager as below.

Apache Tomcat Host Manager Dashboard

As a result, the installation of Apache Tomcat on Ubuntu 20.04 has been completed successfully.

Add Comment

Click here to post a comment