How do I host Java application using Apache AJP connector

http://www.serveridol.com/2011/03/18/how-do-i-host-java-application-using-apache-ajp-connector/

=============================================

Here I’m going to host a java application on my development server which had capable of hosting php/rails sites.

For hosting a Java application, You may complete the following steps.

Packages required

1.mod_jk apache module <
2. Tomcat 6.0.29
3. JDK 1.6.0_14
4. Apache 2.2.3

1. Install mod_jk

Download the the mod_jk built for your Apache version from here http://download.filehat.com/apache//tomcat/tomcat-connectors/jk/binaries/linux/ and copy it to /usr/lib/httpd/modules/ and rename it to mod_jk.so
[root@rc-040 conf]# ls -la /usr/lib/httpd/modules/mod_jk.so
-rw-r–r– 1 root root 416473 Nov 1 10:12 /usr/lib/httpd/modules/mod_jk.so
Now I’m going to create a jk.conf file and workers.properties in “/etc/httpd/conf.d”

#vi /etc/httpd/conf.d/jk.conf

LoadModule jk_module /usr/lib/httpd/modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkShmFile /var/log/httpd/mod_jk.shm
JkLogFile /var/log/httpd/mod_jk.log
JkLogLevel info
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] ”

#vi /etc/httpd/conf.d/workers.properties

# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

# service httpd restart

2. Install Java and Apache Tomcat
I installed Java in ” /usr/java” and tomcat in “/usr/local” and set a permanent environment variable for JAVA_HOME and CATALINA_HOME by copying following variables in s script. My java.sh having
a. Setting up Java variables

# vi /etc/profile.d/java.sh

JAVA_HOME=/usr/java/jdk1.6.0_14
JAVA_FONTS=/usr/share/fonts/truetype
ANT_HOME=/usr/share/ant
JAVACC_HOME=/usr/java/javacc
SPRING_HOME=/usr/local/spring-framework-1.1.4
CLASSPATH=.:$JAVA_HOME/lib
CATALINA_BASE=/usr/local/apache-tomcat-6.0.29
CATALINA_HOME=/usr/local/apache-tomcat-6.0.29
CATALINA_TMPDIR=/usr/local/apache-tomcat-6.0.29/temp
JBOSS_HOME=/usr/local/jboss-4.0.3SP1
PATH=$JAVA_HOME:$JAVA_HOME/bin:$JAVACC_HOME/bin:$ANT_HOME/bin:$CATALINA_HOME/bin:$JBOSS_HOME/bin:ANT_HOME/bin:/usr/local/godesk/usr/bin:$PATH
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/glib-2.0.pc
export PKG_CONFIG_PATH
LD_LIBRARY_PATH=/usr/local/lib;export LD_LIBRARY_PATH;
export PATH JAVA_HOME JAVA_FONTS ANT_HOME JAVACC_HOME CATALINA_BASE CATALINA_HOME CATALINA_TMPDIR JBOSS_HOME
export CLASSPATH

# cp java.sh /etc/profile.d/
# source /etc/profile.d/java.sh
[root@rc-040 public_html]# echo $JAVA_HOME $CATALINA_HOME
/usr/java/jdk1.6.0_14 /usr/local/apache-tomcat-6.0.29

Perfect !

b. Configuring Tomcat

Ajp connector will enabled by default in Tomcat which running on the port 8009 ( check the server.xml file). Ensure that follwoing lines are commented out.

Now you need to Add virtual Host entry in Tomcat server.xml file. Open “/usr/local/apache-tomcat-6.0.29/conf/server.xml” and find the line starts with <Engine name=”Catalina” . Add the following entry after this line and restart the tomcat.

<Engine name=”Catalina” defaultHost=”localhost”>
<Host name=”dplpool.rainconcert.in” appBase=”/home/dplpool/public_html”
autoDeploy=”true” unpackWARs=”true”
xmlValidation=”false” xmlNamespaceAware=”false” >
<Alias>selfcare.rainconcert.in</Alias>
< Context path=”/newapp” docBase=”.”/>
< /Host>

Here are the description of each parameter given,

1. name : Your domain name
2. appBase : Where .war file to be placed (it would be a ftp location in most cases)
3. autoDeploy : This is ideal for development purpose and must be set to “false” in Prod enviorment
4. unpackWAR : Tomcat is exploded the war if it is set to true file by looking for if any class file changes in “WEB-INF” folder
5. Alias : Domain alias, can added any number of domains to use the same hosting space
6. Context path : Like location alias in Apache. Suppose if you want to access the images over the http://domain.tld/images, you need to set in this virtual host

3. Apache Configuration.
I have placed a virtualhost config. file (javapp.conf” under “/etc/httpd/conf.d” having the following content.

< VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /home/dplpool/public_html
DirectoryIndex index.html
Alias / /home/dplpool/public_html
ErrorLog logs/myapp-javahost.log
JkMount /*.jsp worker1
JkMount /* worker1

</VirtualHost>

NB: You need to usr the exact identical hostname in Apache virtualhost and Tomcat virtual host. Otherwise AJP connector can’t identify the workers which JkMount being looked for.

Restart Apache and Tomcat

Testing new hosting,

You can download the sample java application provided by the Apache project. Download this file http://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war and rename it to ROOT.war. Then upload it to “/home/domain/public_html”. You will see the site at http://domain.tld


Tag Cloud