Tomcat : How to start Tomcat on more than one Port.

How to start Tomcat on more than one Port.

Tomcat settings can be found in TomcatFolder/conf/server.xml file.

  1. Root element of the configuration is Server tag.
  2. You can add multiple Service under Server tag for running tomcat on more than one port.

Default Service Configuration should look as follows

  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>

The above configurations states the folllowing things

  1. Service name="Catalina" - Service name is Catalina, you can change it to give your application name.
  2. Connector port="8080" protocol="HTTP/1.1" - Tomcat will run on port 8080
  3. connectionTimeout="20000" - Connection timeout in 20000 mili seconds
  4. Connector port="8009" protocol="AJP/1.3" - AJP connection on port 8009.
  5. name="localhost" - Name of the host where tomcat will run.
  6. appBase="webapp" - Base directory for deployed apps is webapp
  7. unpackWARs="true" - The war files deplyed wll be unpacked

Running Tomcat on multiple ports.

To start tomcat on more than one port you can add multiple Service blocks under Server root tag as shown below

  <Service name="app1">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="app1"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="app1_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
  <Service name="app2">
    <Connector port="8090" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8091" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="app2"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="app2_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>

Here in above configuration we have specified the following.

  1. Service app1 configuration will run on port 8080.
  2. Service app2 configuration will run on port 8090.
  3. app1 will keep files and war files in folder app1.
  4. app2 will keep files and war files in folder app2.
  5. app1 and app2 will write access logs in folder logs/app1_access_log.txt and logs/app2_access_log.txt respectively

Note : None of the above configuration will keep files or war files in webapp folder.

Also Read

No comments :

Post a Comment