Installing Tomcat 10 in cPanel -Almalinux

The following steps require that the hosting account is already created. You must have SSH access enabled. It must be Normal Shell, the Jailed mode will not work.

First, you will need to log in via SSH using the cPanel account credentials. Do not use su or sudo.

Run this command to install ea-tomcat as a container:

/usr/local/cpanel/scripts/ea-podman install ea-tomcat101

This will create the application directory at: /home/cpanelUser/ea-podman.d/container-name E.g.: /home/tomcattest/ea-podman.d/ea-tomcat101.tomcattest.01 Running any of the following commands will give you the container name:

/usr/local/cpanel/scripts/ea-podman containers /usr/local/cpanel/scripts/ea-podman registered

[tomcattest]$ /usr/local/cpanel/scripts/ea-podman containers
{
   "ea-tomcat101.tomcattest.01" : {
      "container_name" : "ea-tomcat101.tomcattest.01",
      "image" : "tomcat:10.1.24",
      "pkg" : "ea-tomcat101",
      "pkg_version" : "10.1.24-1",
      "user" : "tomcattest"
   }
}

Running the following command will retrieve the container name and its ports:

/usr/local/cpanel/scripts/ea-podman running

[tomcattest]$ /usr/local/cpanel/scripts/ea-podman running
{
   "ea-tomcat101.tomcattest.01" : {
      "image" : "docker.io/library/tomcat:10.1.24\n",
      "ports" : [
         "10000",
         "10001"
      ]
   }
}

For example, a container can be named: ea-tomcat101.myCpanelUser.01 You will need to use that name to run any Tomcat commands for this user.

Within the directory /home/cpanelUser/ea-podman.d/container-name the most important directories are:

/conf Where the server.xml file and other configuration files are located. /logs Where the logs are stored. /webapps/ROOT Where the Java application files should be uploaded.\

To verify if Tomcat is working, you can create an index.jsp file inside the directory /webapps/ROOT mentioned earlier, and add the following content:

<html><body>
<% out.println("Hello world! -- From JSP"); %>
</body></html>

Next, you should add the following code in /conf/server.xml in the section where it appears:

<Host name="localhost" appBase="webapps" unpackWARs="false" autoDeploy="false" deployOnStartup="false" deployXML="false">

Add:

<Context path="" docBase="ROOT">
         <!-- Default set of monitored resources -->
         <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>

Next, you need to restart the Tomcat container:

/usr/local/cpanel/scripts/ea-podman restart container-name E.g.: /usr/local/cpanel/scripts/ea-podman restart ea-tomcat101.tomcattest.01

It's important to know which ports were assigned to the installed Tomcat container. You can check this by running: /usr/local/cpanel/scripts/ea-podman running

The first port is the HTTP port and the second port is the AJP port.

At this point, you should be able to see the content of the index.jsp file we created earlier by entering this web address in the browser: http://Server-IP:TomcatHTTP-Port/index.jsp

E.g.: http://69.61.102.92:10000/index.jsp

Steps to view Java applications from domain-based web addresses.

You need to log in to the server via SSH, now as root:

You should create these two directories, where /user/domain should be replaced by the cPanel user and its domain.

mkdir -p /etc/apache2/conf.d/userdata/std/2_4/user/domain/
mkdir -p /etc/apache2/conf.d/userdata/ssl/2_4/user/domain/

Then create the custom.conf file in the first created directory (be careful with /user/domain):

nano /etc/apache2/conf.d/userdata/std/2_4/user/domain/custom.conf

In this file, you should add the following proxy code, replacing 10001 with the second port returned by the command: /usr/local/cpanel/scripts/ea-podman running

<IfModule proxy_ajp_module>
    ProxyPassMatch "/.well-known/(.*)" "!"
    ProxyPassMatch "/(.*).jsp" "ajp://localhost:10001"
    ProxyPassMatch "/servlets/(.*)" "ajp://localhost:10001"
</IfModule>

Next, create a symlink from this file to the second path created above. Remember to replace /user/domain with the cPanel user and its domain.

ln -s /etc/apache2/conf.d/userdata/std/2_4/user/domain/custom.conf /etc/apache2/conf.d/userdata/ssl/2_4/user/domain/custom.conf

Once done, you need to recompile the httpd.conf file by running this command:

/usr/local/cpanel/scripts/rebuildhttpdconf

It should not show any errors. If you encounter an error, it might be in the code of the custom.conf file we created.

Finally, restart Apache:

service httpd restart

You should now be able to view the index.jsp using the normal domain: http://domain/index.jsp

Automatic redirection to HTTPS

First, you need to install SSL on the account using AutoSSL, as usual.

If the domain works with https://domain then you can proceed with automatic redirection.

You will need to access the server.xml file in the conf directory of Tomcat:

cpanelUser/ea-podman.d/container-name/conf

In the server.xml file, locate the line:

<Connector port="8080" protocol="HTTP/1.1"

In that line, you should add: redirectPort="443"

The line should look like the image below:

Next, add this code in the web.xml file of the application path at: /user/ea-podman.d/container-name/webapps/ROOT/WEB-INF

<security-constraint>
 <web-resource-collection>
 <web-resource-name>Entire Application</web-resource-name>
 <url-pattern>/*</url-pattern>
 </web-resource-collection>
 <user-data-constraint>
 <transport-guarantee>CONFIDENTIAL</transport-guarantee>
 </user-data-constraint>
</security-constraint>

Finally, restart the Tomcat container for the user:

/usr/local/cpanel/scripts/ea-podman restart container-name

If you test the domain or application without https, it should automatically redirect to the https version.

That's all regarding the installation of Tomcat itself.

Useful Commands:

This command, executed as the cPanel user, will delete the installed Tomcat container: Replace container-name with the installed one.

/usr/local/cpanel/scripts/ea-podman uninstall container-name --verify

These commands will display the Status, start, restart, or stop the Tomcat container:

/usr/local/cpanel/scripts/ea-podman status container-name

/usr/local/cpanel/scripts/ea-podman start container-name

/usr/local/cpanel/scripts/ea-podman restart container-name

/usr/local/cpanel/scripts/ea-podman stop container-name

With this command, you can run certain commands as root quickly:

/usr/local/cpanel/scripts/ea-podman bash container-name

How to modify Tomcat variables (such as memory xms/xmx)

Connect via SSH as the cPanel user.

Run this command to edit files as root temporarily:

/usr/local/cpanel/scripts/ea-podman bash container-name

You will need to install vim, vi, nano, or any file editor:

apt update

apt install vim

You should be in /user/local/tomcat. Go into the bin subdirectory and edit the catalina.sh file using vim or the installed editor:

Add the following line below the commented box:

CATALINA_OPTS="-Xms512m -Xmx1024m"

The limits in the example can be adjusted as needed.

After saving the change in the catalina.sh file, exit the temporary root mode with:

exit

Restart the container, making sure you are back in cpanel user mode:

/usr/local/cpanel/scripts/ea-podman restart container-name

Check the Tomcat status:

/usr/local/cpanel/scripts/ea-podman status container-name

Then, check that the change took effect with the command:

ps aux |grep tomcat |grep Xmx

Last updated