In this tutorial, we want to teach you to Install and Configure Apache Tomcat on Rocky Linux 8.
Apache Tomcat is a Java Servlet container, or web container, that provides the extended functionality to interact with Java Servlets, while also implementing several technical specifications of the Java platform: JavaServer Pages (JSP), Java Expression Language (Java EL), and WebSocket.
This software enables a web server to handle dynamic Java-based web content using the HTTP protocol. JSP is a similar technology that allows developers to create dynamic content using HTML or XML documents. In terms of their ability to enable dynamic content, Java Servlets and JSP are broadly comparable to PHP or ASP.NET, based on the Java programming language.
By bringing all these Java-based technologies together, Tomcat Apache offers a “pure Java” web server environment for running applications built on the Java programming language.
Install and Configure Apache Tomcat on Rocky Linux 8
To complete this guide, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide the Initial Server Setup with Rocky Linux 8.
Installing Java on Rocky Linux 8
Tomcat is a Java Servlet container, so you must have Java installed on your server. First, update your local package index with the following command:
sudo dnf update
Then, install OpenJDK on your server with the following command:
sudo dnf install java-11-openjdk.x86_64
You can verify your Java installation by checking its version:
java --version
In your output you will see:
Output
openjdk 11.0.13 2021-10-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.13+8-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.13+8-LTS, mixed mode, sharing)
When your Java installation is finished, you must create a Tomcat group and user that will have access to Tomcat only.
To create a Tomcat group, run the following command:
sudo groupadd tomcat
Now create a directory to save the Tomcat files with the following command:
sudo mkdir /opt/tomcat
Add the user in the above directory and disable its login rights with the following command:
sudo useradd -s /bin/nologin -g tomcat -d /opt/tomcat tomcat
when you are done, follow the steps below to install Tomcat on your server.
Set up Tomcat on Rocky Linux 8
At this point, you must visit the Apache Tomcat Downloads page and copy the tar.gz binary distributions link address.
Then, use the wget command to download the Tomcat 10, paste your copied link in the below command:
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.13/bin/apache-tomcat-10.0.13.tar.gz
When your download is completed, extract your file and copy it into the /opt/tomcat directory with the following command:
sudo tar -zxvf apache-tomcat-*.tar.gz -C /opt/tomcat --strip-components=1
Now you need to set the correct permissions for the Tomcat user with the following command:
sudo chown -R tomcat:tomcat /opt/tomcat
Also, you need to allow the script available inside the folder to execute with the command below:
sudo sh -c 'chmod +x /opt/tomcat/bin/*.sh'
Here you should create a systemd unit file for Tomcat. Create the file with your favorite text editor, here we use vi:
sudo vi /etc/systemd/system/tomcat.service
Then, paste the following content to the file:
[Unit] Description=Tomcat webs servlet container After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/jre" Environment="JAVA_OPTS=-Djava.awt.headless=true -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.
At this point, you can start and enable your Tomcat service on Rocky Linux 8 with the following commands:
$ sudo systemctl start tomcat $ sudo systemctl enable tomcat
Verify that your Tomcat service is active and running on your server with the command below:
sudo systemctl status tomcat
In your output you should see:
Output tomcat.service - Tomcat webs servlet container Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: > Active: active (running) since Tue 2021-12-07 07:36:33 EST; 13s ago Main PID: 89736 (java) Tasks: 19 (limit: 11409) Memory: 126.5M CGroup: /system.slice/tomcat.service └─89736 /usr/lib/jvm/jre/bin/java -Djava.util.logging.config.file=/o>
sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
Reload the firewall to apply the new rules:
sudo firewall-cmd --reload
Now you can access the Apache Tomcat web interface on Rocky Linux 8.
Access Apache Tomcat web Interface
In your web browser type your server’s IP address followed by 8080:
http://server-IP-adress:8080
You will see:
If you want to access the “Server Status“, “Manager App” and “Host Manager”, you should follow the steps below.
First, you need to add a username and password to the Tomcat User XML file on Rocky Linux 8. Open the file with your favorite text editor, here we use vi:
sudo vi /opt/tomcat/conf/tomcat-users.xml
At the end of the file before </tomcat-users> paste the following contents:
<role rolename="admin"/> <role rolename="admin-gui"/> <role rolename="manager"/> <role rolename="manager-gui"/> <user username="user" password="pwd" roles="admin,admin-gui,manager,manager-gui"/>
Remember to replace the username and password with your own.
When you are done, save and close the file.
Second, edit the manager context file:
sudo vi /opt/tomcat/webapps/manager/META-INF/context.xml
Comment the following line as shown below:
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
When you are finished, save and close the file.
Next, you need to edit the host manager context file:
sudo vi /opt/tomcat/webapps/host-manager/META-INF/context.xml
Again, comment on the following line as above:
<!-- <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 done.
To apply the changes, restart Tomcat on Rocky Linux 8:
sudo systemctl restart tomcat
Now you can refresh your Tomcat web interface and click on the Manager app. You need to enter the username and password that you have defined in the XML file.
Note: Tomcat Server isn’t particularly efficient as a traditional HTTP server, so Apache is a much better choice for dynamic websites built solely with a language like PHP. But of course, when it comes to sites based entirely on JSP, Tomcat is the indisputable leader of the gang.
Conclusion
At this point, you learn to Install and Configure Apache Tomcat on Rocky Linux 8.
Hope you enjoy it.
You may be interested in these articles: