<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Apache 2.2 + SSL + JK 1.2 + Tomcat 7.0 Clustering Auto Installer for CentOS 6.0"]]></title>
		<link>https://forum.andowson.com/posts/list/20.page</link>
		<description><![CDATA[Latest messages posted in the topic "Apache 2.2 + SSL + JK 1.2 + Tomcat 7.0 Clustering Auto Installer for CentOS 6.0"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Apache 2.2 + SSL + JK 1.2 + Tomcat 7.0 Clustering Auto Installer for CentOS 6.0</title>
				<description><![CDATA[ An Auto Installer for Integrating Apache 2.2 and Tomcat 7.0 for Clustering/Session Replication, Load Balancing with mod_jk 1.2 on CentOS 6.0 
<br>
Author: Andowson Chang 
<br>
Date: 2011.09.29 
<br>
<br>
Design: 
<br>
1. One Apache and Two Tomcat workers on the same machine. 
<br>
2. Use mod_jk for load balancer 
<br>
3. Use mod_ssl for SSL 
<br>
4. Enable cluster configuration on both Tomcat workers to do session replication 
<br>
[img]http://andowson.myweb.hinet.net/img/apache_tomcat_integration.jpg[/img] 
<br>
<br>
Steps: 
<br>
1.Install CentOS 6.0 ([color=red]disbale SELinux and Firewall[/color]) 
<br>
2.Install JRE 
<br>
3.Install Apache and mod_ssl 
<br>
4.Generate SSL key and certificate 
<br>
5.Edit httpd.conf 
<br>
6.Edit ssl.conf 
<br>
7.Install Tomcat, mod_jk 
<br>
8.Edit server.xml 
<br>
9.Edit mod_jk.conf 
<br>
10.Edit workers.properties 
<br>
11.Edit uriworkermap.properties 
<br>
12.Start Apache and Tomcat 
<br>
<br>
Testing: 
<br>
1.Browsing http://&lt;your_server_ip&gt;/clustertest.jsp, add some keys and values. 
<br>
2.Shutdown worker1 
<br>
3.Browsing http://&lt;your_server_ip&gt;/clustertest.jsp, check if the keys and values are still there. 
<br>
4.Run restart.sh 
<br>
5.Browsing http://&lt;your_server_ip&gt;/clustertest.jsp, check if the keys and values are still there. 
<br>
6.Browsing https://&lt;your_server_ip&gt;/clustertest.jsp, check if the keys and values are still there. 
<br>
<br>
Implementation: 
<br>
1.Choose Minimal when install CentOS 6.0 
<br>
2.Create a directory /root/setup and download the auto installer script for step 2-11, save it as httpd2_tomcat7_setup.sh in /root/setup 
<br>
[code=text] 
<br>
mkdir -p /root/setup 
<br>
wget http://www.andowson.com/download/httpd2_tomcat7_setup.sh 
<br>
[/code] 
<br>
then run it 
<br>
[code=text] 
<br>
chmod 755 /root/setup/httpd2_tomcat7_setup.sh 
<br>
cd /root/setup 
<br>
./httpd2_tomcat7_setup.sh 
<br>
[/code] 
<br>
<br>
[b]httpd2_tomca7_setup.sh[/b]: 
<br>
[code=bash] 
<br>
#!/bin/bash 
<br>
# Name: Apache 2.2.x &amp;&amp; Tomcat 7.0.x auto installer for CentOS 6.x 
<br>
# Author: Andowson Chang (andowson [at] gmail [dot] com) 
<br>
# Version: 5.15 
<br>
# Last Modified: 2011-09-29 
<br>
# Source: http://www.andowson.com 
<br>
# 
<br>
# Assumption: 
<br>
# * FQDN hostname is correctly set on /etc/sysconfig/network and /etc/hosts 
<br>
# ex. 
<br>
# /etc/sysconfig/network: 
<br>
# HOSTNAME=www.andowson.com 
<br>
# /etc/hosts: 
<br>
# 192.168.1.1 www.andowson.com www 
<br>
# (reboot after modified) 
<br>
# * run this script as root user 
<br>
# Post-Install: 
<br>
# * disable SELINUX: 
<br>
# /etc/selinux/config 
<br>
# SELINUX=disabled 
<br>
# (reboot after modified) 
<br>
# * allow HTTP(80:tcp), HTTPS(443:tcp), 45564:udp in iptables 
<br>
# 
<br>
# This program will 
<br>
# 1.install JRE 7 
<br>
# 2.install httpd and mod_ssl 
<br>
# 3.modify /etc/http/conf/httpd.conf 
<br>
# 4.generate SSL key and a self-signed certificate 
<br>
# 5.modify /etc/http/conf.d/ssl.conf 
<br>
# 6.check for the lastest stable version of Tomcat 7.0.x 
<br>
# 7.download apache-tomcat-7.0.x.tar.gz 
<br>
# 8.unpack it into /var/apache-tomcat-7.0.x 
<br>
# 9.make a symbolic link /var/tomcat7 to the above directory for easy upgrade 
<br>
# to a later version 
<br>
# 10.check memory size to determine some JVM parameters 
<br>
# 11.check for the latest stable verion of Tomcat Connector 1.2.x 
<br>
# 12.download tomcat-connectors-1.2.x-src.tar.gz 
<br>
# 13.unpack it 
<br>
# 14.configure, make and make install 
<br>
# 15.create user tomcat with home directory /var/tomcat7 
<br>
# 16.make two instances of tomcat for load balancing, here I put them under 
<br>
# /var/robust/worker[1,2] for easy to backup all of them. 
<br>
# 17.setup default host directory(for testing) 
<br>
# 18.generate server.xml 
<br>
# 19.generate tomcat-users.xml 
<br>
# 20.modify web.xml for production use 
<br>
# 21.generate mod_jk.conf 
<br>
# 22.generate workers.properties 
<br>
# 23.generate uriworkermap.properties 
<br>
# 24.generate tomcat startup script 
<br>
# 25.modify web.xml for cluster test 
<br>
# 26.generate clustertest.jsp for cluster test 
<br>
# 27.generate cleanup.sh for easy to uninstall everything 
<br>
<br>
# 
<br>
# intranet workaround 
<br>
# If your server doesn't have direct access to the Internet, ie. in the company 
<br>
# intranet behind a firewall, you can modify your server's proxy setting by edit 
<br>
# /etc/wgetrc: 
<br>
#http_proxy = http://proxy.yourcompany.com:8080/ 
<br>
#use_proxy = on 
<br>
<br>
# If you still can't use proxy then you have to download all the files manually. 
<br>
# After download, copy them into ${SETUP_DIR}. And then change the following 
<br>
# default version number to the correct number you have downloaded. 
<br>
# 
<br>
TOMCAT_VERSION=7.0.21 
<br>
TOMCAT_CONNECTOR_VERSION=1.2.32 
<br>
<br>
# 
<br>
# auto detect ip and hostname 
<br>
# 
<br>
ip=`/sbin/ifconfig|grep "inet addr"|awk '{print $2}'|cut -d":" -f2|head -1` 
<br>
hostname=`/bin/hostname -f` 
<br>
<br>
# 
<br>
# adjustable parameters, you can modify any of them to fit your own need 
<br>
# 
<br>
DOMAIN=`/bin/hostname -d` 
<br>
COUNTRY=TW 
<br>
STATE=Taiwan 
<br>
LOCATION=Taipei 
<br>
COMPANY="Andowson Ltd." 
<br>
ORGANIZATION= 
<br>
HOSTNAME=${hostname} 
<br>
hostname=${ip} 
<br>
MIRROR_HOST=apache.ntu.edu.tw 
<br>
CATALINA_HOME=/var/tomcat7 
<br>
WORKER_ROOT=/var/robust 
<br>
WEBAPP_ROOT=/var/webapps 
<br>
SETUP_DIR=/root/setup/web 
<br>
<br>
# 
<br>
# create the setup directory 
<br>
# 
<br>
mkdir -p ${SETUP_DIR} 
<br>
cd ${SETUP_DIR} 
<br>
echo '/etc/init.d/tomcat stop' &gt; ${SETUP_DIR}/cleanup.sh 
<br>
echo '/etc/init.d/httpd stop' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
# 
<br>
# install JRE 
<br>
# 
<br>
OS_MODEL=`uname -m` 
<br>
JRE_FILE="" 
<br>
if [ ${OS_MODEL} == "i686" ]; then 
<br>
 JRE_FILE="jdk-7-linux-i586.rpm" 
<br>
elif [ ${OS_MODEL} == "x86_64" ]; then 
<br>
 JRE_FILE="jdk-7-linux-x64.rpm" 
<br>
fi 
<br>
echo "Install ${JRE_FILE}" 
<br>
if [ -n ${JRE_FILE} ] &amp;&amp; [ ! -r ${JRE_FILE} ]; then 
<br>
 wget http://download.oracle.com/otn-pub/java/jdk/7/${JRE_FILE} -O ${JRE_FILE} 
<br>
fi 
<br>
if [ -r ${JRE_FILE} ]; then 
<br>
 rpm -Uvh ${JRE_FILE} 
<br>
fi 
<br>
<br>
# 
<br>
# install httpd and mod_ssl 
<br>
# 
<br>
yum -y install httpd mod_ssl 
<br>
cp -p /etc/httpd/conf/httpd.conf /tmp/httpd.conf 
<br>
cp -p /etc/httpd/conf.d/ssl.conf /tmp/ssl.conf 
<br>
<br>
# 
<br>
# modify httpd.conf 
<br>
# 
<br>
sed -i -e "s/ServerTokens OS/ServerTokens ProductOnly/g" \ 
<br>
-e "s/KeepAlive Off/KeepAlive On/g" \ 
<br>
-e "s/Group apache/Group tomcat/g" \ 
<br>
-e "s/root@localhost/webmaster@${DOMAIN}/g" \ 
<br>
-e "276a\ServerName ${HOSTNAME}:80" \ 
<br>
-e "292a\VirtualDocumentRoot /var/webapps/%0" \ 
<br>
-e "s/DirectoryIndex index.html index.html.var/DirectoryIndex sorry.html index.html index.htm default.html default.htm index.html.var index.jsp/g" \ 
<br>
-e '500a\LogFormat "%V %h %l %u %t \\"%r\\" %&gt;s %b" vcommon' \ 
<br>
-e '500a\LogFormat "%V %h %l %u %t \\"%r\\" %&gt;s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" vcombined' \ 
<br>
-e '504c\LogFormat "%h %l %u %t \\"%r\\" %&gt;s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\" %I %O" combinedio' \ 
<br>
-e "526c\CustomLog logs/access_log combinedio\nCustomLog logs/vhost.log vcombined" \ 
<br>
-e "s/ServerSignature On/ServerSignature Off/g" \ 
<br>
-e "743c\LanguagePriority zh-TW en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN" \ 
<br>
-e "972a\&lt;Location /&gt;" \ 
<br>
-e "972a\ # Insert filter" \ 
<br>
-e "972a\ SetOutputFilter DEFLATE\n" \ 
<br>
-e "972a\ # Netscape 4.x has some problems..." \ 
<br>
-e "972a\ BrowserMatch ^Mozilla/4 gzip-only-text/html\n" \ 
<br>
-e "972a\ # Netscape 4.06-4.08 have some more problems" \ 
<br>
-e '972a\ BrowserMatch ^Mozilla/4\\.0[678] no-gzip\n' \ 
<br>
-e "972a\ # MSIE masquerades as Netscape, but it is fine" \ 
<br>
-e '972a\ # BrowserMatch \\bMSIE \!no-gzip \!gzip-only-text/html\n' \ 
<br>
-e "972a\ # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48" \ 
<br>
-e "972a\ # the above regex won't work. You can use the following" \ 
<br>
-e "972a\ # workaround to get the desired effect:" \ 
<br>
-e '972a\ BrowserMatch \\bMSI[E] \!no-gzip \!gzip-only-text/html\n' \ 
<br>
-e "972a\ # Don't compress images" \ 
<br>
-e '972a\ SetEnvIfNoCase Request_URI \\.(?:gif|jpe?g|png)$ no-gzip dont-vary\n' \ 
<br>
-e "972a\ # Make sure proxies don't deliver the wrong content" \ 
<br>
-e "972a\ Header append Vary User-Agent env=\!dont-vary" \ 
<br>
-e "972a\&lt;/Location&gt;\n" \ 
<br>
-e "972a\RewriteEngine on" \ 
<br>
-e "972a\RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)" \ 
<br>
-e "972a\RewriteRule .* - [F]\n" /etc/httpd/conf/httpd.conf 
<br>
echo 'cp -p /tmp/httpd.conf /etc/httpd/conf/httpd.conf' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
# 
<br>
# generate SSL key and cert 
<br>
# 
<br>
openssl genrsa -out /etc/pki/tls/private/${HOSTNAME}.key 2048 
<br>
openssl req -new -key /etc/pki/tls/private/${HOSTNAME}.key -out /etc/pki/tls/certs/${HOSTNAME}.csr -subj "/C=${COUNTRY}/ST=${STATE}/L=${LOCATION}/O=${COMPANY}/OU=${ORGANIZATION}/CN=${HOSTNAME}" 
<br>
openssl x509 -req -days 3650 -in /etc/pki/tls/certs/${HOSTNAME}.csr -signkey /etc/pki/tls/private/${HOSTNAME}.key -out /etc/pki/tls/certs/${HOSTNAME}.crt 
<br>
echo 'rm -rf /etc/pki/tls/private/'${HOSTNAME}'.key' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
echo 'rm -rf /etc/pki/tls/certs/'${HOSTNAME}'.csr' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
echo 'rm -rf /etc/pki/tls/certs/'${HOSTNAME}'.crt' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
# 
<br>
# modify ssl.conf 
<br>
# 
<br>
sed -i -e "79a\JkMountFile conf/uriworkermap.properties\n" \ 
<br>
-e "s/localhost.crt/${HOSTNAME}.crt/g" \ 
<br>
-e "s/localhost.key/${HOSTNAME}.key/g" /etc/httpd/conf.d/ssl.conf 
<br>
echo 'cp -p /tmp/ssl.conf /etc/httpd/conf.d/ssl.conf' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
/sbin/chkconfig --level 235 httpd on 
<br>
echo '/sbin/chkconfig httpd off' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
# 
<br>
# check the latest stable version of Tomcat 7.0.x 
<br>
# 
<br>
wget http://tomcat.apache.org/whichversion.html -q -t 1 -T 5 -O /tmp/tomcat.html 
<br>
if [ -s /tmp/tomcat.html ]; then 
<br>
 TOMCAT_VERSION=`grep "7\.0\." /tmp/tomcat.html|grep -v "7\.0\.x"|cut -d"&gt;" -f2|cut -d"&lt;" -f1` 
<br>
fi 
<br>
rm -rf /tmp/tomcat.html 
<br>
<br>
echo "Install Tomcat ${TOMCAT_VERSION}" 
<br>
if [ ! -r apache-tomcat-${TOMCAT_VERSION}.tar.gz ]; then 
<br>
 wget http://${MIRROR_HOST}/tomcat/tomcat-7/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz 
<br>
fi 
<br>
tar zxvf apache-tomcat-${TOMCAT_VERSION}.tar.gz -C /var 
<br>
ln -s /var/apache-tomcat-${TOMCAT_VERSION} ${CATALINA_HOME} 
<br>
<br>
echo 'rm -rf /var/apache-tomcat-'${TOMCAT_VERSION} &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
echo 'rm -rf '${CATALINA_HOME} &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
# 
<br>
# check memory size to determine some JVM parameters 
<br>
# 
<br>
mem=`cat /proc/meminfo|grep "MemTotal:"|awk '{print $2}'` 
<br>
let KB=1024 
<br>
let memsize=$mem/$KB 
<br>
let heapsize=$memsize/2 
<br>
let newsize=$heapsize/4 
<br>
echo 'JAVA_HOME="/usr/java/latest" 
<br>
JAVA_OPTS="-server -XX:NewSize='${newsize}'m -XX:MaxNewSize='${newsize}'m -XX:SurvivorRatio=8 -XX:MaxPermSize=128m -Xss256k -Xms'${heapsize}'m -Xmx'${heapsize}'m -Djava.net.preferIPv4Stack=true -Djava.awt.headless=true"' &gt; ${CATALINA_HOME}/bin/setenv.sh 
<br>
chmod 755 ${CATALINA_HOME}/bin/setenv.sh 
<br>
<br>
# 
<br>
# check the latest stable version of Tomcat Connector 1.2.x 
<br>
# 
<br>
# pre-requirment 
<br>
yum -y install httpd-devel apr-devel libtool automake make gcc gcc-c++ 
<br>
<br>
wget http://tomcat.apache.org/download-connectors.cgi -q -t 1 -T 5 -O /tmp/connector.html 
<br>
if [ -s /tmp/connector.html ]; then 
<br>
 TOMCAT_CONNECTOR_VERSION=`grep "1\.2\." /tmp/connector.html|cut -d"&gt;" -f2|cut -d"&lt;" -f1 |awk '{print $2}'|grep "1.2"|uniq` 
<br>
fi 
<br>
rm -rf /tmp/connector.html 
<br>
echo "Install Tomcat Connector JK ${TOMCAT_CONNECTOR_VERSION}" 
<br>
if [ ! -r tomcat-connectors-${TOMCAT_CONNECTOR_VERSION}-src.tar.gz ]; then 
<br>
 wget http://${MIRROR_HOST}/tomcat/tomcat-connectors/jk/tomcat-connectors-${TOMCAT_CONNECTOR_VERSION}-src.tar.gz 
<br>
fi 
<br>
tar zxvf tomcat-connectors-${TOMCAT_CONNECTOR_VERSION}-src.tar.gz 
<br>
cd tomcat-connectors-${TOMCAT_CONNECTOR_VERSION}-src/native 
<br>
./buildconf.sh 
<br>
./configure --with-apxs=/usr/sbin/apxs 
<br>
make 
<br>
make install 
<br>
cd ${SETUP_DIR} 
<br>
echo 'rm -rf /etc/httpd/modules/mod_jk.so' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
# 
<br>
# add user tomcat 
<br>
# 
<br>
/usr/sbin/groupadd -g 80 tomcat 
<br>
/usr/sbin/useradd tomcat -u 80 -g tomcat -d ${CATALINA_HOME} -M 
<br>
echo '/usr/sbin/userdel -r tomcat' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
# 
<br>
# setup workers' directory 
<br>
# 
<br>
mkdir -p ${WORKER_ROOT}/worker1/logs 
<br>
mkdir -p ${WORKER_ROOT}/worker1/temp 
<br>
mkdir -p ${WORKER_ROOT}/worker1/work 
<br>
cp -rf ${CATALINA_HOME}/conf ${WORKER_ROOT}/worker1/conf 
<br>
mkdir -p ${WEBAPP_ROOT} 
<br>
ln -s ${WEBAPP_ROOT} ${WORKER_ROOT}/worker1/webapps 
<br>
cp -rf ${WORKER_ROOT}/worker1 ${WORKER_ROOT}/worker2 
<br>
echo 'rm -rf '${WORKER_ROOT} &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
echo 'rm -rf '${WEBAPP_ROOT} &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
# 
<br>
# setup default host directory 
<br>
# 
<br>
mkdir -p ${WEBAPP_ROOT}/${hostname} 
<br>
cp -rf ${CATALINA_HOME}/webapps/ROOT/* ${WEBAPP_ROOT}/${hostname}/. 
<br>
mkdir -p ${WORKER_ROOT}/worker1/conf/Catalina/${hostname} 
<br>
mkdir -p ${WORKER_ROOT}/worker2/conf/Catalina/${hostname} 
<br>
cp -rf ${CATALINA_HOME}/webapps/host-manager/manager.xml ${WORKER_ROOT}/worker1/conf/Catalina/${hostname} 
<br>
cp -rf ${CATALINA_HOME}/webapps/host-manager/manager.xml ${WORKER_ROOT}/worker2/conf/Catalina/${hostname} 
<br>
<br>
# 
<br>
# generate server.xml 
<br>
# 
<br>
echo '&lt;?xml version="1.0" encoding="utf-8"?&gt; 
<br>
&lt;Server port="workerPort" shutdown="shutdownCode"&gt; 
<br>
<br>
 &lt;!-- Prevent memory leaks due to use of particular java/javax APIs--&gt; 
<br>
 &lt;Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /&gt; 
<br>
 &lt;Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /&gt; 
<br>
 &lt;Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /&gt; 
<br>
<br>
 &lt;GlobalNamingResources&gt; 
<br>
 &lt;!-- Used by Manager webapp --&gt; 
<br>
 &lt;Resource name="UserDatabase" auth="Container" 
<br>
 type="org.apache.catalina.UserDatabase" 
<br>
 description="User database that can be updated and saved" 
<br>
 factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 
<br>
 pathname="conf/tomcat-users.xml" /&gt; 
<br>
 &lt;/GlobalNamingResources&gt; 
<br>
<br>
 &lt;Service name="Catalina"&gt; 
<br>
 &lt;!-- Define an AJP 1.3 Connector on port ajpPort --&gt; 
<br>
 &lt;Connector port="ajpPort" address="127.0.0.1" 
<br>
 enableLookups="false" maxThreads="1000" connectionTimeout="3000" 
<br>
 protocol="AJP/1.3" /&gt; 
<br>
<br>
 &lt;Engine name="Catalina" defaultHost="mydomain" jvmRoute="workerNo"&gt; 
<br>
<br>
 &lt;Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/&gt; 
<br>
<br>
 &lt;!-- Use the LockOutRealm to prevent attempts to guess user passwords 
<br>
 via a brute-force attack --&gt; 
<br>
 &lt;Realm className="org.apache.catalina.realm.LockOutRealm"&gt; 
<br>
 &lt;!-- This Realm uses the UserDatabase configured in the global JNDI 
<br>
 resources under the key "UserDatabase". Any edits 
<br>
 that are performed against this UserDatabase are immediately 
<br>
 available for use by the Realm. --&gt; 
<br>
 &lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm" 
<br>
 resourceName="UserDatabase"/&gt; 
<br>
 &lt;/Realm&gt; 
<br>
' &gt; /tmp/server.header.template 
<br>
<br>
sed -e "s/workerPort/8105/g" -e "s/shutdownCode/`head -1024c /dev/urandom | md5sum | cut -d " " -f1`/g" -e "s/ajpPort/8109/g" -e "s/workerNo/worker1/g" -e "s/mydomain/${hostname}/g" /tmp/server.header.template &gt; ${WORKER_ROOT}/worker1/conf/server.header 
<br>
sed -e "s/workerPort/8205/g" -e "s/shutdownCode/`head -1024c /dev/urandom | md5sum | cut -d " " -f1`/g" -e "s/ajpPort/8209/g" -e "s/workerNo/worker2/g" -e "s/mydomain/${hostname}/g" /tmp/server.header.template &gt; ${WORKER_ROOT}/worker2/conf/server.header 
<br>
rm -rf /tmp/server.header.template 
<br>
<br>
echo ' &lt;Host name="'${hostname}'" appBase="webapps/'${hostname}'" 
<br>
 unpackWARs="true" autoDeploy="true"&gt; 
<br>
 &lt;Valve className="org.apache.catalina.valves.AccessLogValve" 
<br>
 directory="logs" prefix="'${hostname}'_access_log." suffix=".txt" 
<br>
 pattern="combined" resolveHosts="false"/&gt; 
<br>
 &lt;Context path="" docBase="" reloadable="false" /&gt; 
<br>
 &lt;/Host&gt;' &gt; ${WORKER_ROOT}/worker1/conf/server.host 
<br>
<br>
cp -rf ${WORKER_ROOT}/worker1/conf/server.host ${WORKER_ROOT}/worker2/conf/server.host 
<br>
<br>
echo ' &lt;/Engine&gt; 
<br>
 &lt;/Service&gt; 
<br>
&lt;/Server&gt;' &gt; ${WORKER_ROOT}/worker1/conf/server.footer 
<br>
<br>
cp -rf ${WORKER_ROOT}/worker1/conf/server.footer ${WORKER_ROOT}/worker2/conf/server.footer 
<br>
<br>
cd ${WORKER_ROOT}/worker1/conf 
<br>
cat server.header server.host server.footer &gt; server.xml 
<br>
cd ${WORKER_ROOT}/worker2/conf 
<br>
cat server.header server.host server.footer &gt; server.xml 
<br>
<br>
# 
<br>
# generate tomcat-users.xml 
<br>
# 
<br>
MANAGER_PASSWORD=`head -1024c /dev/urandom | md5sum | cut -d " " -f1 | awk '{print substr($1,1,8)}'` 
<br>
echo '&lt;?xml version="1.0" encoding="utf-8"?&gt; 
<br>
&lt;tomcat-users&gt; 
<br>
 &lt;role rolename="manager-gui"/&gt; 
<br>
 &lt;user username="manager" password="'${MANAGER_PASSWORD}'" roles="manager-gui"/&gt; 
<br>
&lt;/tomcat-users&gt;' &gt; ${WORKER_ROOT}/worker1/conf/tomcat-users.xml 
<br>
echo 
<br>
echo "Your Tomcat Manager's login is 'manager', password is '${MANAGER_PASSWORD}'" 
<br>
echo 
<br>
cp -rf ${WORKER_ROOT}/worker1/conf/tomcat-users.xml ${WORKER_ROOT}/worker2/conf/tomcat-users.xml 
<br>
<br>
# 
<br>
# modify web.xml for production use 
<br>
# 
<br>
sed -i -e "240a\ &lt;init-param&gt;\n &lt;param-name&gt;genStringAsCharArray&lt;/param-name&gt;\n &lt;param-value&gt;true&lt;/param-value&gt;\n &lt;/init-param&gt;" \ 
<br>
-e "240a\ &lt;init-param&gt;\n &lt;param-name&gt;trimSpaces&lt;/param-name&gt;\n &lt;param-value&gt;true&lt;/param-value&gt;\n &lt;/init-param&gt;" ${WORKER_ROOT}/worker1/conf/web.xml 
<br>
cp -rf ${WORKER_ROOT}/worker1/conf/web.xml ${WORKER_ROOT}/worker2/conf/web.xml 
<br>
<br>
chmod 600 ${WORKER_ROOT}/worker*/conf/server.* 
<br>
chmod 600 ${WORKER_ROOT}/worker*/conf/tomcat-users.xml 
<br>
chown -R tomcat:tomcat ${WORKER_ROOT} 
<br>
<br>
# 
<br>
# generate mod_jk.conf 
<br>
# 
<br>
echo 'LoadModule jk_module modules/mod_jk.so 
<br>
<br>
JkWorkersFile /etc/httpd/conf/workers.properties 
<br>
JkShmFile logs/jk-runtime-status 
<br>
JkLogFile "|/usr/sbin/rotatelogs /var/log/httpd/mod_jk.log 86400" 
<br>
JkLogLevel info 
<br>
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]" 
<br>
JkRequestLogFormat "%w %V %T" 
<br>
LogFormat "%h %l %u %t \"%r\" %&gt;s %b %{JK_WORKER_NAME}n %{JK_LB_FIRST_NAME}n %{JK_LB_FIRST_BUSY}n %{JK_LB_LAST_NAME}n %{JK_LB_LAST_BUSY}n" mod_jk_log 
<br>
CustomLog logs/worker_access_log mod_jk_log 
<br>
<br>
# Load mount points 
<br>
JkMountFile conf/uriworkermap.properties 
<br>
<br>
# Deny direct access to WEB-INF 
<br>
&lt;LocationMatch ".*WEB-INF.*"&gt; 
<br>
 deny from all 
<br>
&lt;/LocationMatch&gt;' &gt; /etc/httpd/conf.d/mod_jk.conf 
<br>
echo 'rm -rf /etc/httpd/conf.d/mod_jk.conf' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
# 
<br>
# generate workers.properties 
<br>
# 
<br>
echo '# workers.properties - ajp13 
<br>
# 
<br>
# List workers 
<br>
# 
<br>
worker.list=loadbalancer, jkstatus 
<br>
<br>
# 
<br>
# Define worker1 
<br>
# 
<br>
worker.worker1.type=ajp13 
<br>
worker.worker1.host=localhost 
<br>
worker.worker1.port=8109 
<br>
worker.worker1.socket_timeout=1200 
<br>
worker.worker1.connection_pool_size=1 
<br>
worker.worker1.connection_pool_timeout=1300 
<br>
worker.worker1.lbfactor=1 
<br>
# Define prefered failover node for worker1 
<br>
worker.worker1.redirect=worker2 
<br>
<br>
# 
<br>
# Define worker2 
<br>
# 
<br>
worker.worker2.type=ajp13 
<br>
worker.worker2.host=localhost 
<br>
worker.worker2.port=8209 
<br>
worker.worker2.socket_timeout=1200 
<br>
worker.worker2.connection_pool_size=1 
<br>
worker.worker2.connection_pool_timeout=1300 
<br>
worker.worker2.lbfactor=1 
<br>
# Disable worker2 for all requests except failover 
<br>
worker.worker2.activation=d 
<br>
<br>
# 
<br>
# Defining a load balancer 
<br>
# 
<br>
worker.loadbalancer.type=lb 
<br>
worker.loadbalancer.balance_workers=worker1, worker2 
<br>
<br>
# 
<br>
# Define status worker 
<br>
# 
<br>
worker.jkstatus.type=status' &gt; /etc/httpd/conf/workers.properties 
<br>
echo 'rm -rf /etc/httpd/conf/workers.properties' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
# 
<br>
# generate uriworkermap.properties 
<br>
# 
<br>
echo '/jkmanager/*=jkstatus 
<br>
/*.jsp=loadbalancer 
<br>
/*.page=loadbalancer 
<br>
/servlet/*=loadbalancer 
<br>
/manager/*=loadbalancer' &gt; /etc/httpd/conf/uriworkermap.properties 
<br>
echo 'rm -rf /etc/httpd/conf/uriworkermap.properties' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
# 
<br>
# generate tomcat startup script 
<br>
# 
<br>
echo '#!/bin/sh 
<br>
# 
<br>
# tomcat Startup script for Tomcat, the Apache Servlet Engine 
<br>
# 
<br>
# chkconfig: - 84 16 
<br>
# description: Tomcat Servlet Engine 
<br>
# processname: tomcat 
<br>
# pidfile: /var/run/worker1.pid /var/run/worker2.pid 
<br>
<br>
# Source function library. 
<br>
. /etc/rc.d/init.d/functions 
<br>
<br>
# User under which tomcat will run 
<br>
TOMCAT_USER=tomcat 
<br>
<br>
RETVAL=0 
<br>
<br>
CATALINA_HOME='${CATALINA_HOME}' 
<br>
WORKER_ROOT='${WORKER_ROOT}' 
<br>
WORKER_LIST=( worker1 worker2 ) 
<br>
WORKER_PORT=( 8105 8205 ) 
<br>
WORKER_AJP_PORT=( 8109 8209 ) 
<br>
<br>
# start, debug, stop, and status functions 
<br>
start() { 
<br>
 i=$1 
<br>
 SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep ${WORKER_PORT[i]}|wc -l` 
<br>
 if [ $SHUTDOWN_PORT -ne 0 ]; then 
<br>
 echo "Tomcat ${WORKER_LIST[i]} already started" 
<br>
 else 
<br>
 echo "Starting tomcat ${WORKER_LIST[i]}..." 
<br>
 CATALINA_BASE="$WORKER_ROOT/${WORKER_LIST[i]}" 
<br>
 su -l $TOMCAT_USER -c "export CATALINA_BASE=$CATALINA_BASE; $CATALINA_HOME/bin/startup.sh" 
<br>
 echo -n "Processing ..." 
<br>
 SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep ${WORKER_PORT[i]}|wc -l` 
<br>
 while [ $SHUTDOWN_PORT -eq 0 ]; do 
<br>
 echo -n "..." 
<br>
 sleep 10 
<br>
 SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep ${WORKER_PORT[i]}|wc -l` 
<br>
 done 
<br>
 echo " Done!" 
<br>
 echo "Tomcat ${WORKER_LIST[i]} started in normal mode" 
<br>
 RETVAL=$? 
<br>
 [ $RETVAL = 0 ] &amp;&amp; touch /var/lock/subsys/${WORKER_LIST[i]} /var/run/${WORKER_LIST[i]}.pid 
<br>
 fi 
<br>
} 
<br>
<br>
debug() { 
<br>
 i=$1 
<br>
 SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep ${WORKER_PORT[i]}|wc -l` 
<br>
 if [ $SHUTDOWN_PORT -ne 0 ]; then 
<br>
 echo "Tomcat ${WORKER_LIST[i]} already started" 
<br>
 else 
<br>
 echo "Starting tomcat ${WORKER_LIST[i]} in debug mode..." 
<br>
 CATALINA_BASE="$WORKER_ROOT/${WORKER_LIST[i]}" 
<br>
 rm -rf $CATALINA_BASE/work/* 
<br>
<br>
 chown -R $TOMCAT_USER:$TOMCAT_USER $CATALINA_HOME 
<br>
 chown -R $TOMCAT_USER:$TOMCAT_USER $WORKER_ROOT 
<br>
 su -l $TOMCAT_USER -c "export CATALINA_BASE=$CATALINA_BASE; $CATALINA_HOME/bin/catalina.sh jpda start" 
<br>
 echo -n "Processing ..." 
<br>
 SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep ${WORKER_PORT[i]}|wc -l` 
<br>
 while [ $SHUTDOWN_PORT -eq 0 ]; do 
<br>
 echo -n "..." 
<br>
 sleep 10 
<br>
 SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep ${WORKER_PORT[i]}|wc -l` 
<br>
 done 
<br>
 echo " Done!" 
<br>
 echo "Tomcat ${WORKER_LIST[i]} started in debug mode" 
<br>
 RETVAL=$? 
<br>
 [ $RETVAL = 0 ] &amp;&amp; touch /var/lock/subsys/${WORKER_LIST[i]} /var/run/${WORKER_LIST[i]}.pid 
<br>
 fi 
<br>
} 
<br>
<br>
stop() { 
<br>
 i=$1 
<br>
 SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep ${WORKER_AJP_PORT[i]}|wc -l` 
<br>
 if [ $SHUTDOWN_PORT -eq 0 ]; then 
<br>
 echo "Tomcat ${WORKER_LIST[i]} already stopped" 
<br>
 else 
<br>
 echo "Stopping tomcat ${WORKER_LIST[i]} ..." 
<br>
 CATALINA_BASE="$WORKER_ROOT/${WORKER_LIST[i]}" 
<br>
 su -l $TOMCAT_USER -c "export CATALINA_BASE=$CATALINA_BASE; $CATALINA_HOME/bin/shutdown.sh" 
<br>
 echo -n "Processing ..." 
<br>
 SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep ${WORKER_AJP_PORT[i]}|wc -l` 
<br>
 while [ $SHUTDOWN_PORT -ne 0 ]; do 
<br>
 echo -n "..." 
<br>
 sleep 10 
<br>
 PROCESS_COUNT=`ps -ef|grep ${WORKER_LIST[i]}|grep -v grep|wc -l` 
<br>
 if [ $PROCESS_COUNT -ne 0 ]; then 
<br>
 kill -9 `ps -ef|grep ${WORKER_LIST[i]}|grep -v grep|awk '\'{print \$2}\''` 
<br>
 fi 
<br>
 SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep ${WORKER_AJP_PORT[i]}|wc -l` 
<br>
 done 
<br>
 echo " Done!" 
<br>
 echo "Tomcat ${WORKER_LIST[i]} stopped" 
<br>
 RETVAL=$? 
<br>
 [ $RETVAL=0 ] &amp;&amp; rm -f /var/lock/subsys/${WORKER_LIST[i]} /var/run/${WORKER_LIST[i]}.pid 
<br>
 fi 
<br>
} 
<br>
<br>
status() { 
<br>
 for (( i = 0 ; i &lt; ${#WORKER_LIST[@]} ; i++ )) 
<br>
 do 
<br>
 SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep ${WORKER_AJP_PORT[i]}|wc -l` 
<br>
 if [ $SHUTDOWN_PORT -eq 0 ]; then 
<br>
 echo "Tomcat ${WORKER_LIST[i]} stopped" 
<br>
 else 
<br>
 MODE="normal" 
<br>
 JPDA_PORT=`netstat -vatn|grep LISTEN|grep 8000|wc -l` 
<br>
 if [ $JPDA_PORT -ne 0 ]; then 
<br>
 MODE="debug" 
<br>
 fi 
<br>
 echo "Tomcat ${WORKER_LIST[i]} running in $MODE mode" 
<br>
 fi 
<br>
 done 
<br>
} 
<br>
<br>
case "$1" in 
<br>
 start) 
<br>
 start 0 
<br>
 start 1 
<br>
 ;; 
<br>
 debug) 
<br>
 debug 0 
<br>
 debug 1 
<br>
 ;; 
<br>
 stop) 
<br>
 stop 0 
<br>
 stop 1 
<br>
 ;; 
<br>
 restart) 
<br>
 stop 0 
<br>
 start 0 
<br>
 stop 1 
<br>
 start 1 
<br>
 ;; 
<br>
 redebug) 
<br>
 stop 0 
<br>
 debug 0 
<br>
 stop 1 
<br>
 debug 1 
<br>
 ;; 
<br>
 status) 
<br>
 status 
<br>
 ;; 
<br>
 *) 
<br>
 echo "Usage: $0 {start|debug|stop|restart|redebug|status}" 
<br>
 exit 1 
<br>
esac 
<br>
<br>
exit $RETVAL' &gt; /etc/rc.d/init.d/tomcat 
<br>
<br>
chmod 755 /etc/rc.d/init.d/tomcat 
<br>
/sbin/chkconfig --add tomcat 
<br>
/sbin/chkconfig --level 235 tomcat on 
<br>
<br>
echo '/sbin/chkconfig tomcat off' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
echo '/sbin/chkconfig --del tomcat' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
echo 'rm -rf /etc/rc.d/init.d/tomcat' &gt;&gt; ${SETUP_DIR}/cleanup.sh 
<br>
<br>
chmod 755 ${SETUP_DIR}/cleanup.sh 
<br>
<br>
echo 'Tomcat installation is completed!' 
<br>
echo 
<br>
echo 'You can type "/etc/init.d/tomcat start" to start using tomcat now.' 
<br>
echo 'You can type "/etc/init.d/httpd start" to start using apache now.' 
<br>
echo 'Open a browser, and go to http://'${hostname}' to see it.' 
<br>
<br>
# 
<br>
# modify web.xml for cluster test 
<br>
# 
<br>
sed -i -e "26a\ &lt;distributable/&gt;" ${WEBAPP_ROOT}/${hostname}/WEB-INF/web.xml 
<br>
<br>
# 
<br>
# generate clustertest.jsp for cluster test 
<br>
# 
<br>
echo '&lt;%@ page contentType="text/html; charset=UTF-8" import="java.util.*"%&gt; 
<br>
&lt;html&gt; 
<br>
&lt;head&gt; 
<br>
&lt;title&gt;Cluster Session Replication Test&lt;/title&gt; 
<br>
&lt;/head&gt; 
<br>
&lt;body&gt; 
<br>
&lt;% 
<br>
 out.println("Session ID=" + session.getId() + "&lt;br&gt;"); 
<br>
<br>
 String key = request.getParameter("key"); 
<br>
 if (key != null &amp;&amp; key.length() &gt; 0) { 
<br>
 String value = request.getParameter("value"); 
<br>
 session.setAttribute(key, value); 
<br>
 } 
<br>
<br>
 out.println("&lt;b&gt;Session Listing&lt;/b&gt;&lt;br&gt;"); 
<br>
<br>
 Enumeration e = session.getAttributeNames(); 
<br>
 while (e.hasMoreElements()) { 
<br>
 String name = (String) e.nextElement(); 
<br>
 String value = (String)session.getAttribute(name); 
<br>
 out.println(name + " = " + value + "&lt;br&gt;"); 
<br>
 } 
<br>
%&gt; 
<br>
<br>
 &lt;form method="post"&gt; 
<br>
 key:&lt;input type="text" size="20" name="key"&gt; 
<br>
<br>
 value:&lt;input type="text" size="20" name="value"&gt; 
<br>
<br>
 &lt;input type="submit"&gt; 
<br>
 &lt;/form&gt; 
<br>
&lt;/body&gt; 
<br>
&lt;/html&gt;' &gt; ${WEBAPP_ROOT}/${hostname}/clustertest.jsp 
<br>
[/code] 
<br>
<br>
4.Then you can use the following two commands to do step12 
<br>
service tomcat start 
<br>
service httpd start 
<br>
5. For testing, you can use the following command to shutdown or startup a single Tomcat worker: 
<br>
su -l tomcat -c "export CATALINA_BASE=/var/robust/worker1; /var/tomcat7/bin/shutdown.sh" 
<br>
su -l tomcat -c "export CATALINA_BASE=/var/robust/worker1; /var/tomcat7/bin/startup.sh" 
<br>
su -l tomcat -c "export CATALINA_BASE=/var/robust/worker2; /var/tomcat7/bin/shutdown.sh" 
<br>
su -l tomcat -c "export CATALINA_BASE=/var/robust/worker2; /var/tomcat7/bin/startup.sh" 
<br>
<br>
Or you may use a shell script to restart Tomcat repeatedly to verify if the session replication really works. 
<br>
[b]restart.sh[/b] 
<br>
[code] 
<br>
 for (( i = 0 ; i &lt; 10 ; i++ )) 
<br>
 do 
<br>
 service tomcat restart 
<br>
 done 
<br>
[/code] 
<br>
<br>
If you can see all of the session objects no matter how Tomcat is restarted, then Tomcat clustering(session replication) is done. 
<br>
<br>
Note: If you have enabled firewall like iptables, please add http(80:tcp), https(443:tcp) and [color=red]45564:udp[/color] port to the allowed list. 
<br>
<br>
6. Finally, browse https://&lt;your_server_ip&gt;/ to check SSL works. You have to add this site as your trust site because we use a self-signed SSL certificate. 
<br>
<br>
Reference: 
<br>
http://tomcat.apache.org/connectors-doc/reference/apache.html 
<br>
http://tomcat.apache.org/connectors-doc/generic_howto/loadbalancers.html 
<br>
http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html 
<br>
http://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html 
<br>
http://tomcat.apache.org/tomcat-7.0-doc/logging.html 
<br>
http://www.easywayserver.com/tomcat-clustering.htm 
<br>
http://www.easywayserver.com/implementation-tomcat-clustering.htm]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/556/1151.page</guid>
				<link>https://forum.andowson.com/posts/preList/556/1151.page</link>
				<pubDate><![CDATA[Thu, 29 Sep 2011 17:29:54]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>