Tomcat with Apache2 VirtualHost AJP (mod_jk)

Tomcat changes:

You should be able to visit http://yourip:8080/manager/html and the below:

Deploy your tomcat application sample.war (433.79 kb) using the manager.

Note down the deployed application name (sample).

Edit /etc/tomcat7inst/8xx/conf/server.xml with following lines AFTER the ending </Host> tag, and BEFORE the ending </Engine> tag, insert the following:

<!– sample.com (here the appname is derived from the deploy above)–>
<Host name="
www.sample.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Alias>sample.com</Alias>
<Context path="" docBase="sample" debug="0" reloadable="true"/>
</Host>

Find the following in the same file (server.xml) and uncomment by removing <!– and –>
< !–
<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />
–>

<Connector port="8xx8" protocol="AJP/1.3" redirectPort="8443" />

Edit /etc/tomcat7inst/880/bin/startup.sh

export JAVA_OPTS="
-Dcom.qbw.instance=880
-Xms1g -Xmx1g -XX:MaxPermSize=128m
-Djava.rmi.server.hostname=192.168.1.186
"


Issue following the following command to activate the tomcat virtual host settings:

/etc/init.d/tomcat8xx stop

/etc/init.d/tomcat8xx start

Apache2 changes with load balancer:

Install the modjk module using following commad:

apt-get install libapache2-mod-jk

Files created:

  • /etc/libapache2-mod-jk/workers.properties
  • /var/log/apache2/mod_jk.log
  • /var/log/apache2/jk-runtime-status

Edit /etc/libapache2-mod-jk/workers.properties

#workers.tomcat_home=/usr/share/tomcat6
#workers.java_home=/usr/lib/jvm/default-java
# Change to your port
worker.ajp13_worker.port=8108
#Change to your Tomcat IP
worker.ajp13_worker.host=192.168.1.126
worker.ajp13_worker.type=ajp13

Create a apache2 virtualhost for eg. sample.com.conf with following contents on it.

<VirtualHost *:80>
ServerName
www.sample.com
ServerAlias sample.com
ServerAdmin webmaster@sample.com
JkMount /* ajp13_worker
CustomLog /srv/www-logs/sample.com.access.log combined
ErrorLog /srv/www-logs/sample.com.error.log
</VirtualHost>

<VirtualHost sample.com:80>
ServerName sample.com
ServerAlias sample.com

DocumentRoot /var/lib/tomcat6/webapps/appname/
<Directory /var/lib/tomcat6/webapps/appname/>
Options FollowSymLinks Indexes
AllowOverride None
</Directory>
JkMount /* ajp13_worker
</VirtualHost>

Save conf file

Enable site:

cd /etc/apache2/sites-available

a2ensite sample.com.conf

Restart apache2 with following command /etc/init.d/apache2 restart

Update your hosts file to point sample.com to ip 192.168.1.186 (apache server ip)

Figures crossed. If every is followed as specified, the tomcat webapp deployed as appname shall be visible in your browser as simple apache2 page such as http://abc.com as opposed to http://abc.com:8080

Benefit: From now on, we just need to deploy the tomcat application and finally apache2 virtualhost creation followed by a restart to activate the new settings.


Tag Cloud