CentOS 5
Sponsored Link

JAVA Apprication Server - Tomcat6
2008/08/31
  Install Tomcat6.
JDK is requied to installed and WEB Server is also requied to installed.

[1] Install and configure Tomcat6. Make sure Get latest Tomcat 6 from here.
[root@www ~]#
wget http://ftp.riken.jp/net/apache/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.tar.gz

[root@www ~]#
tar zxvf apache-tomcat-6.0.20.tar.gz

[root@www ~]#
mv apache-tomcat-6.0.20 /usr/tomcat6

[root@www ~]#
useradd -d /usr/tomcat6 -s /sbin/nologin tomcat

useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[root@www jsvc-src]#
chown -R tomcat. /usr/tomcat6
[2] Create INIT script.
[root@www ~]#
cd /usr/tomcat6/bin/

[root@www ~]#
tar zxvf jsvc.tar.gz

[root@www ~]#
cd jsvc-src

[root@www jsvc-src]#
[root@www jsvc-src]#
sh configure --with-java=/usr/java/default

[root@www jsvc-src]#
[root@www jsvc-src]#
chown tomcat. jsvc

[root@www jsvc-src]#
cp -p jsvc ../

[root@www jsvc-src]#
[root@www ~]#
vi /etc/rc.d/init.d/tomcat


# an example

#!/bin/bash
#
# chkconfig: - 90 10
# description: Tomcat 6 - Java apprication server

. /etc/init.d/functions

JAVA=/usr/java/default
CATALINA_HOME=/usr/tomcat6
TOMCAT_USER=tomcat
TMP_DIR=/tmp
CATALINA_OPTS=
CLASSPATH=\
$JAVA/lib/tools.jar:\
$CATALINA_HOME/bin/commons-daemon.jar:\
$CATALINA_HOME/bin/bootstrap.jar
PIDFILE=/var/run/tomcat.pid
LOCKFILE=/var/lock/subsys/tomcat
DAEMON=/bin/jsvc

start(){
    echo -n "Starting jsvc: "
    $DAEMON \
    -pidfile $PIDFILE \
    -user $TOMCAT_USER \
    -home $JAVA \
    -Dcatalina.home=$CATALINA_HOME \
    -Djava.io.tmpdir=$TMP_DIR \
    -outfile $CATALINA_HOME/logs/catalina.out \
    -errfile '&1' \
    $CATALINA_OPTS \
    -cp $CLASSPATH \
    org.apache.catalina.startup.Bootstrap

    RETVAL=$?
    if [ $RETVAL = 0 ]; then
       echo_success
       touch $LOCKFILE
    else
       echo_failure
    fi
    echo
}

stop(){
    echo -n "Shutting down jsvc: "
    $DAEMON \
    -stop \
    -pidfile $PIDFILE
    org.apache.catalina.startup.Bootstrap
    RETVAL=$?
    if [ $RETVAL = 0 ]; then
       echo_success
       rm -f $PIDFILE $LOCKFILE
    else
       echo_failure
    fi
    echo
}

case "$1" in
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       status $DAEMON
       RETVAL=$?
       ;;
    *)
       echo $"Usage: jsvc {start|stop|restart|status}"
       exit 1
       ;;
esac

[root@www ~]#
chmod 755 /etc/rc.d/init.d/tomcat

[root@www ~]#
/etc/rc.d/init.d/tomcat start

Starting jsvc:
[  OK  ]

[root@www ~]#
chkconfig --add tomcat

[root@www ~]#
chkconfig tomcat on
[3] Access to "http://(your hostname or IP address):8080/" with Web browser and make sure following tomcat site is shown normally.
 
[4] Configure to be able to access without specify 8080 in URL.
[root@www ~]#
vi /etc/httpd/conf.d/proxy_ajp.conf


# add at the bottom

ProxyPass /tomcat/ ajp://localhost:8009/


[root@www ~]#
/etc/rc.d/init.d/httpd restart

Stopping httpd:
[  OK  ]

Starting httpd:
[  OK  ]
[5] Access to "http://(your hostname or IP address)/tomcat/" with web browser and verify tomcat site like [3] section is shown normally.

[6] Create a test servlet that shows current day and time and make sure uf it is working normally.
[root@www ~]#
mkdir /usr/tomcat6/webapps/ROOT/WEB-INF/classes

[root@www ~]#
chown tomcat. /usr/tomcat6/webapps/ROOT/WEB-INF/classes

[root@www ~]#
cd /usr/tomcat6/webapps/ROOT/WEB-INF/classes

[root@www classes]#
vi daytime.java


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

public class daytime extends HttpServlet {

public void doGet(HttpServletRequest request

 
,HttpServletResponse response)

throws IOException, ServletException{


response.setContentType("text/html");

PrintWriter out = response.getWriter();

Calendar cal = Calendar.getInstance();

out.println("<html>");

out.println("<head>");

out.println("<title>DayTime</title>");

out.println("</head>");

out.println("<body>");

out.println("<div style=\"font-size: 40px; text-align: center; font-weight: bold\">");

out.println(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE));


out.println("</div>");

out.println("</body>");

out.println("</html>");

}

}


[root@www classes]#
javac -classpath /usr/tomcat6/lib/servlet-api.jar daytime.java

[root@www classes]#
vi /usr/tomcat6/webapps/ROOT/WEB-INF/web.xml


# insert following lines between <web-app> and </web-app>

<servlet>
   <servlet-name>daytime</servlet-name>
   <servlet-class>daytime</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>daytime</servlet-name>
   <url-pattern>/daytime</url-pattern>
</servlet-mapping>


</web-app>
[7] Access to "http://(your hostname or IP address)/tomcat/daytime" and make sure following page is shown normally.
 
Matched Content
 
Tweet