tags:

views:

474

answers:

3

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.

A: 

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.

skaffman
Hmm, I think you're right about the sledgehammer - but maybe I could take that idea and wrap the Tomcat startup.sh script in another script and replace the port in server.xml with my environment variable before executing startup.sh.
Mark MacIver
+2  A: 

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.

Mark MacIver
One step better (especially if uses multiple instances with CATALINA_BASE)Create a file "setenv.sh" add the JAVA_OPTS line and place it in the instance's bin directory.Either CATALINA_HOME/bin/setenv.sh or CATALINA_BASE/bin/setenv.sh
willCode4Beer
Here is an example of my start_solr.bat that passes in a port and another variable! Awesome fix: REM This guy handles specifying the port number to startup on.REM We don't assume that we know what port to run on.if "%1" == "" goto displayUsageset JAVA_OPTS=%JAVA_OPTS% -Dport.http.nonssl=%1 -Dsolr.solr.home=../../call startup.batgoto end:displayUsageecho.echo Usage: start_solr.bat [port i.e. 9001]goto end:endcd "%CURRENT_DIR%"
Eric Pugh
A: 

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%"
Eric Pugh