Apache Tomcat is an open-source web server used for deploying Java-based web applications. It is platform-independent and allows you to runs the JavaServer (JSP), JavaServlet, and Java Expression languages. Apache Tomcat enables a webserver to handle dynamic Java-based web content using the HTTP protocol.

In this post, we will explain how to install Apache Tomcat 10 on Rocky Linux 8.

Step 1 – Install Java

Apache Tomcat is a Java-based application, so Java must be installed on your server. If not installed, you can install it using the following command:

dnf install java-11-openjdk -y

After installing Java, verify the Java version with the following command:

java -version

Sample output:

openjdk version "11.0.12" 2021-07-20 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.12+7-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.12+7-LTS, mixed mode, sharing)

Step 2 – Download Apache Tomcat 10

Before starting, you will need to add a dedicated user for Tomcat. You can add it using the following command:

useradd -r -d /opt/tomcat/ -s /bin/false -c "Tomcat User" tomcat

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

wget https://downloads.apache.org/tomcat/tomcat-10/v10.0.10/bin/apache-tomcat-10.0.10.tar.gz

After downloading Apache Tomcat, create a directory for Tomcat and extract the downloaded file inside the /opt/tomcat directory:

mkdir /opt/tomcat
tar xzf apache-tomcat-10.0.10.tar.gz -C /opt/tomcat --strip-components=1

Next, change the ownership of /opt/tomcat directory to tomcat:

chown -R tomcat: /opt/tomcat/

Step 3 – Configure Tomcat Admin User

Next, you will need to create an admin user for managing Manager and Host Manager. You can do it by editing the /opt/tomcat/conf/tomcat-users.xml file:

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

Add the following lines just above the last line:

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

Save and close the file when you are finished.

Step 4 – Configure Tomcat for Remote Host

By default, Tomcat can be accessed only from the localhost, so you will need to configure Tomcat to access it from the remote host.

To access the Manager from the remote host, edit the context.xml file:

nano /opt/tomcat/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" />

Save and close the file when you are finished.

To access the Host Manager from the remote host, edit the context.xml file:

nano /opt/tomcat/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.

Step 5 – Create a Systemd Unit File for Apache Tomcat

Next, it is recommended that you create a systemd unit file to manage the Tomcat service. You can create it with the following command:

nano /etc/systemd/system/tomcat.service

Add the following lines:

[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

Save and close the file, then reload the systemd daemon with the following command:

systemctl daemon-reload

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

systemctl start tomcat
systemctl enable tomcat

You can verify the status of the Tomcat service with the following command:

systemctl status tomcat

Sample output:

● tomcat.service - Apache Tomcat Server
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-08-08 09:35:36 UTC; 20s ago
  Process: 16830 ExecStart=/opt/tomcat/bin/catalina.sh start (code=exited, status=0/SUCCESS)
 Main PID: 16841 (java)
    Tasks: 24 (limit: 23695)
   Memory: 99.6M
   CGroup: /system.slice/tomcat.service
           └─16841 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.ju>

Aug 08 09:35:36 RockyLinux8 systemd[1]: Starting Apache Tomcat Server...
Aug 08 09:35:36 RockyLinux8 systemd[1]: Started Apache Tomcat Server.

Step 6 – Access Apache Tomcat Web UI

At this point, Apache Tomcat is started and listening on port 8080. You can check it with the following command:

ss -antpl | grep 8080

Sample output:

LISTEN 0      100                     *:8080            *:*    users:(("java",pid=16841,fd=43))                                                                                                    

You can now access Tomcat using the URL http://your-server-ip:8080. You should see the following screen:
Access Tomcat
To access the Manager App, click on the Manager App button. You will be asked to provide an admin user and password as shown below:
Tomcat Login Page
Provide your admin username and password and click on the Sign in button. You should see the following screen:
Tomcat Manager Page
To access the Host Manager App, click on the Host Manager link. You should see the following screen:
Tomcat Host Manager Page

Conclusion

Congratulations! You have successfully installed Apache Tomcat 10 on Rocky Linux 8. You can now deploy your Java application easily with Apache Tomcat on your Atlantic.Net dedicated server hosting account.