How to install Apache Tomcat on Linux 9

Share on Social Media

In this configuration guide, you will learn how to install Apache Tomcat on Linux 9 or other Red Hat based Linux distros. #centlinux #linux #apachetomcat

What is Apache Tomcat?:

Apache Tomcat (called “Tomcat” for short) is a free and open-source implementation of the Jakarta Servlet, Jakarta Expression Language, and WebSocket technologies. It provides a “pure Java” HTTP web server environment in which Java code can also run. Thus it’s a Java web application server, although not a full JEE application server.

Tomcat is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation, released under the Apache License 2.0 license.

Video to install Apache Tomcat on Linux:

YouTube player

Environment Specification:

We are using a minimal installed Rocky Linux 9 virtual machine with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – Rocky Linux release 9.1 (Blue Onyx)
  • Hostname – tomcat-01.centlinux-com.preview-domain.com
  • IP Address – 192.168.116.131/24

Read Also: How to install Apache Tomcat on CentOS 7

Update your Linux Server:

By using a ssh client, login to your Linux server as root user.

Set a FQDN (Fully Qualified Domain Name) for your Linux server.

# hostnamectl set-hostname tomcat-01.centlinux-com.preview-domain.com

Refresh your yum cache by using dnf command.

# dnf makecache
Rocky Linux 9 - BaseOS                          280 kB/s | 1.7 MB     00:06
Rocky Linux 9 - AppStream                       653 kB/s | 6.4 MB     00:09
Rocky Linux 9 - Extras                          3.0 kB/s | 7.0 kB     00:02
Metadata cache created.

Execute following command to upgrade software packages in your Linux operating system.

# dnf update -y

Sometimes, the above command also upgrades your Linux Kernel.

In such situation, you should reboot your Linux operating system with newly installed Kernel.

# reboot

After reboot, check the Linux Kernel and operating system versions.

# cat /etc/rocky-release
Rocky Linux release 9.1 (Blue Onyx)

# uname -r
5.14.0-162.6.1.el9_1.0.1.x86_64

Install Tomcat Prerequisites:

By using dnf command, install software packages that are required to download and install Apache Tomcat software.

# dnf install -y wget tar gzip

Apache Tomcat 10.0.x requires Java 8 or later JRE (Java Runtime Environment).

Therefore, you can install OpenJDK 11 (an open source implementation of Java platform) on Linux machine.

# dnf install -y java-11-openjdk

After installation, check the version of your active Java runtime environment.

# java --version
openjdk 11.0.17 2022-10-18 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.17.0.8-2.el9_0) (build 11.0.17+8-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.17.0.8-2.el9_0) (build 11.0.17+8-LTS, mixed mode, sharing)

Create a Linux user to own Tomcat software and processes.

# useradd -r -d /opt/tomcat/ -s /sbin/nologin -c "Tomcat User" tomcat

Install Apache Tomcat on Linux:

Apache Tomcat website provides the latest version of Java application server for various platforms.

You can use wget command to download Tomcat software directly from Linux CLI.

# cd /tmp
# wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.27/bin/apache-tomcat-10.0.27.tar.gz
--2022-12-13 08:48:03--  https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.27/bin/apache-tomcat-10.0.27.tar.gz
Resolving dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
Connecting to dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11984522 (11M) [application/x-gzip]
Saving to: ‘apache-tomcat-10.0.27.tar.gz’

apache-tomcat-10.0. 100%[===================>]  11.43M  1.14MB/s    in 10s

2022-12-13 08:48:13 (1.14 MB/s) - ‘apache-tomcat-10.0.27.tar.gz’ saved [11984522/11984522]

Create a directory and extract downloaded tarball therein.

# mkdir /opt/tomcat
# tar xf apache-tomcat-10.0.27.tar.gz -C /opt/tomcat --strip-components=1

Grant ownership of /opt/tomcat directory to tomcat user.

# chown -R tomcat:tomcat /opt/tomcat/

You need to create one or more admin users to manage your Tomcat server via Application Manager.

Edit tomcat-users.xml file by using vim text editor.

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

Add following lines just before </tomcat-users> tag.

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

By default Application Manager is allowed to be accessed from localhost only.

Therefore, you must edit the following file to make your application manager accessible from other machines within the same network.

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

Search for following lines in this file.

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

Comment out these lines as follows.

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

Similarly, repeat the above step for following file.

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

Create Systemd Service:

Create a systemd unit to define a service for your Java application server.

# vi /usr/lib/systemd/system/tomcat.service

Add following directives in this file.

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

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

Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat

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

RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Execute following command to notify systemd that you have created a new unit file.

# systemctl daemon-reload

Enable and start Tomcat service.

# systemctl enable --now tomcat.service
Created symlink /etc/systemd/system/multi-user.target.wants/tomcat.service → /usr/lib/systemd/system/tomcat.service.

Verify the status of Tomcat service.

# systemctl status tomcat.service
● tomcat.service - Apache Tomcat Server
     Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor pr>
     Active: active (running) since Tue 2022-12-13 09:17:12 CST; 15s ago
    Process: 15601 ExecStart=/opt/tomcat/bin/catalina.sh start (code=exited, st>
   Main PID: 15612 (java)
      Tasks: 28 (limit: 4463)
     Memory: 85.0M
        CPU: 10.879s
     CGroup: /system.slice/tomcat.service
             └─15612 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat/>

Dec 13 09:17:12 tomcat-01.centlinux-com.preview-domain.com systemd[1]: Starting Apache Tomcat Serv>
Dec 13 09:17:12 tomcat-01.centlinux-com.preview-domain.com catalina.sh[15601]: Tomcat started.
Dec 13 09:17:12 tomcat-01.centlinux-com.preview-domain.com systemd[1]: Started Apache Tomcat Serve>

Configure Linux Firewall:

Allow the Tomcat default service port 8080/tcp in Linux firewall, to make it accessible across the network.

# firewall-cmd --permanent --add-port=8080/tcp
success

# firewall-cmd --reload
success

Accessing Application Manager:

Open URL http://tomcat-01.centlinux-com.preview-domain.com:8080/ in a web browser.

Tomcat Default Homepage

Click on “Server Status” button to check you Java application server status.

Java Application Server Status

Go back and click on “Application Manager”. You may need to login by using the credentials that you have defined in tomcat-users.xml file.

Application Manager

Conclusion:

In this configuration guide, you have learned how to install Apache Tomcat on Linux 9. To start using your Java application server, we recommend that you should have Tomcat: The Definitive Guide (PAID LINK) by O’Reilly Media as a quick reference.