Monday, April 16, 2012

How to configure apache-tomcat on linux box?

■  Purpose : Configure apache-tomcat 
■  OS Environment : Linux (RHEL, Centos)
■  Required Applications: JDk 1.7.0_03, Apache tomcat 6.0.35
■ Implementation Steps :

1.  JDK setup :

$ pwd
/jdk1.7.0_03
$ export JAVA_HOME=/jdk1.7.0_03
$ export PATH=$JAVA_HOME/bin:$PATH

$ which java
/jdk1.7.0_03/bin/java

$  java -version
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b04)
Java HotSpot(TM) 64-Bit Server VM (build 22.1-b02, mixed mode)

2. Apache Tomcat setup :

$ groupadd tomcat
$ useradd -g tomcat -s /usr/sbin/nologin -m -d /home/tomcat tomcat
$ cd /var/lib
$ tar zxvf /usr/local/src/apache-tomcat-6.0.35.tar.gz
$ chown -R tomcat.tomcat /var/lib/apache-tomcat-6.0.35

3. Verify tomcat version  :

$ /var/lib/apache-tomcat-6.0.35/bin/version.sh

Output will look like below :
   
Using CATALINA_BASE: /var/lib/apache-tomcat-6.0.35
Using CATALINA_HOME: /var/lib/apache-tomcat-6.0.35
Using CATALINA_TMPDIR: /var/lib/apache-tomcat-6.0.35/temp
Using JRE_HOME: /jdk1.7.0_03
Using CLASSPATH: /var/lib/apache-tomcat-6.0.35/bin/bootstrap.jar
Server version: Apache Tomcat/6.0.35
Server built: Nov 28 2011 11:20:06
Server number: 6.0.35.0
OS Name: Linux
OS Version: 2.6.18-308.el5
Architecture: amd64
JVM Version: 1.7.0_03-b04
JVM Vendor: Oracle Corporation

4. Starting/Stopping Tomcat:

$ export CATALINA_HOME=/var/lib/apache-tomcat-6.0.35
$ export CATALINA_BASE=/var/lib/apache-tomcat-6.0.35
$ su -p -s /bin/sh tomcat $CATALINA_HOME/bin/startup.sh

You can make posix compliant script to run above script during booting. Export can be mentioned in root user's profile.

Output will look like  :

Using CATALINA_BASE: /var/lib/apache-tomcat-6.0.35
Using CATALINA_HOME: /var/lib/apache-tomcat-6.0.35
Using CATALINA_TMPDIR: /var/lib/apache-tomcat-6.0.35/temp
Using JRE_HOME: /jdk1.7.0_03
Using CLASSPATH: /var/lib/apache-tomcat-6.0.35/bin/bootstrap.jar

5. Check if tomcat process is running or not :

$ ps aux |grep java
tomcat 10711 9.4 10.1 463404 51696 pts/1 Sl 18:58 0:02 /jdk1.7.0_03/bin/java -Djava.util.logging.config.file=/var/lib/apache-tomcat-6.0.35/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/var/lib/apache-tomcat-6.0.35/endorsed -classpath /var/lib/apache-tomcat-6.0.35/bin/bootstrap.jar -Dcatalina.base=/var/lib/apache-tomcat-6.0.35 -Dcatalina.home=/var/lib/apache-tomcat-6.0.35 -Djava.io.tmpdir=/var/lib/apache-tomcat-6.0.35/temp org.apache.catalina.startup.Bootstrap start
root 10730 0.0 0.1 61232 724 pts/1 R+ 18:58 0:00 grep java


6. Shutdown tomcat application :

$ su -p -s /bin/sh tomcat $CATALINA_HOME/bin/shutdown.sh

Output will look like :

Using CATALINA_BASE: /var/lib/apache-tomcat-6.0.35
Using CATALINA_HOME: /var/lib/apache-tomcat-6.0.35
Using CATALINA_TMPDIR: /var/lib/apache-tomcat-6.0.35/temp
Using JRE_HOME: /jdk1.7.0_03
Using CLASSPATH: /var/lib/apache-tomcat-6.0.35/bin/bootstrap.jar

$ ps aux |grep java
root 10763 0.0 0.1 61232 724 pts/1 R+ 18:59 0:00 grep java

7. Switching to Tomcat User Account :

$ su - -s /bin/sh tomcat
$ id
uid=502(tomcat) gid=503(tomcat) groups=503(tomcat) context=root:system_r:unconfined_t:SystemLow-SystemHigh


Control+D to exit from tomcat shell

8. Setting Up First Tomcat JVM Instance :

$ mkdir -p /opt/tomcat-instance/sales.example.com
$ cd /opt/tomcat-instance/sales.example.com
$ cp -a /var/lib/apache-tomcat-6.0.35/conf .
$ mkdir common logs temp server shared webapps work
$ chown -R tomcat.tomcat /opt/tomcat-instance

9. Environment setup for instance :

$ su - -s /bin/sh tomcat
$ id
uid=1001(tomcat) gid=1001(tomcat) groups=1001(tomcat)
$ pwd
/home/tomcat
$ whoami
tomcat

$ cat /opt/tomcat-instance/sales.env 

export JAVA_HOME=/jdk1.7.0_03
export PATH=\$JAVA_HOME/bin:\$PATH
export CATALINA_HOME=/var/lib/apache-tomcat-6.0.35
export CATALINA_BASE=/opt/tomcat-instance/sales.example.com

10. Configuring Tomcat Network Ports : Since this is the first Tomcat instance that's being created here, the default port numbers can be left unchanged in $CATALINA_BASE/conf/server.xml (/opt/tomcat-instance/sales.example.com/conf/server.xml):

11. Starting First Tomcat Instance :

$ source /opt/tomcat-instance/sales.env
$ $CATALINA_HOME/bin/startup.sh


Output will look like :

Using CATALINA_BASE: /opt/tomcat-instance/sales.example.com
Using CATALINA_HOME: /var/lib/apache-tomcat-6.0.35
Using CATALINA_TMPDIR: /opt/tomcat-instance/sales.example.com/temp
Using JRE_HOME: /jdk1.7.0_03
Using CLASSPATH: /var/lib/apache-tomcat-6.0.35/bin/bootstrap.jar


12. Setting Up a Web Application for First Tomcat JVM Instance :

$ vi $CATALINA_BASE/conf/server.xml :


 docBase attribute is set to mysales which stands for the application name within the URL, i.e. "http://serverip/mysales" or "http://serverip:8080/mysales".

13. Setup home page for web Application :

$ cat $CATALINA_BASE/webapps/sales/index.html 


"http://www.w3.org/TR/html4/loose.dtd"

14. Restart First Tomcat Instance :

$ source /opt/tomcat-instance/sales.env
$ $CATALINA_HOME/bin/shutdown.sh

$ $CATALINA_HOME/bin/startup.sh

14. Test :
Access http://serverip/mysales/ , you'll see "Apache Tomcat Sales Home Page".

15. Deploying Java Servlet for Web Application in First Tomcat JVM Instance :

a. Setting up Java Servlet Layout :

$ mkdir -p $CATALINA_BASE/webapps/sales/WEB-INF/classes

$ mkdir $CATALINA_BASE/webapps/sales/WEB-INF/lib
b. Create a Java Servlet :

vi $CATALINA_BASE/webapps/sales/WEB-INF/classes/Sales.java  and put following entries in this file :

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Sales extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("Sales Page");
out.println("");
out.println("");
out.println("

Executing Sales ...

");
out.println("");
out.println("");
}
}


c. Compile above java program :


$ cd $CATALINA_BASE/webapps/sales/WEB-INF/classes
$ javac -classpath "$CATALINA_HOME/lib/*" Sales.java
$ ls
Sales.class Sales.java


d. Configuring the Java Servlet :

$ cat $CATALINA_BASE/webapps/sales/WEB-INF/web.xml
servlet_sales
Sales
servlet_sales
/execute

e. Modify index.html &  put some entries :

$ vi $CATALINA_BASE/webapps/sales/index.html

16. Testing and Executing the Java Servlet :

 $ source /opt/tomcat-instance/sales.env
$ $CATALINA_HOME/bin/shutdown.sh
$ $CATALINA_HOME/bin/startup.sh


17. Out of  http://serverip/mysales/  will be 

Apache Tomcat Sales Home Page
Execute Sales

Now click on "Execute Sales", you'll see following :

Executing Sales ...

No comments:

Post a Comment