Sunday, March 3, 2013

How to run Tomcat on different port


By default, Tomcat listens on port 8080.

Tomcat configuration file is $CATALINA_HOME/conf/server.xml

To use a different port number, edit the port attribute value of Connector port in $CATALINA_HOME/conf/server.xml:

<!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />


How to find the $CATALINA_HOME directory?

# ps -ef | grep java
tomcat      1437       1  0 07:04 ?        00:00:07 /usr/java/jdk1.7.0_09/bin/java -Djava.util.logging.config.file=/opt/apache-tomcat-7.0.32/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/apache-tomcat-7.0.32/endorsed -classpath /opt/apache-tomcat-7.0.32/bin/bootstrap.jar:/opt/apache-tomcat-7.0.32/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-tomcat-7.0.32 -Dcatalina.home=/opt/apache-tomcat-7.0.32 -Djava.io.tmpdir=/opt/apache-tomcat-7.0.32/temp org.apache.catalina.startup.Bootstrap start

catalina.home=/opt/apache-tomcat-7.0.32 gives the $CATALINA_HOME

$CATALINA_HOME/conf/server.xml = /opt/apache-tomcat-7.0.32/conf/server.xml

Change the port number for Tomcat

Change the port number to 9080 by editing "Connector port" value in $CATALINA_HOME/conf/server.xml, as follows

<Connector port="9080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />


# service tomcat stop
Using CATALINA_BASE:   /opt/apache-tomcat-7.0.32
Using CATALINA_HOME:   /opt/apache-tomcat-7.0.32
Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.32/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_09
Using CLASSPATH:       /opt/apache-tomcat-7.0.32/bin/bootstrap.jar:/opt/apache-tomcat-7.0.32/bin/tomcat-juli.jar

# service tomcat start
Using CATALINA_BASE:   /opt/apache-tomcat-7.0.32
Using CATALINA_HOME:   /opt/apache-tomcat-7.0.32
Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.32/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_09
Using CLASSPATH:       /opt/apache-tomcat-7.0.32/bin/bootstrap.jar:/opt/apache-tomcat-7.0.32/bin/tomcat-juli.jar

Verify if the port number has changed after restarting tomcat

# pgrep java
2667

# netstat -anp | grep java
tcp        0      0 :::8009                     :::*                        LISTEN      2667/java
tcp        0      0 :::9080                     :::*                        LISTEN      2667/java
unix  2      [ ]         STREAM     CONNECTED     22042  2667/java

Note : 8009 is the port number used by mod_jk module to connect Apache to Tomcat

# telnet localhost 9080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

# nc -v -z localhost 9080
Connection to localhost 9080 port [tcp/glrpc] succeeded! 

No comments:

Post a Comment