How to Run Multiple Instances of Tomcat on a Single Server
Sometimes you want to run multiple instance of Tomcat in same single GNU/Linux machine. Each will use the same binary, but turn to different areas for data.
In this guide I'll run Tomcat on Linux machine and create two new Tomcat instances - one to run App1 and second for App2. Regardless of which GNU/Linux distribution you use or which Java applications you run in Tomcat, the principles are the same as those shown here.
Tomcat relies on settings defined in certain environment variables. To
run multiple instances, you have to understand the role of two variables
in particular:
$CATALINA_HOME represents the root of the Tomcat.
$CATALINA_BASE is used when Tomcat is configured to run multiple instances. If defined, Tomcat calculates all relative references for files in the following directories on the basis of the value set for CATALINA_BASE instead of CATALINA_HOME:
By default on $CATALINA_BASE has no value, so all the directories are related to $CATALINA_HOME. We want to change this, and to do so we'll work with the script that starts and stops Tomcat, but first, let's create the Tomcat directories that we want to use to store the information from our Tomcats.
Step SSL certificate creation/generation
Note: Create folder at any location; I created under /home/hussain folder named keytool_fin
e.g.: /home/hussain/keytool_fin
Using terminal making folder under above mention folder
1- mkdir keytool_fin
2- cd keytool_fin
Generation of SSL in keytool_fin folder
3- keytool -genkey -alias cb2 -keyalg RSA -keystore server3.keystore -keysize 2048
4- keytool -export -alias cb2 -keystore server3.keystore -file server3.cer
5- sudo keytool -import -alias cb2 -file server3.cer -keystore $JAVA_HOME/jre/lib/security/cacerts
note after this it ask your machine root password then ask you password just type changeit
Step Tomcat configurations
1- Download tomcat from here
2- Uncompress it and copy into two different folders like name tomcate_1 and tomcate_2
(e.g.: tomcate_poral, tomcate_rule)
For tomcat_1
3- Go to tomcat_1/bin folder and open startup.sh file and past below lines in it
export BASEDIR=/home/hussain/tomcat_1
export CATALINA_BASE=/home/hussain/tomcat_1
export CATALINA_HOME=/home/hussain/tomcat_1
4- Go to tomcate_1/bin folder and open shutdown.sh file and past above lines in it.
5- Then go to tomcat_1/conf folder and open server.xml file and change ports
change from 8005 to 8205 -- this is for shut-down port
no change 8080 port -- this is A "Connector" for endpoint by which requests are received
change from 8009 to 8209 -- this is for AJP 1.3 Connector port
and add these lines in it.
<Connector port="8443"
maxHttpHeaderSize="8192"
maxThreads="150"
minSpareThreads="25"
enableLookups="false"
disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="/home/hussain/keytool_fin/server3.keystore"
keystorePass="changeit" SSLEnabled="true"
protocol="org.apache.coyote.http11.Http11Protocol" />
For tomcat_2
6- Go to tomcat_2/bin folder and open startup.sh file and past below lines in it
export BASEDIR=/home/hussain/tomcat_2
export CATALINA_BASE=/home/hussain/tomcat_2
export CATALINA_HOME=/home/hussain/tomcat_2
7- Go to tomcate_2/bin folder and open shutdown.sh file and past above lines in it.
8- Go to tomcat_2/conf and open server.xml file and change ports
change from 8005 to 8105 -- this is for shut-down port
change from 8080 to 8686 -- this is A "Connector" for endpoint by which requests are received
change from 8009 to 8109 -- this is for AJP 1.3 Connector port
and add these lines in it.
<Connector port="8446"
maxHttpHeaderSize="8192"
maxThreads="150"
minSpareThreads="25"
enableLookups="false"
disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="/home/hussain/keytool_fin/server3.keystore"
keystorePass="changeit" SSLEnabled="true"
protocol="org.apache.coyote.http11.Http11Protocol" />
9- Then start the servers
For tomcat_1
tomcat_1/bin> ./startup.sh -Djavax.net.ssl.trustStore="/home/hussain/keytool_fin/server3.keystore"
For tomcat_2
tomcat_2/bin> ./startup.sh -Djavax.net.ssl.trustStore="/home/hussain/keytool_fin/server3.keystore"
10- then go to browser and type
For tomcat_1
localhost:8080 or https://localhost:8443 you should see tomcat home page.
For tomcat_2
localhost:8686 or https://localhost:8446 you should see tomcat home page.
11- Then deploy your war into both tomcat_1 and tomcat_2 webapps folder and restart both servers
_______________________________________________________________________________
$CATALINA_HOME represents the root of the Tomcat.
- lib contains all libraries used for Tomcat. Any library present here is visible to all Tomcat instances.
- bin contains shell scripts to start and stop Tomcat. You can create a modified version in /etc/init.d/ to start and stop the instances.
$CATALINA_BASE is used when Tomcat is configured to run multiple instances. If defined, Tomcat calculates all relative references for files in the following directories on the basis of the value set for CATALINA_BASE instead of CATALINA_HOME:
- conf contains configuration files and related DTDs. The most important file located here is server.xml.
- logs holds log and output files.
- webapps is where you put the applications.
- work – Tomcat translates and converts any JavaServer Pages (JSP) into servlets and stores them here.
- temp is used for temporary files.
By default on $CATALINA_BASE has no value, so all the directories are related to $CATALINA_HOME. We want to change this, and to do so we'll work with the script that starts and stops Tomcat, but first, let's create the Tomcat directories that we want to use to store the information from our Tomcats.
Step SSL certificate creation/generation
Note: Create folder at any location; I created under /home/hussain folder named keytool_fin
e.g.: /home/hussain/keytool_fin
Using terminal making folder under above mention folder
1- mkdir keytool_fin
2- cd keytool_fin
Generation of SSL in keytool_fin folder
3- keytool -genkey -alias cb2 -keyalg RSA -keystore server3.keystore -keysize 2048
4- keytool -export -alias cb2 -keystore server3.keystore -file server3.cer
5- sudo keytool -import -alias cb2 -file server3.cer -keystore $JAVA_HOME/jre/lib/security/cacerts
note after this it ask your machine root password then ask you password just type changeit
1- Download tomcat from here
2- Uncompress it and copy into two different folders like name tomcate_1 and tomcate_2
(e.g.: tomcate_poral, tomcate_rule)
For tomcat_1
3- Go to tomcat_1/bin folder and open startup.sh file and past below lines in it
export BASEDIR=/home/hussain/tomcat_1
export CATALINA_BASE=/home/hussain/tomcat_1
export CATALINA_HOME=/home/hussain/tomcat_1
4- Go to tomcate_1/bin folder and open shutdown.sh file and past above lines in it.
5- Then go to tomcat_1/conf folder and open server.xml file and change ports
change from 8005 to 8205 -- this is for shut-down port
no change 8080 port -- this is A "Connector" for endpoint by which requests are received
change from 8009 to 8209 -- this is for AJP 1.3 Connector port
and add these lines in it.
<Connector port="8443"
maxHttpHeaderSize="8192"
maxThreads="150"
minSpareThreads="25"
enableLookups="false"
disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="/home/hussain/keytool_fin/server3.keystore"
keystorePass="changeit" SSLEnabled="true"
protocol="org.apache.coyote.http11.Http11Protocol" />
For tomcat_2
6- Go to tomcat_2/bin folder and open startup.sh file and past below lines in it
export BASEDIR=/home/hussain/tomcat_2
export CATALINA_BASE=/home/hussain/tomcat_2
export CATALINA_HOME=/home/hussain/tomcat_2
7- Go to tomcate_2/bin folder and open shutdown.sh file and past above lines in it.
8- Go to tomcat_2/conf and open server.xml file and change ports
change from 8005 to 8105 -- this is for shut-down port
change from 8080 to 8686 -- this is A "Connector" for endpoint by which requests are received
change from 8009 to 8109 -- this is for AJP 1.3 Connector port
and add these lines in it.
<Connector port="8446"
maxHttpHeaderSize="8192"
maxThreads="150"
minSpareThreads="25"
enableLookups="false"
disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="/home/hussain/keytool_fin/server3.keystore"
keystorePass="changeit" SSLEnabled="true"
protocol="org.apache.coyote.http11.Http11Protocol" />
9- Then start the servers
For tomcat_1
tomcat_1/bin> ./startup.sh -Djavax.net.ssl.trustStore="/home/hussain/keytool_fin/server3.keystore"
For tomcat_2
tomcat_2/bin> ./startup.sh -Djavax.net.ssl.trustStore="/home/hussain/keytool_fin/server3.keystore"
10- then go to browser and type
For tomcat_1
localhost:8080 or https://localhost:8443 you should see tomcat home page.
For tomcat_2
localhost:8686 or https://localhost:8446 you should see tomcat home page.
11- Then deploy your war into both tomcat_1 and tomcat_2 webapps folder and restart both servers
_______________________________________________________________________________
Syed Hussain Haider
email : ali7us(at)yahoo(dot)com
cell : +92 331 21 55 258
linkedin : http://pk.linkedin.com/in/syedhaider7
_______________________________________________________________________________
No comments:
Post a Comment