Apache Tomcat is a free and open-source Java EE-compliant web server and servlet container for hosting web applications in which Java code can run. It is an implementation of Java Servlet, JavaServer Pages, Java Expression Language, and Java WebSocket technologies. Tomcat is platform-independent and just requires Java installed in your system.

Features

  • Free and very lightweight tool.
  • Fast startup and deploy times.
  • Well documented and big market share.
  • Open-source tool.
  • Cross-platform software.

Apache Tomcat software is released under the Apache License Version 2. The latest version is Apache Tomcat version 10.0 which implements the Servlet 5.0 and JavaServer Pages 3.0 specifications from Jakarta EE. It has additional features that include;

  • Remove support for NPN when using the Tomcat Native Connector as NPN was never standardized.
  • Update the memory leak protection code to support stopping application-created executor threads when running on Java 19 and later.
  • Increase the default buffer size for replication messages from 43800 to 65536 bytes. 
  • Improvements to French, Japanese, and Chinese translations.
  • Correctly calculate bytes written to a response.

This guide shows how to install Apache Tomcat 10 on CentOS 9|AlmaLinux 9|RHEL 9 systems.

Install Apache Tomcat 10 on CentOS 9|AlmaLinux 9|RHEL 9

Update your package system.

sudo dnf update

Step 1. Install Java on CentOS 9|AlmaLinux 9|RHEL 9

Install the default available Java on your system.

sudo dnf install java

Check the version to confirm successful installation.

$ java -version
openjdk version "11.0.15" 2022-04-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.15+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.15+10-LTS, mixed mode, sharing)

Step 2. Install Apache Tomcat on CentOS 9|AlmaLinux 9|RHEL 9

To install Apache, Head over to Apache Downloads to get the Apache Tomcat 10 version. Alternatively, You can download it using the wget command as follows.

sudo dnf -y install wget
wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.0.23/bin/apache-tomcat-10.0.23.tar.gz

Extract the files and move them to a new directory.

tar -xvf apache-tomcat-10*.tar.gz
sudo mv apache-tomcat-10.0.22 /usr/local/tomcat

It is advisable to run the tomcat service as a normal user. Create a tomcat user.

sudo groupadd tomcat
sudo useradd -d /usr/local/tomcat -r -s /bin/false -g tomcat tomcat

Change the ownership of the directory to tomcat user.

sudo chown -R tomcat:tomcat /usr/local/tomcat/

You can verify the change of ownership using the following command.

$ sudo ls -l usr/local/tomcat
drwxr-x---. 2 tomcat tomcat  4096 Jun 19 21:37 bin
-rw-r-----. 1 tomcat tomcat 19010 Jun  2 19:53 BUILDING.txt
drwx------. 2 tomcat tomcat  4096 Jun  2 19:53 conf
-rw-r-----. 1 tomcat tomcat  6210 Jun  2 19:53 CONTRIBUTING.md
drwxr-x---. 2 tomcat tomcat  4096 Jun 19 21:37 lib
-rw-r-----. 1 tomcat tomcat 60269 Jun  2 19:53 LICENSE
drwxr-x---. 2 tomcat tomcat     6 Jun  2 19:53 logs
-rw-r-----. 1 tomcat tomcat  2333 Jun  2 19:53 NOTICE
-rw-r-----. 1 tomcat tomcat  3398 Jun  2 19:53 README.md
-rw-r-----. 1 tomcat tomcat  6908 Jun  2 19:53 RELEASE-NOTES
-rw-r-----. 1 tomcat tomcat 16515 Jun  2 19:53 RUNNING.txt
drwxr-x---. 2 tomcat tomcat    30 Jun 19 21:37 temp
drwxr-x---. 7 tomcat tomcat    81 Jun  2 19:53 webapps
drwxr-x---. 2 tomcat tomcat     6 Jun  2 19:53 work

Configure CATALINA_HOME Environment variable required to run tomcat server.

echo "export CATALINA_HOME="/usr/local/tomcat"" >> ~/.bashrc
source ~/.bashrc

Step 3. Configure Web Management Interface

In order to access Tomcat Web Management Interface, Create a user to access the manager-gui and admin-gui.

Edit the tomcat-users file.

sudo vi /usr/local/tomcat/conf/tomcat-users.xml

Add the following lines just above the </tomcat-users> tag at the end of the file.

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

Remember to update the ‘password’ field with your preferred one. Save and exit the file.

Next, we will allow the tomcat manager and host manager to be accessible for your remote IP address. By default, the Manager app and Host-manager are only accessible on localhost.

For the host manager app, edit the following file

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

Add your remote system IP address.

<Context antiResourceLocking="false" privileged="true" >
<Valve className="org.apache.catalina.valves.RemoteAddrValve" 
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|your-remote-system-ip" />

Update the system IP of the remote machine to use. Save and exit the file.

For the manager app, edit the following file

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

Add your remote IP address

<Context antiResourceLocking="false" privileged="true" >
<Valve className="org.apache.catalina.valves.RemoteAddrValve" 
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|your-remote-system-ip" />

Update the system IP of the remote machine to use. Save and exit the file.

Run Tomcat as a service

Start the Tomcat Service using the following command.

cd /usr/local/tomcat
sudo ./bin/startup.sh

The sample output is as shown below.

Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

If you have Firewall enabled, allow port 8080 for tomcat.

sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

You can access the Tomcat server on port 8080 on your browser.

To access the Manager GUI click on Manager App. Then you will be presented will a login prompt as shown below.

Enter the Login details and the page shows as below.

For the Host manager GUI, you follow the same process and the page is as shown below.

Configure NGINX as Tomcat Proxy

If you have not enabled the EPEL repository in your system, follow The instructions to Enable the EPEL repo.

Then once done, install Nginx Web Server

sudo dnf install nginx

Start and enable the Nginx service on boot.

sudo systemctl start nginx
sudo systemctl enable nginx

You can check the status.

$ systemctl status nginx
 nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor pre>
     Active: active (running) since Mon 2022-06-20 11:36:33 EAT; 40s ago
   Main PID: 71019 (nginx)
      Tasks: 3 (limit: 23427)
     Memory: 2.8M
        CPU: 35ms
     CGroup: /system.slice/nginx.service
             ├─71019 "nginx: master process /usr/sbin/nginx"
             ├─71020 "nginx: worker process"
             └─71021 "nginx: worker process"

Create a new virtual host configuration file for tomcat.

sudo vi /etc/nginx/conf.d/tomcat.conf

Append the following to the file.

server {

listen 80;

listen [::]:80;

server_name  tomcat.example.com;

location / {
proxy_pass http://localhost:8080;
}
}

tomcat.example.com is the value of your domain name. Save and close the file.

Restart Nginx to apply changes.

sudo systemctl restart nginx

Configure SELinux to access the Tomcat service.

sudo setsebool -P httpd_can_network_connect=1

You can now access tomcat using http://tomcat.example.com without specifying the port 8080.

Conclusion

This guide has shown you how to Install Tomcat on CentOS 9|AlmaLinux 9|RHEL 9. Tomcat is lightweight and is preferable in processes that deal with numerous large-scale, mission-critical web applications across a diverse range of industries and organizations.

Similar guides:

LEAVE A REPLY

Please enter your comment!
Please enter your name here