Monday, October 3, 2022

Install and Configure Apache Tomcat on Ubuntu 20.04

In this article, we want to teach you How To Install and Configure Apache Tomcat on Ubuntu 20.04.

Apache Tomcat is a web container. It allows the users to run Servlet and JAVA Server Pages that are based on web applications. It can be used as an HTTP server.

How To Install and Configure Apache Tomcat on Ubuntu 20.04

To set up Apache Tomcat on Ubuntu 20.04, you need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do these, you can follow our article about the Initial Server Setup with Ubuntu 20.04.

Now you can follow the steps below to install Apache Tomcat on your server.

Set up Apache Tomcat on Ubuntu 20.04

First, update your local package index with the following command:

sudo apt update

To check the latest version of Apache Tomcat, you can visit the Apache Tomcat downloads page.

Tomcat 10 needs to have JRE 8 or higher is installed on your server.

To install OpenJDK on Ubuntu 20.04, you can use the following command:

sudo apt install default-jdk -y 

Verify your Java installation with the following command:

java -version 

In your output you will see:

Output
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)

It is recommended to run the Tomcat service as a non-root user. To create a new user run the following command:

sudo useradd -m -d /opt/tomcat -U -s /bin/false tomcat 

This command will create a user and group named tomcat on your system.

Now you can use the wget command to download the Tomcat 10 on Ubuntu 20.04:

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.13/bin/apache-tomcat-10.0.13.tar.gz

Remember to replace the latest version of Tomcat in the above command.

When your download is completed, extract the file in the /opt/tomcat home directory with the following command:

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

Now you need to set the correct permissions for Apache Tomcat on Ubuntu 20.04 with the following commands:

$sudo chown -R tomcat:tomcat /opt/tomcat/  $sudo chmod -R u+x /opt/tomcat/bin 

Now you have the latest Tomcat on your Ubuntu 20.04.

Configure Tomcat on Ubuntu 20.04

At this point, you need to configure Tomcat with user accounts to secure access to admin/manager pages.

You need to edit the Tomcat user configuration file. Open the file with your favorite text editor, here we use vi:

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

Now you need to add the following content to the <tomcat-users>. Just remember to replace the password with a strong and secure password for your manager and admin.

</tomcat-users>
<!-- user manager can access only manager section -->
<role rolename="manager-gui" />
<user username="manager" password="_SECRET_PASSWORD_" roles="manager-gui" />

<!-- user admin can access manager and admin section both -->
<role rolename="admin-gui" />
<user username="admin" password="_SECRET_PASSWORD_" roles="manager-gui,admin-gui" />

When you are done, save and close the file.

Note: The default Tomcat manager and host-manager applications are accessible for localhost only.

To allow access to these pages from the remote system, you need to modify the following configuration files.

For the manager open the file with your favorite text editor, here we use vi:

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

Here you need to comment on the section added for IP address restriction to allow connections from anywhere as shown below:

<Context antiResourceLocking="false" privileged="true" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
  <!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> --> ...

When you are done, save and close the file.

Do the same thing for the host:

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

Comment the same section as the manager:

<Context antiResourceLocking="false" privileged="true" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
  <!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
...

When you are done, save and close the file.

Here you need to create a Tomcat systemd unit file. Open and create the file with your favorite text editor, here we use vi:

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

Add the following content to the file:

[Unit]
Description=Tomcat
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
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

When you are done, save and close the file.

Reload the systemd service to apply the new Tomcat systemd unit file:

sudo systemctl daemon-reload 

Now start the Tomcat on Ubuntu 20.04 with the following command:

sudo systemctl start tomcat.service 

Then, use the flowing command to start the service every time at boot:

sudo systemctl enable tomcat.service

You can verify that your Tomcat is up and running on Ubuntu 20.04 with the following command:

sudo systemctl status tomcat.service 

In your output you will see:

Output
tomcat.service - Tomcat
Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset>
Active: active (running) since Mon 2021-11-29 10:17:50 CET; 5min ago
Main PID: 4907 (java)
Tasks: 21 (limit: 2282)
Memory: 207.1M
CGroup: /system.slice/tomcat.service
└─4907 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util>

Now you can access the Apache Tomcat web interface.

How to Access the Apache Tomcat Web Interface

At this point, you can access your Tomcat web interface by typing your Server’s IP address in your web browser followed by 8080:

http://server-ip-address:8080

You will see:

Tomcat interface on Ubuntu 20.04

If you see this page means that you have successfully installed Tomcat on your Ubuntu 20.04.

As you can see in the web interface, you have Manager App and Host Manager.

The Manager interface provides you with the basic functionality you need to manage your deployed web applications.

Tomcat Host Manager App is used to create/remove Virtual Hosts from the Tomcat service.

Conclusion

At this point, you learn to set up and configure Apache Tomcat on Ubuntu 20.04.

Hope you enjoy using it.

May this article about Installing Apache Tomcat on Centos 7 be useful for you.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles