Published on Mar 1 2023 in Control Panels Java Tomcat

Control Web Panel aka CWP users can use Java/Tomcat in many ways (not only via the built-inb CWP Tomcat engine that runs under a dedicated user). Each user can have their own application server and JVM.

This article provides a guide on setting up multiple domains with Java support in the Control Web Panel (CWP). Users can employ Java/Tomcat in various ways, allowing each user to have their own application server. The article explores three likely scenarios for multiple domains and Java applications, detailing the steps for implementation. Scenario ‘a’ is particularly outlined, illustrating how to configure one account with one or more domains and a Tomcat server. The guide covers domain pointing, SSL certificate renewal, web server configuration, Tomcat webapps directory creation, server.xml modifications, and proxy setup. Scenario ‘b’ is also introduced for users requiring separate web applications and restricted user access, with detailed steps for each user setup.

For multiple domains and multiple Java applications there are 3 most likely scenarios:

  1. One account with one or more domains and one Tomcat

  2. Multiple accounts where each of them is hosting one or more domains and a Tomcat

  3. A mix of above

Scenario ‘a’: Setting up one account with multiple domains and a Tomcat server

We will use user tomcat and domain1.com, domain2.com as the example domains.

  1. Begin by pointing the domains (domain1.com and domain2.com) to your server IP using the domain registrar’s DNS service or Cloudflare DNS service.

  2. Utilize CWP to renew SSL certificates for the domains under Webserver Settings > SSL Certificates > AutoSSL [Free].

  3. Using CWP - Webserver Settings > Webserver Conf Editor > /usr/local/apache/conf.d/vhosts/ > /usr/local/apache/conf.d/vhosts/domain1.com.conf uncomment # IncludeOptional ... line. Repeat the same for /usr/local/apache/conf.d/vhosts/domain1.com.ssl.conf. Repeat these 2 edits for additional domains if necessary.

  4. Create tomcat webapps directories for the 2 domains i.e.

/home/tomcat/apache-tomcat-9.0.63/webapps_domain1
/home/tomcat/apache-tomcat-9.0.63/webapps_domain2

You can do it using SSH (as user tomcat) and mkdir command or using Midnight Commander file manager (mc command) in SSH or web-based terminal. These directories must be owned by tomcat user. If you created them as root do not forget to run su - tomcat -c 'chown -R tomcat: $CATALINA_HOME'.

Upload your web applications renamed to ROOT.war to respective directories.

  1. Also as user tomcat, edit /home/tomcat/apache-tomcat-9.0.63/conf/server.xml (you can use F4 in Midnight Commander or an editor of your choice) and add 2 Host entries, e.g.:
<Host name="domain1.com" autoDeploy="true" appBase="webapps_domain1" unpackWARs="true">
<Alias>www.domain1.com</Alias>;
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
    prefix="domain1.com_access_log." suffix=".txt"
    pattern="%h %l %u %t &quot;%r&quot; %s %b" resolveHosts="false"/>
</Host>

For domain2.com the snippet is the same, just update domain name in 4 places.

  1. Restart Tomcat (as user tomcat) with sudo systemctl restart java-tomcat or use whatever start/stop mechanism is used on your server (including vanilla shutdown.sh/startup.sh scripts).

  2. Verify tomcat log /home/tomcat/apache-tomcat-9.0.63/logs/catalina.out. It can take several seconds for the ‘Server started’ message to appear the the log.

  3. Set up proxy that will work for both HTTP and HTTPS connections and will allow you to skip Tomcat port number from URL:

mkdir -p /usr/local/apache/conf/userdata/tomcat/domain1.com
cat > /usr/local/apache/conf/userdata/tomcat/domain1.com/proxy.conf<<EOF
ProxyPreserveHost On
ProxyPass / ajp://127.0.0.1:8009/ retry=5
ProxyPassReverse / ajp://127.0.0.1:8009/
EOF

Finally restart webserver (frontend) with systemctl restart httpd.

Scenario ‘b’: Setting up multiple accounts, each hosting one or more domains and a Tomcat server

If you need to separate web applications so that each user has only access to his application use this scenario. It will also prevent misbehaving application of domain1 to impact functioning of domain2 application. Follow above steps for a single user say ‘tomcat1’ and his domain or domains. Then do the same for next user say ‘tomcat2’. Do not forget to update ports in Tomcat’s server.xml and proxy.conf so that tomcat2 ports do not conflict with the ones of tomcat1.