Change Tomcat port on the fly

I would like to save the Tomcat v5.5 port in an environment variable and listen to Tomcat on that port. So far, the only way I can change the port is to change $ CATALINA_HOME / conf / server.xml. Is there a way to set the port value by supplying an external value when starting Tomcat? I am running Tomcat on Solaris.

+4
source share
3 answers

Create a script to run Tomcat. At the start of the script, export JAVA_OPTS to specify a value for the Tomcat port.http.nonssl (note that you can call this property no matter what you want).

 export JAVA_OPTS=-Dport.http.nonssl=${CATALINA_BASE_PORT} 

As you can see, I set port.http.nonssl to the environment variable ${CATALINA_BASE_PORT}

Then the script starts Tomcat:

 $CATALINA_HOME/bin/startup.sh 

Now you need to modify the Tomcat $CATALINA_HOME/conf/server.xml file so that the non-SSL HTTP connector uses the port.http.nonssl property instead of the 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 environment variable $ {CATALINA_BASE_PORT} whenever you start it with a new start script.

+17
source

Just to follow Mark Makiver’s first answer, here is my start_solr.bat file, which indicates the port and 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 ie 9001] goto end :end cd "%CURRENT_DIR%" 
+1
source

This is a bit like using a sledgehammer to crack a nut, but you can use Tomcat built into JBoss, which has a single mechanism for replacing system properties in arbitrary configuration files, including tomcat server.xml. An environment variable can be passed as a system property when the script is run (using -D).

JBoss can be divided into not much more than its core and tomcat, so you will not need to run the whole match. But it will still be significantly harder than a standalone Tomcat.

0
source

Source: https://habr.com/ru/post/1286773/


All Articles