TT Lab
Get started
Learn Learning paths Courses

Tomcat & nginx Operations

Starting Tomcat and Learning Its Structure

Continue in TT Lab

Goal

You start and stop Tomcat without systemd, grasp the directory structure and listening ports, and inject JVM options with setenv.sh and verify from the process that they were actually applied.

Why it matters

Servers at SI sites often run from a tar.gz provided by a vendor, and container environments have no systemd. So the ability to handle catalina.sh directly becomes a basic skill. There is one more thing — a "state where the process is up but the port is not open" really exists. It happens with port conflicts or connector initialization failures. The habit of confirming startup not by the process but by the log and the port gets built into your hands in this lab.

Steps

  1. Create the /root/tc directory and save the output of java -version to /root/tc/java.txt. (It must include standard error.)
  2. Create /root/tc/layout.csv. The first line is dir,purpose. There must be seven rows, bin, conf, lib, logs, webapps, work and temp, and each purpose must be 8 or more characters.
  3. Start Tomcat with /opt/tomcat/bin/catalina.sh start. http://127.0.0.1:8080/ must return HTTP 200.
  4. Find the startup time (in milliseconds) in /opt/tomcat/logs/catalina.out and save the number only to /root/tc/startup.txt.
  5. Among the ports currently in LISTEN, save the ports opened by Tomcat to /root/tc/ports.txt, one per line in ascending order.
  6. Create /opt/tomcat/webapps/hello/index.html and put the string LabHub in the body. http://127.0.0.1:8080/hello/ must return 200 and the body must contain LabHub.
  7. Stop Tomcat and start it again. (catalina.sh stop 30 -force, then catalina.sh start) After the restart, http://127.0.0.1:8080/hello/ must still return 200.
  8. Create /opt/tomcat/bin/setenv.sh, define the options below as CATALINA_OPTS, and restart. -Xms256m -Xmx256m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/root/tc/dump Create the /root/tc/dump directory in advance too. You pass only if these options are actually visible in the arguments of the running Java process.

Notes

Check the JDK version

Create the /root/tc directory and save the output of java -version to /root/tc/java.txt. (It must include standard error.)

The output of java -version goes to standard error. To save it to a file, be careful with the redirection.

Summarize directory roles

Create /root/tc/layout.csv. The first line is dir,purpose. There must be seven rows, bin, conf, lib, logs, webapps, work and temp, and each purpose must be 8 or more characters.

Write in one line what each directory holds. Try to distinguish the difference between work and temp, and between lib and webapps/*/WEB-INF/lib.

Start Tomcat

Start Tomcat with /opt/tomcat/bin/catalina.sh start. http://127.0.0.1:8080/ must return HTTP 200.

systemctl is not in this environment. Run the startup script under bin directly. Startup is asynchronous, so running curl right away can fail.

Check the startup time

Find the startup time (in milliseconds) in /opt/tomcat/logs/catalina.out and save the number only to /root/tc/startup.txt.

catalina.out has a line of the form 'Server startup in [N] milliseconds'. You need to extract only the number, so put sed or awk after grep.

Check the listening ports

Among the ports currently in LISTEN, save the ports opened by Tomcat to /root/tc/ports.txt, one per line in ascending order.

Look at the ports currently in the LISTEN state with ss -ltn. Besides the service port, one more should be open, and what that is is the key to this step.

Deploy a static context

Create /opt/tomcat/webapps/hello/index.html and put the string LabHub in the body. http://127.0.0.1:8080/hello/ must return 200 and the body must contain LabHub.

If you create a directory under webapps, its name becomes the context path. Deployment works even without a WAR.

Stop and restart

Stop Tomcat and start it again. (catalina.sh stop 30 -force, then catalina.sh start) After the restart, http://127.0.0.1:8080/hello/ must still return 200.

Get into the habit of giving the stop script a wait time and -force. You can check whether the restart actually happened from the number of startup log lines in catalina.out.

Inject JVM options with setenv.sh

Create /opt/tomcat/bin/setenv.sh, define the options below as CATALINA_OPTS, and restart. -Xms256m -Xmx256m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/root/tc/dump Create the /root/tc/dump directory in advance too. You pass only if these options are actually visible in the arguments of the running Java process.

If you edit catalina.sh directly, it disappears when Tomcat is upgraded. Create bin/setenv.sh and define CATALINA_OPTS; a restart is needed for it to take effect. Check whether it actually took effect from the process's launch arguments.