Starting Tomcat and Learning Its Structure
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
- Create the
/root/tcdirectory and save the output ofjava -versionto/root/tc/java.txt. (It must include standard error.) - Create
/root/tc/layout.csv. The first line isdir,purpose. There must be seven rows,bin,conf,lib,logs,webapps,workandtemp, and eachpurposemust be 8 or more characters. - Start Tomcat with
/opt/tomcat/bin/catalina.sh start.http://127.0.0.1:8080/must return HTTP 200. - Find the startup time (in milliseconds) in
/opt/tomcat/logs/catalina.outand save the number only to/root/tc/startup.txt. - Among the ports currently in LISTEN, save the ports opened by Tomcat to
/root/tc/ports.txt, one per line in ascending order. - Create
/opt/tomcat/webapps/hello/index.htmland put the stringLabHubin the body.http://127.0.0.1:8080/hello/must return 200 and the body must containLabHub. - Stop Tomcat and start it again.
(
catalina.sh stop 30 -force, thencatalina.sh start) After the restart,http://127.0.0.1:8080/hello/must still return 200. - Create
/opt/tomcat/bin/setenv.sh, define the options below asCATALINA_OPTS, and restart.-Xms256m -Xmx256m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/root/tc/dumpCreate the/root/tc/dumpdirectory in advance too. You pass only if these options are actually visible in the arguments of the running Java process.
Notes
java -version 2> /root/tc/java.txt— the version information goes out on standard error.- Look at the LISTEN ports with
ss -ltn. Extract only the port withgrepandawk -F:. - To check the running JVM arguments:
ps -ef | grep javaortr '\0' ' ' < /proc/<PID>/cmdline - Common mistake 1: running
curlright after startup and failing. Wait a few seconds or retry. - Common mistake 2: creating
setenv.shbut not restarting, so the options are not applied. - Common mistake 3: in
setenv.sh, whereCATALINA_OPTS=belongs, usingJAVA_OPTS=. Both work, but what is also applied atstopisJAVA_OPTS;CATALINA_OPTS, unlike it, applies only at startup. For heap options,CATALINA_OPTSis correct.
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.