Change Tomcat port on-the-fly
tomcat
Solution
Create a script to launch Tomcat. In the launch script, export JAVA_OPTS to specify a value for the Tomcat property `port.http.nonssl` (note you can call this property whatever you want).
export JAVA_OPTS=-Dport.http.nonssl=${CATALINA_BASE_PORT}
As you can see, I've set `port.http.nonssl` to the environment variable `${CATALINA_BASE_PORT}`
The script then launches Tomcat:
$CATALINA_HOME/bin/startup.sh
You now need to change the Tomcat `$CATALINA_HOME/conf/server.xml` file so the non-SSL HTTP connector uses the `port.http.nonssl` property instead of a hardcoded value.
<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="${port.http.nonssl}" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
Now Tomcat will use the port defined in the ${CATALINA_BASE_PORT} environment variable whenever you start it via the new launch script.
Problem
I'd like to store the Tomcat v5.5 port in an environment variable and have Tomcat listen on that port. So far the only way I can see to change the port is by amending $CATALINA_HOME/conf/server.xml. Is there a way to set the port value by supplying an external value when starting Tomcat? I'm running Tomcat on Solaris.