How to Install Apache Tomcat Server on CentOS Stream 9 Tutorial

How to Install Apache Tomcat Server on CentOS Stream 9.  The options for the right Java server is a tricky task. You aim is to have a stable solution for  developing and deploying the websites successfully. Apache Tomcat is a popular option as it is reliable, mature and open source with excellent community support. Let’s learn Apache Tomcat.

What is Apache Tomcat

Apache Tomcat is a free and open source Java servlet container developed under the open source environment in 1998. The main aim of Apache Tomcat is to implement the various Java Enterprise Specs, including the Websites API, Java-Server Pages and Java Servlet. The HTTP server is used to serve the static web content. If you want to serve the dynamic content, then use the Servlet. When you combine HTTP and Servlet they become the Apache Tomcat server.

Apache Tomcat is specially designed to use the webserver to manage the dynamic Java based content with the help of HTTP protocols. Tomcat executes Java Servlets to serve web pages that include Java code and Java EE applications.

Advantages of Apache Tomcat

  • Free and Open source – It is an open source means anyone can use the core file for developing personal or commercial applications.
  • Lightweight – Tomcat is a lightweight application that enables you to deploy the content easily with fast data processing power.
  • Flexible – Tomcat is adjustable and customizable, that allows you to customize your application as per your requirements.
  • Stable – Tomcat provides a stable application that runs independently on apache installation.
  • Well Documented – The Tomcat documentation library provides comprehensive information to the developer who wants to learn the development of the Tomcat application.

In this post, we will explain how to install Apache Tomcat Server on CentOS Stream 9

Install Java JDK

Apache Tomcat is written in Java. So Java JDK must be installed on your server. You can install it by running the following command:

				
					dnf install java-11-openjdk-devel -y
				
			

Once Java installation is completed, verify the Java installation using the following command:

				
					java --version
				
			

You should see the Java version in the following output:

				
					openjdk 11.0.15 2022-04-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.15+9-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.15+9-LTS, mixed mode, sharing)
				
			

Install Apache Tomcat on CentOS Stream 9

First, it is recommended to add a dedicated user to run Apache Tomcat. You can add a new user named tomcat using the following command:

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

Next, download the latest version of Apache Tomcat using the following command:

				
					wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.21/bin/apache-tomcat-10.0.21.tar.gz
				
			

Once the download is completed, you should get the following output:

				
					--2022-05-19 15:51:50--  https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.21/bin/apache-tomcat-10.0.21.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: 11928158 (11M) [application/x-gzip]
Saving to: ‘apache-tomcat-10.0.21.tar.gz’

apache-tomcat-10.0.21.tar.gz        100%[=================================================================>]  11.38M  64.9MB/s    in 0.2s    

2022-05-19 15:51:50 (64.9 MB/s) - ‘apache-tomcat-10.0.21.tar.gz’ saved [11928158/11928158]

				
			

Extract the downloaded file to the /opt/tomcat directory with the following command:

				
					tar -xvzf apache-tomcat-10.0.21.tar.gz -C /opt/tomcat/
				
			

Next, navigate to the /opt/tomcat directory and rename the Apache Tomcat directory:

				
					cd /opt/tomcat
mv apache-tomcat-10.0.21 tomcat10
				
			

Next, change the ownership of the /opt/tomcat to tomcat user and group:

				
					chown -R tomcat:tomcat /opt/tomcat
				
			

Create a Systemd Service File for Tomcat

Next, you will need to create a systemd service file to manage the Apache Tomcat service. You can create it using the following command:

				
					nano /etc/systemd/system/tomcat.service
				
			

Add the following lines:

				
					[Unit]
Description=Apache Tomcat 10 Servlet container
Wants=network.target
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/jre"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"

Environment="CATALINA_BASE=/opt/tomcat/tomcat10"
Environment="CATALINA_HOME=/opt/tomcat/tomcat10"
Environment="CATALINA_PID=/opt/tomcat/tomcat10/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/tomcat10/bin/startup.sh
ExecStop=/opt/tomcat/tomcat10/bin/shutdown.sh
Restart=always

[Install]
WantedBy=multi-user.target

				
			

Save and close the file then reload the systemd daemon to apply the changes:

				
					systemctl daemon-reload
				
			

Next, start and enable the Tomcat service to start at system reboot:

				
					systemctl enable tomcat --now
				
			

You can now verify the status of the Apache Tomcat with the following command:

				
					systemctl status tomcat
				
			

You will get the following output:

				
					● tomcat.service - Apache Tomcat 10 Servlet container
   Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-05-19 15:53:18 UTC; 6s ago
  Process: 1945 ExecStart=/opt/tomcat/tomcat10/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 1952 (java)
    Tasks: 29 (limit: 23696)
   Memory: 156.6M
   CGroup: /system.slice/tomcat.service
           └─1952 /usr/lib/jvm/jre/bin/java -Djava.util.logging.config.file=/opt/tomcat/tomcat10/conf/logging.properties -Djava.util.logging.>

May 19 15:53:18 centos systemd[1]: Starting Apache Tomcat 10 Servlet container...
May 19 15:53:18 centos systemd[1]: Started Apache Tomcat 10 Servlet container.
				
			

Enable Tomcat Remote Login

First, you will need to create an admin user to access the Tomcat application. You can create it by editing the tomcat-users.xml file:

				
					nano /opt/tomcat/tomcat10/conf/tomcat-users.xml
				
			

Add the following lines before the last line:

				
					<role rolename="manager-gui"/>
    <user username="manager" password="managerpassword" roles="manager-gui" />
<role rolename="admin-gui"/>
	<user username="admin" password="adminpassword" roles="admin-gui"/>

				
			

Save and close the file when you are finished.

By default, Tomcat is configured to be accessed only from the localhost. So you will need to configure Tomcat remote login. To enable remote login for Application Manager, edit the context.xml file:

				
					nano /opt/tomcat/tomcat10/webapps/manager/META-INF/context.xml
				
			

Remove the following lines:

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

To enable remote login for Host Manager, edit the context.xml file:

				
					nano /opt/tomcat/tomcat10/webapps/host-manager/META-INF/context.xml
				
			

Remove the following lines:

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

Save and close the file when you are finished then restart the Tomcat service to apply the changes:

				
					systemctl restart tomcat
				
			

Configure Nginx as a Reverse Proxy for Tomcat

It is good to configure the Nginx as a reverse proxy to access the Apache Tomcat via port 80. First, install the Nginx package using the following command:

				
					dnf install nginx -y
				
			

Once the Nginx package is installed, start and enable the Nginx service using the following command:

				
					systemctl start nginx
systemctl enable nginx
				
			

Next, create an Nginx virtual host for Tomcat:

				
					nano /etc/nginx/conf.d/tomcat.conf
				
			

Add the following lines:

				
					server {
    listen       80;
    server_name  tomcat.example.com;

    access_log  /var/log/nginx/tomcat.access.log;
    error_log   /var/log/nginx/tomcat.error.log;


    location / {
      proxy_pass http://127.0.0.1:8080;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

				
			

Save and close the file, then verify the Nginx for any syntax error using the following command:

				
					nginx -t
				
			

You should see the following output:

				
					nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
				
			

Next, restart the Nginx service to apply the changes:

				
					systemctl restart nginx
				
			

You can also check the Nginx status using the following command:

				
					systemctl status nginx
				
			

You will get the following output:

				
					● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-05-19 15:55:42 UTC; 13s ago
  Process: 4657 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 4654 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 4652 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 4658 (nginx)
    Tasks: 3 (limit: 23696)
   Memory: 5.0M
   CGroup: /system.slice/nginx.service
           ├─4658 nginx: master process /usr/sbin/nginx
           ├─4659 nginx: worker process
           └─4660 nginx: worker process

May 19 15:55:42 centos systemd[1]: nginx.service: Succeeded.
May 19 15:55:42 centos systemd[1]: Stopped The nginx HTTP and reverse proxy server.
May 19 15:55:42 centos systemd[1]: Starting The nginx HTTP and reverse proxy server...
May 19 15:55:42 centos nginx[4654]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
May 19 15:55:42 centos nginx[4654]: nginx: configuration file /etc/nginx/nginx.conf test is successful
May 19 15:55:42 centos systemd[1]: Started The nginx HTTP and reverse proxy server.

				
			

Configure Firewall

Next, you will need to allow HTTP and HTTPS service through the firewalld. You can allow them with the following commands:

				
					firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
				
			

Next, reload the firewalld to apply the changes:

				
					firewall-cmd --reload
				
			

Access Tomcat Dashboard

1. Open your web browser and access the Apache Tomcat using the URL http://tomcat.example.com. You should see the Tomcat dashboard on the following page:

2. Click on the Manager App. You will be asked to provide an admin username and password as shown below:

3. Provide your admin username, password, and click on the Sign in button. You should see the Tomcat Application Manager dashboard on the following page:

4. Click on the Host Manager. You will be asked to provide an admin username and password as shown below:

5. Provide your admin username, password, and click on the Sign in button. You should see the Tomcat Host Manager dashboard on the following page:

Great effort. We have learned How to Install Apache Tomcat Server on CentOS Stream 9.

How to Install Apache Tomcat Server on CentOS Stream 9 Conclusion

This post shows you how to install Apache Tomcat on CentOS Stream 9. We also explained how to configure Nginx as a reverse proxy for Tomcat. Now, you can build your Java-based application and deploy it using the Apache Tomcat.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x