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.
This is a bit like using a sledgehammer to crack a nut, but you could use the Tomcat that's embedded in JBoss, which has a unified mechanism for substituting system properties in arbitrary config files, including the tomcat server.xml. Your environment variable can be passed through as a system property in the startup script (using -D).
JBoss can be stripped down to not much more than its kernel and tomcat, so you wouldn't need to run the whole shooting match. But it would still be considerably heavier than standalone Tomcat.
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.
Just to follow up on the first answer by Mark MacIver, here is my start_solr.bat file that specifies a port and a environment variable:
if "%1" == "" goto displayUsage
set JAVA_OPTS=%JAVA_OPTS% -Dport.http.nonssl=%1 -Dsolr.solr.home=../../
call startup.bat
goto end
:displayUsage
echo.
echo Usage: start_solr.bat [port i.e. 9001]
goto end
:end
cd "%CURRENT_DIR%"