Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Tomcat port on-the-fly

Tags:

tomcat

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.

like image 585
Mark MacIver Avatar asked Jun 25 '09 20:06

Mark MacIver


People also ask

How do I find my Tomcat port number?

1) The default port number of Tomcat is 8080. On a server, if another application occupies port 8080, Tomcat will fail to be started normally, in which case you have to configure a new port for Tomcat. 2) If a number of Tomcats run on a server, then you have to configure a separate port for each Tomcat.

Can Tomcat run on port 443?

Tomcat can be configured to listen on SSL Port 443. Then you could turn off the SSL listener in the Apache Web server and use only Tomcat to handle your SSL connections. You can modify the Tomcat configuration by editing the file named "server. xml" in the Tomcat conf directory.


1 Answers

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.

like image 172
2 revs Avatar answered Oct 11 '22 05:10

2 revs