Running multiple instances of tomcat on one machine (avoiding port conflicts)

In tomcat ports are specified in conf/server.xml:

bawi@hp:~/workspace/apache-tomcat-6.0.26$ cat conf/server.xml | grep port=\"
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1"
port="8080" protocol="HTTP/1.1"
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

All ports match pattern 8xxx, so theoretically it is possible to define easily up to 65 different tomcat instances (from 1xxx to 65xxx). However, in order not to conflict with other ports on the system, I recommend defining 6 non-conflicting instances like 8080, 18080, …., 58080 for the base 8080.

An easy solution is to define tomcat.port.prefix variable in bin/setenv.sh, set it to 1, …, 5 for 18080, …, 58080 respectively, i.e.:

JAVA_OPTS=$JAVA_OPTS" -Dtomcat.port.prefix=1"

and update the server.xml to get the result:

bawi@hp:~/workspace/apache-tomcat-6.0.26$ cat conf/server.xml | grep port=\"
<Server port="${tomcat.port.prefix}8005" shutdown="SHUTDOWN">
<Connector port="${tomcat.port.prefix}8080" protocol="HTTP/1.1"
port="${tomcat.port.prefix}8080" protocol="HTTP/1.1"
<Connector port="${tomcat.port.prefix}8443" protocol="HTTP/1.1" SSLEnabled="true"
<Connector port="${tomcat.port.prefix}8009" protocol="AJP/1.3" redirectPort="${tomcat.port.prefix}8443" />

You can do it via command:

sed -i 's/ort=\"/ort=\"${tomcat.port.prefix}/g' conf/server.xml
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s