Friday, 13 December 2013

Installing Apache Tomcat

Installing Apache Tomcat

Tomcat is a free, open-source implementation of Java Servlet and JavaServer Pages technologies developed under the Jakarta project at the Apache Software

INSTALLING TOMCAT

First we need to install java. Download JDK from http://www.oracle.com/technetwork/java/javase/install-linux-64-self-extracting-142068.html


Installing Java Runtime Environment
Download the binary from http://www.oracle.com/technetwork/java/javase/install-linux-64-self-extracting-142068.htm

root@linux [~]# wget http://www.oracle.com/xxxxx/jdk-7-linux-i586.bin
root@linux [~]# ./jdk-7-linux-i586.bin
root@linux [~]# JAVA_HOME=/usr/java/jdk
root@linux [~]# export PATH=$JAVA_HOME/bin:$PATH
root@linux [~]# which java
/usr/local/jdk/bin/java

Download the apache-tomcat source
root@linux [~]# http://apache.osuosl.org/tomcat/tomcat-7/v7.0.0/bin/apache-tomcat-7.0.0.tar.gz
root@linux [~]# tar -xvzf apache-tomcat-7.0.0.tar.gz
root@linux [~]# mv apache-tomcat-7.0.0 /usr/local/tomcat
root@linux [~]# groupadd tomcat
root@linux [~]# useradd -g tomcat -s /usr/sbin/nologin -m -d /home/tomcat tomcat

CATALINA_HOME is the directory where Tomcat is installed and here CATALINA_HOME=/usr/local/tomcat

Check the version and environment variables of tomcat installed as mentioned below.

root@linux [~]# /usr/local/tomcat/bin/version.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar

Add the following variables in /etc/profile.

Open the file /etc/profile and add

JAVA_HOME=”/usr/java/jdk”
export JAVA_HOME
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/java/jdk/bin
export PATH
CATALINA_HOME=”/usr/local/tomcat”
export CATALINA_HOME
CATALINA_BASE=”/usr/local/tomcat”
export CATALINA_BASE
CLASSPATH=$CATALINA_HOME/lib/servlet-api.jar
export CLASSPATH

Done with the installation of Java and Tomcat. The tomcat and apache will be working at this moment. You just need to connect tomcat & apache using the most commonly used connector known as mod_jk. Mod_Jk is Tomcat-Apache plug-in that handles the communication between Tomcat and Apache.

You can either use mod_jk or mod_proxy_ajp to forward the request to tomcat. Lets see how to configure mod_proxy_ajp because mod_proxy_ajp is easy to configure.

If you are having a domain and if you need to forward all request to tomcat, configure the virtual host entry for the domain as listed below.

All request to Tomcat:

<VirtualHost IP>
       ServerName www.example.com
       ServerAlias example.com
       DirectoryIndex index.jsp index.php index.html
       DocumentRoot /home/user/public_html
       ProxyPass / ajp://127.0.0.1:8009
</VirtualHost>

Only one particular directory to Tomcat:

<VirtualHost IP>
       ServerName www.example.com
       ServerAlias example.com
       DirectoryIndex index.jsp index.php index.html
       DocumentRoot /home/user/public_html
       ProxyPass /myApp ajp://127.0.0.1:8009/MyApp
</VirtualHost>

In the above case, only http://domainname.com/MyApp will be served by Tomcat.

If you still wish to use mod_jk connector, refer the steps below…

Downloading and installing mod_jk connector

root@linux [~]# wget http://www.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.37-src.tar.gz
root@linux [~]# tar -xvf tomcat-connectors-1.2.37-src.tar.gz
root@linux [~]# cd tomcat-connectors-1.2.37-src.tar.gz/native
root@linux [~]# ./buildconf.sh
root@linux [~]# ./configure –with-apxs=/usr/local/apache/bin/apxs
root@linux [~]# make
root@linux [~]# make install

The mod_jk.so will be generated in tomcat-connectors-1.2.37-src.tar.gz/native/apache directory. Copy the so file to apache modules directory

root@linux [~]# cp -rp tomcat-connectors-1.2.37-src/native/apache/mod_jk.so /usr/local/apache/modules/

Add the following entry in httpd.conf file.

LoadModule jk_module modules/mod_jk.so
Include “/usr/local/apache/conf/jk.conf”

Open /usr/local/apache/conf/jk.conf and add the following

JkWorkersFile /usr/local/tomcat/conf/jk/workers.properties
JkLogFile /usr/local/apache/logs/mod_jk.log
JkLogLevel info
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] ”
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat “%w %V %T

Create the Jkworkers file as mentioned : /usr/local/tomcat/conf/jk/workers.properties

Add the entries as listed below.

workers.tomcat_home= /usr/local/tomcat
workers.java_home=/usr/java/jdk
ps=/
worker.list=ajp13
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13

Now you need to add virtual host entry for the domain in server.xml file.

Open the file /usr/local/tomcat/conf/server.xml and add the following.

<Host name=”domain1.com” debug=”0″ appBase=”webapps” unpackWARs=”true”> <Alias>www.domain1.com</Alias> <Logger className=”org.apache.catalina.logger.FileLogger” directory=”logs” prefix=”virtual_log1.” suffix=”.log” timestamp=”true”/> <Context path=”" docBase=”/home/user/public_html” debug=”0″ reloadable=”true”/> </Host>

Add the mod_jk module entry for the domain in httpd.conf.

Open /usr/local/apache/conf/httpd.conf with our favourite editor and add the entries as listed below in the virtual host entry of domain.

<IfModule mod_jk.c>
JkMount /*.do ajp13
JkMount /*.jsp ajp13
JkMount /servlet/* ajp13
JkMount /servlets/* ajp13
JkMount /manager/* ajp13
</IfModule>

Restart apache and tomcat servers

root@linux [~]# /etc/rc.d/init.d/httpd restart
root@linux [~]# /usr/local/tomcat/bin/startup.sh

Place a test.jsp page with the following code and access the page.

<html>
  <head>
  <title>Hello from JSP</title>
  <%!
  String message = "Hello, World. From JSP test page Tomcat is running.";
  %>
  </head>
  <body>
  <hr color="#000000" />
  <center>
  <h2><font color="#3366cc"><%= message%></font></h2>
  <h3><font color="#0000ff"><%= new java.util.Date() %> </font></h3>
  <hr color="#000000" />
  </center>
  </body>
  </html>

No comments:

Post a Comment