Tomcat multi instance manual config

Get Tomcat to follow links:

### cd /var/lib/tomcat7/webapps/QBW-FrontPage/META-INF

### ls -l
-rw-r–r– 1 root root 89 Feb 4 00:22 context.xml
### vi context.xml
<?xml version='1.0' encoding='utf-8'?>
<Context reloadable="true" allowLinking="true" />

Needed for: QBW-FrontPage.war (435.41 kb)

Tomcat

Tomcat is actually very simple install. It is a matter of extracting the files in a location, modifying a few scripts, and running the startup scripts. For those who are not 100% familiar with how tomcat works, it sets up using the environment variables of the user that is executing the startup script.

Create “tomcat” Group & User

First things first, what we would like to do, is create a new user called tomcat and make it impossible for it to logon.

sudo groupadd tomcat

Now you have to create a new user called “tomcat” (useradd tomcat) who belongs to the group “tomcat” (-g tomcat) and cannot login –s /usr/sbin/nologin. You also should set the home directory of that user to the directory where you moved the Tomcat server in the previous step. In this case that would be “/usr/local/tomcat” (-d /usr/local/tomcat). So you should end up with a statement that looks something like this:
sudo useradd -g tomcat -s /usr/sbin/nologin -m -d /usr/local/tomcat tomcat

3. Now you should also add the user to the “www-data” group. This group should already exist. You do that by executing the following command:

sudo usermod -G www-data tomcat

4. Create INIT File for Tomcat

Now you should create an INIT-File that makes it possible to start, stop and restart your Tomcat Server. This file must be located in your “/etc/init.d/” directory. You can use the following command to create a file called “tomcat” and open up that file in an editor (I used nano).

sudo vi /etc/init.d/tomcat8x0

Now you should add the following lines into the file an save it:

#Tomcat auto-start

#description: Auto-starts tomcat

#processname: tomcat

#pidfile: /var/run/tomcat.pid

#this path should point to your JAVA_HOME Directory

#####export JAVA_HOME=/usr/lib/jvm/java-6-sun

case $1 in

start)

sh /opt/tomcat7/8×0/bin/startup.sh

;;

stop)

sh /opt/tomcat7/8×0/bin/shutdown.sh

;;

restart)

sh /opt/tomcat7/8×0/bin/startup.sh

sh /opt/tomcat7/8×0/bin/shutdown.sh

;;

esac

exit 0

Make sure you set the right paths for the startup.sh and shutdown.sh scripts. They reside in the /bin directory of your tomcat path (use the path to which you moved the tomcat files in step 2).

5. Adjust Permissions of INIT File

Since you have to execute the tomcat file, you have to assign the correct rights for the file to be executable.
This line should do the trick:

sudo chmod 755 /etc/init.d/tomcat8x0

6. Make Tomcat auto-start on boot (optional)

If you want the Tomcat Server to start every time the system boots up you can use the “update-rc.d” command to set a symbolic link at the correct runlevel. For the “tomcat fle” this looks like this:

sudo update-rc.d tomcat8x0 defaults

You can remove the autostart with the following command: sudo update-rc.d -f tomcat8x0 remove

Now the Tomcat Server starts automatically at system bootup. This step is optional you can always start your Tomcat Server manually like this:

sudo /etc/init.d/tomcat8x0 start

If we have already installed on another server, lets use SCP

Dest server: sudo chmod 777 /opt

Source server: sudo scp -r /opt/tomcat7 davidq@tomcat-02:/opt

Dest server: sudo chmod 755 /opt

IF scp was used….. (SET JMX HOST NAME)……Skip to section: Change the owner

Download the gz

mkdir ~/download

cd ~/download

wget http://apache.mirrors.tds.net/tomcat/tomcat-7/v7.0.35/bin/apache-tomcat-7.0.35.tar.gz

Now lets extract the files. Remember, our files are in the folder /download

tar -xf ​apache-tomcat-7.0.35.tar.gz

Copy them to the /opt folder

sudo cp -rf ~/download/apache-tomcat-7.0.35/* /opt/tomcat7/

sudo rm /opt/tomcat7/bin/*.bat

For every instance, we need to copy the folder over. For this exercise, we will create 2 instances:

sudo cp -rf /opt/tomcat7 /opt/tomcat7/8×0

sudo vi /opt/tomcat7/8×0/webapps/ROOT/index.jsp

replace:

<h1>${pageContext.servletContext.serverInfo}</h1>

With

<h1>${pageContext.servletContext.serverInfo} : <font color="red">Instance 8×0</font></h1>

Now we need to modify the startup and shutdown scripts. In order to do so, lets go into the bin folder of each instance.

cd /opt/tomcat7/8×0/bin

In this folder will be startup.sh and shutdown.sh scripts. We need to modify these scripts to include the right environmental parameters. Add these commands to the startup.sh script:

#export JAVA_HOME=/usr/java/jdk1.7.0_07

#export JAVA_HOME=/usr/bin/java
​#export PATH=$JAVA_HOME/bin:$PATH
​export BASEDIR=/opt/tomcat7/810
​export CATALINA_BASE=/opt/tomcat7/810
​export CATALINA_HOME=/opt/tomcat7/810

and enable JMX

export JAVA_OPTS="-Dcom.sun.management.jmxremote.port=810x

-Dcom.sun.management.jmxremote.ssl=false

-Dcom.sun.management.jmxremote.authenticate=false

-Djava.rmi.server.hostname=192.168.1.8x"

or secured

export JAVA_OPTS="-Dcom.sun.management.jmxremote.port=8×09

-Djava.rmi.server.hostname=192.168.1.xx

-Dcom.sun.management.jmxremote.ssl=false

-Dcom.sun.management.jmxremote.authenticate=true

-Dcom.sun.management.jmxremote.password.file=/opt/tomcat7/jmxremote.password

-Dcom.sun.management.jmxremote.access.file=/opt/tomcat7/jmxremote.access

"

sudo vi /opt/tomcat7/jmxremote.access

#monitorRole readonly

#controlRole readwrite

username readwrite

sudo vi /opt/tomcat7/jmxremote.password

#monitorRole tomcat

#controlRole tomcat

username password

Or we can modify the ENVIRONMENT variables???

Now we define the environment variables JAVA_HOME and JRE_HOME. This file is in the "environment" in / etc. Command to edit the file:

sudo gedit /etc/environment


Here we record the routes where we have installed Java in my case this is as follows:

JAVA_HOME="/usr/local/jdk1.6.0_23"

JRE_HOME="/usr/local/jdk1.6.0_23/jre"

PATH="…(other path):$JAVA_HOME:$JRE_HOME"

How your script should look with the script modifications.

​Once done, do the same for the shutdown script (shutdown.sh).

Next, in the conf folder, we need to modify the server.xml file to modify the server ports used for tomcat. This is how we do it:

There are two ports needed: (1) the actual http port and (2) the shutdown port. For every instance we will create, these ports need to be different (Later we will see how to use the same ports with another method).

For our first instance, we can leave the defaults, 8080 and 8001. For the next instance, we will change the ports. (I like to add a 100 to the default which means 8180 and 8101 for the second port)

​​Configuring the shutdown port from 8005 to 8101 in server.xml

​Configuring the http port from 8080 to 8180 in server.xml

Change the owner of the folders to the group tomcat and the user tomcat:

sudo chown -R tomcat:tomcat /opt/tomcat7

Now to run the instance of Tomcat, lets execute the script as the user tomcat:

cd /opt/tomcat7/8×0/bin

su -p -s /bin/sh tomcat startup.sh

Lets check out our separate tomcat instances:

http://ipaddress-of-server:8101/

<role rolename="manager-script"/>

<role rolename="manager-jmx"/>

<role rolename="manager-status"/>

<role rolename="manager-gui"/>

<role rolename="admin-gui"/>

<role rolename="admin-script"/>

<user username="user" password="password" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>

Autostart after boot

http://linuxphile.org/node/7

JMX (more reading)

http://tomcat.apache.org/tomcat-5.5-doc/monitoring.html#Enabling_JMX_Local

http://www.mulesoft.com/tomcat-jmx#.URFKkYJopec

HAProxy

I mentioned earlier that there is a way to have all your instances of Tomcat to respond on the same http port. We can achieve this using a server called HAProxy. HAProxy is an open source load balancer that you can use to redirect http requests to web servers that are using different ports. For a full breakdown of the configuration, please read my post on HAProxy here.



Tag Cloud