Sunday, March 3, 2013

Tomcat Installation in CentOS


Tomcat is a Java servlet container and web server from the Apache Software Foundation. Tomcat is written in Java, which prompts the need to have  Java runtime installed before we can build or test it.

There are two ways to install tomcat on CentOS
  • Using the package manager yum
  • Downloading the latest Apache multiplatform binary release
Installing Tomcat Using yum

1) Prepare the repository
yum install yum-priorities

2) Install Java
yum -y install java

3) Search for tomcat packages to be installed
yum search tomcat6
N/S Matched: tomcat6 
tomcat6.noarch : Apache Servlet/JSP Engine, RI for Servlet 2.5/JSP 2.1 API
tomcat6-admin-webapps.noarch : The host-manager and manager web applications for
                             : Apache Tomcat
tomcat6-docs-webapp.noarch : The docs web application for Apache Tomcat
tomcat6-el-2.1-api.noarch : Expression Language v1.0 API
tomcat6-javadoc.noarch : Javadoc generated documentation for Apache Tomcat
tomcat6-jsp-2.1-api.noarch : Apache Tomcat JSP API implementation classes
tomcat6-lib.noarch : Libraries needed to run the Tomcat Web container
tomcat6-log4j.noarch : Log4J support for Apache Tomcat
tomcat6-servlet-2.5-api.noarch : Apache Tomcat Servlet API implementation classes
tomcat6-webapps.noarch : The ROOT and examples web applications for Apache Tomcat

4) Install tomcat - We shall just install  the following packages - tomcat6, tomcat6-webapps, tomcat6-admin-webapps

yum -y install tomcat6 tomcat6-webapps tomcat6-admin-webapps

5) Start tomcat6

service tomcat6 status
tomcat6 is stopped                                         [  OK  ]

# service tomcat6 start
Starting tomcat6:                                          [  OK  ]

# service tomcat6 status
tomcat6 (pid 7891) is running...                           [  OK  ]

Verify if tomcat is listening on port 8080
# netstat -nlp | grep 8080
tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN      7891/java


File Structure of tomcat6 when installed using yum


The Red Hat file structure is different than the default file structure Tomcat 6 has when installing from source. Here is the file structure that is used when installing using yum method:

The below command shall provide an idea of file structure for tomcat
# rpm -ql tomcat6

Some major files are as follows
/etc/tomcat6 (this is where the main tomcat config files reside)
/usr/share/doc/usr/share/tomcat6
/usr/share/tomcat6/bin
/usr/share/tomcat6/conf
/usr/share/tomcat6/lib
/usr/share/tomcat6/logs
/usr/share/tomcat6/temp
/usr/share/tomcat6/webapps
/usr/share/tomcat6/work
/var/cache/tomcat6
/var/cache/tomcat6/temp
/var/cache/tomcat6/work
/var/lib/tomcat6 (this is where you will add and/or change most of your files)
/var/lib/tomcat6/webapps
/var/log/tomcat6

Installing Tomcat7 using Apache mutliplatform binary release


Just followed the steps in http://www.davidghedini.com/pg/entry/install_tomcat_7_on_centos


From http://tomcat.apache.org/, get to know the latest version of Tomcat available. The latest version is Tomcat 7. Tomcat 7 implements the JavaServer Pages 2.2 and Servlet 3.0 specifications

First we need to download and install

1) JDK
2) JRE

for CentOS.

JRE (Java Runtime Environment) is needed to run Java applications and applets on our system.

To develop Java applications and applets, you need the JDK (Java Development Kit), which includes the JRE.

Installing JDK 7(1.7)

The latest JDK available is JDK 7.  Tomcat 7 requires a minimum of JDK 6

At the time of installation, the JDK available for download was "JDK 7 Update 9"

The JDK is specific to 32-bit and 64-bit versions. My CentOS is a 64-bit version. Hence I  download jdk-7u9-linux-x64.tar.gz

Start by creating a new directory /usr/java

# mkdir /usr/java
# cd /usr/java

Extract the package jdk-7u9-linux-x64.tar.gz in this directory.

# tar xzvf jdk-7u9-linux-x64.gz

This will create the directory /usr/java/jdk1.7.0_09. This will be our JAVA_HOME.

We can now set JAVA_HOME and put Java into the path of our users.

To set it for your current session, you can issue the following from the CLI:
# JAVA_HOME=/usr/java/jdk1.7.0_09
# export JAVA_HOME
# PATH=$JAVA_HOME/bin:$PATH
# export PATH

To set the JAVA_HOME permanently, however, we need to add below to the ~/.bash_profile of the user (in this case, root).  We can also add it /etc/profile and then source it to give to all users.

In case, Using JDK6 instead of JDK7 -
If you decided to use JDK 6 rather than JDK 7 as we did above, simply save the JDK 6 bin file to /opt (or another location), then navigate to /usr/java and issue: 'sh /opt/jdk-6u33-linux-x64.bin'. This will create a JAVA Home of /usr/java/jdk1.6.0.33. JDK6 bin file can be downloaded from http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-419409.html#jdk-6u33-oth-JPR

Now check the Java version, as follows
# java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

If your Java version is at least 1.5.0, you can install Tomcat 5.5 or Tomcat 6.0. If you do not have a Java runtime of at least version 1.5.0, you cannot install Tomcat 6.0 or higher without first updating Java.


Download and Unpack Tomcat Binary release

We will now install Tomcat 7 under /opt

The latest Tomcat is Tomcat 7.x. Download the latest tomcat package from http://tomcat.apache.org/download-70.cgi

The latest binary distribution is Tomcat 7.0.32
# cd /opt
# wget http://apache.techartifact.com/mirror/tomcat/tomcat-7/v7.0.32/bin/apache-tomcat-7.0.32.tar.gz

This will create a directory /opt/apache-tomcat-7.0.32

The root of apache tomcat installation is referred as $CATALINA_HOME
So thus, CATALINA_HOME is /opt/apache-tomcat-7.0.32

Optionally, Tomcat may be configured for multiple instances by defining $CATALINA_BASE for each instance. If multiple instances are not configured, $CATALINA_BASE is the same as $CATALINA_HOME

Under /opt, create a symbolic link named "tomcat" to directory "/opt/apache-tomcat-7.0.32"
# ln -s apache-tomcat-7.0.32 tomcat

For security purpose, it is always better to run tomcat as non-root user

Now let us create a user and group named "tomcat"
# groupadd tomcat

Create user "tomcat" with home directory as /opt/tomcat/temp, and set user tomcat’s login shell to something that doesn’t actually work, such as /sbin/nologin

# useradd -s /sbin/nologin -d /opt/tomcat/temp -g tomcat -c 'Tomcat User'  tomcat

# id tomcat
uid=502(tomcat) gid=502(tomcat) groups=502(tomcat)

Change ownership of the tomcat files to the user tomcat we created above:
# chown -Rf tomcat.tomcat /opt/apache-tomcat-7.0.32


Configure Tomcat to Run as a Service

We will now see how to run Tomcat as a service and create a simple Start/Stop/Restart script, as well as to start Tomcat at boot.

Change to the /etc/init.d directory and create a script called 'tomcat' as shown below.

cd /etc/init.d
vi tomcat

#!/bin/bash  
# description: Tomcat Start Stop Restart  
# processname: tomcat  

# Set JAVA home directory   
JAVA_HOME=/usr/java/jdk1.7.0_09
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH

# Set Tomcat home directory
CATALINA_HOME=/opt/apache-tomcat-7.0.32
  
case $1 in  
start)  
/bin/su -s /bin/sh tomcat $CATALINA_HOME/bin/startup.sh  
;;   
stop)     
/bin/su -s /bin/sh tomcat $CATALINA_HOME/bin/shutdown.sh  
;;   
restart)  
/bin/su -s /bin/sh tomcat $CATALINA_HOME/bin/shutdown.sh  
/bin/su -s /bin/sh tomcat $CATALINA_HOME/bin/startup.sh  
;;   
esac      
exit 0  

In the above script, we are simply calling the startup.sh and shutdown.sh scripts located in the Tomcat bin directory (/opt/apache-tomcat-7.0.32/bin)

Since the user "tomcat" was created with no login option(/sbin/nologin), we specify the shell "/bin/sh" to start and stop the script as user "tomcat"

# chmod +x tomcat

# service tomcat start
Using CATALINA_BASE:   /opt/apache-tomcat-7.0.32
Using CATALINA_HOME:   /opt/apache-tomcat-7.0.32
Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.32/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-7.0.32/bin/bootstrap.jar:/opt/apache-tomcat-7.0.32/bin/tomcat-juli.jar

We should review the catalina.out log located at /opt/apache-tomcat-7.0.32/logs/catalina.out and check for any errors
# less /opt/apache-tomcat-7.0.32/logs/catalina.out

Check if tomcat is running as follows. Check for running java processes and  tomcat as follows
# pgrep java
12513

# netstat -nlp | grep :8080
tcp        0      0 :::8080                     :::*                        LISTEN      12513/java

#stop tomcat service

# service tomcat stop
Using CATALINA_BASE:   /opt/apache-tomcat-7.0.32
Using CATALINA_HOME:   /opt/apache-tomcat-7.0.32
Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.32/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-7.0.32/bin/bootstrap.jar:/opt/apache-tomcat-7.0.32/bin/tomcat-juli.jar

# pgrep java
#


Configure tomcat service to start automatically on boot

To make /etc/init.d/tomcat script to run on boot

# chkconfig --add tomcat
service tomcat does not support chkconfig

To overcome this error "service tomcat does not support chkconfig", add the line "# chkconfig: 235 20 80" in beginning of file /etc/init.d/tomcat.
 * 235 stand for runlevels and
 * 20 & 80 stand for stop and start priorities

Now /etc/init.d/tomcat will look as follows

#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 235 20 80

# Set JAVA home directory
JAVA_HOME=/usr/java/jdk1.7.0_09
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH

# Set Tomcat home directory
CATALINA_HOME=/opt/apache-tomcat-7.0.32

case $1 in
start)
/bin/su -s /bin/sh tomcat $CATALINA_HOME/bin/startup.sh
;;
stop)
/bin/su -s /bin/sh tomcat $CATALINA_HOME/bin/shutdown.sh
;;
restart)
/bin/su -s /bin/sh tomcat $CATALINA_HOME/bin/shutdown.sh
/bin/su -s /bin/sh tomcat $CATALINA_HOME/bin/startup.sh
;;
esac
exit 0

Now run
# chkconfig --add tomcat

No error shall be reported now

# chkconfig --level 235 tomcat on
# chkconfig --list tomcat
tomcat          0:off   1:off   2:on    3:on    4:off   5:on    6:off

Thus tomcat installation is completed.


More about Starting Up and Stopping Tomcat

We used the following scripts to start and stop tomcat

  • $CATALINA_HOME/bin/startup.sh
  • $CATALINA_HOME/bin/shutdown.sh

where, CATALINA_HOME = /opt/apache-tomcat-7.0.32.

Both startup.sh and shutdown.sh actually use catalina.sh

Tomcat invocation scripts are available under the path /opt/apache-tomcat-7.0.32/bin/
* catalina.sh - The main Tomcat script. This runs the java command to invoke the Tomcat startup and shutdown classes.
* cpappend.sh - This is used internally, and then only on Windows systems, to append items to Tomcat classpath environment variables.
* digest.sh - This makes a "crypto" digest of Tomcat passwords. Use it to generate encrypted passwords.
* setclasspath.sh - This is also only used internally and sets the Tomcat classpath and several other environment variables.
* shutdown.sh - This runs "catalina stop" and shuts down Tomcat.
* startup.sh - This runs "catalina start" and starts up Tomcat.
* tool-wrapper.sh - This is a generic Tomcat command-line tool wrapper script that can be used to set environment variables and then call the main method of any fully qualified class that is in the classpath that is set. This is used internally by the digest script.
* version.sh - This runs the catalina version, which outputs Tomcat’s version information.

catalina.sh is the main Tomcat script which is called by both startup.sh as well as shutdown.sh scripts.

The main script, catalina, is invoked with one of several arguments. The most common arguments are
 * start,
 * run, or
 * stop.

When invoked with "start" (as it is when called from startup), it starts up Tomcat with the standard output and standard error streams directed into the file CATALINA_HOME/logs/catalina.out.

The "run" argument causes Tomcat to leave the standard output and error streams where they currently are (such as to the console window) useful for running from a terminal when you want to see the startup output.

Let us see their illustration

# ./catalina.sh start
Using CATALINA_BASE:   /opt/apache-tomcat-7.0.32
Using CATALINA_HOME:   /opt/apache-tomcat-7.0.32
Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.32/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_09
Using CLASSPATH:       /opt/apache-tomcat-7.0.32/bin/bootstrap.jar:/opt/apache-tomcat-7.0.32/bin/tomcat-juli.jar

# pgrep java
13616

# ./catalina.sh stop
Using CATALINA_BASE:   /opt/apache-tomcat-7.0.32
Using CATALINA_HOME:   /opt/apache-tomcat-7.0.32
Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.32/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_09
Using CLASSPATH:       /opt/apache-tomcat-7.0.32/bin/bootstrap.jar:/opt/apache-tomcat-7.0.32/bin/tomcat-juli.jar

# pgrep java
#

The config file read during start is /opt/apache-tomcat-7.0.32/conf/server.xml.

Thus the config file, server.xml, is available under the path $CATALINA_BASE/conf or $CATALINA_HOME/conf


Tomcat Environment Variables

CATALINA_BASE This sets the base directory for writable or customized portions of a Tomcat installation tree, such as logging files, work directories, Tomcat’s conf directory, and the webapps directory. It is an alias for CATALINA_HOME.

CATALINA_HOME This sets the base directory for static (read-only) portions of Tomcat, such as Tomcat’s lib directories and command-line scripts.

CATALINA_OPTS This passes through Tomcat-specific command-line options to the java command.

CATALINA_TMPDIR This sets the directory for Tomcat temporary files. CATALINA_HOME/temp

JAVA_HOME This sets the location of the Java runtime or JDK that Tomcat will use.

JRE_HOME This is an alias to JAVA_HOME.

JAVA_OPTS This is where you may set any Java command-line options.

JPDA_TRANSPORT This variable may set the transport protocol used for JPDA debugging.

JPDA_ADDRESS This sets the address for the JPDA used with the catalina jpda start command. For example, the port number and JPDA transport implementation can be set with JPDA_ADDRESS=8000 and JPDA_TRANSPORT=dt_socket.

JSSE_HOME This sets the location of the Java Secure Sockets Extension used with HTTPS.

CATALINA_PID This variable may optionally hold the path to the process ID file that Tomcat should use when starting up and shutting down.




23 comments:

  1. hello..
    i had download and configured java7 and tomcat7 in cent os 6.3..

    [root@www ~]#tar xvfz /opt/jdk/jdk-7u11-linux-x64.tar
    [root@www ~]#tar xvfz /opt/tomcat/apache-tomcat-7.0.34.tar.gz
    [root@www ~]#chmod +x /opt/tomcat/apache-tomcat-7.0.39-src/bin/*.sh
    [root@www ~]#groupadd tomcat
    [root@www ~]#useradd -g tomcat -d /opt/tomcat/apache-tomcat-7.0.39-src tomcat
    [root@www ~]#chown tomcat:tomcat /opt/tomcat/apache-tomcat-7.0.39-src
    [root@www ~]#vi /etc/init.d/tomcat
    #!/bin/bash
    # description: Tomcat Start Stop Restart
    # processname: tomcat
    # chkconfig: 234 20 80
    JAVA_HOME=/opt/jdk/jdk1.7.0_11
    export JAVA_HOME
    PATH=$JAVA_HOME/bin:$PATH
    export PATH
    CATALINA_HOME=/opt/tomcat/apache-tomcat-7.0.39-src

    case $1 in
    start)
    sh $CATALINA_HOME/bin/startup.sh
    ;;
    stop)
    sh $CATALINA_HOME/bin/shutdown.sh
    ;;
    restart)
    sh $CATALINA_HOME/bin/shutdown.sh
    sh $CATALINA_HOME/bin/startup.sh
    ;;
    esac
    exit 0
    "tomcat" 23L, 508C

    [root@www ~]# JAVA_HOME=/opt/jdk/jdk1.7.0_11
    [root@www ~]# export JAVA_HOME
    [root@www ~]# PATH=$JAVA_HOME/bin:$PATH
    [root@www ~]# export PATH
    [root@www ~]# vim /root/.bash_profile
    # .bash_profile

    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
    . ~/.bashrc
    fi

    # User specific environment and startup programs
    JAVA_HOME=/opt/jdk/jdk1.7.0_11/
    export JAVA_HOME
    PATH=$JAVA_HOME/bin:$PATH
    export PATH
    #PATH=$PATH:HOME/bin
    #export PATH
    #export JAVA_HOME=/opt/jdk/jdk1.7.0_11/bin/java_files
    #export PATH=$PATH:/opt/jdk/jdk1.7.0_11/bin
    [root@www ~]#init 6
    [root@www ~]#chmod 777 /etc/init.d/tomcat
    [root@www ~]#chkconfig --add tomcat
    [root@www ~]#chkconfig --level 234 tomcat on
    [root@www ~]#chkconfig --list tomcat
    tomcat 0:off 1:off 2:on 3:on 4:on 5:off 6:off
    [root@www ~]#[root@www ~]# service tomcat stop
    Using CATALINA_BASE: /opt/tomcat/apache-tomcat-7.0.39-src
    Using CATALINA_HOME: /opt/tomcat/apache-tomcat-7.0.39-src
    Using CATALINA_TMPDIR: /opt/tomcat/apache-tomcat-7.0.39-src/temp
    Using JRE_HOME: /opt/jdk/jdk1.7.0_11
    Using CLASSPATH: /opt/tomcat/apache-tomcat-7.0.39-src/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-7.0.39-src/bin/tomcat-juli.jar
    Error: Could not find or load main class org.apache.catalina.startup.Bootstrap

    i dont know what is this error

    ReplyDelete
  2. I believe that you are using source code distribution /opt/tomcat/apache-tomcat-7.0.39-src, instead of binary distribution /opt/tomcat/apache-tomcat-7.0.34.tar.gz. So that may be the cause

    [root@www ~]#tar xvfz /opt/tomcat/apache-tomcat-7.0.34.tar.gz
    [root@www ~]#chmod +x /opt/tomcat/apache-tomcat-7.0.39-src/bin/*.sh

    ReplyDelete
  3. Hi,

    Just wanted to say thank for this installation tutorial, It was very helpful.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete


  6. I found a lot of information here to create this actually best for all newbie here. Thank you for this information.

    Artificial Intelligence Training In Hyderabad

    ReplyDelete
  7. if you want to learn digital marketing in mumbai. excelr solutions providing best AI course in mumbai.for more details click here

    digital marketing courses in mumbai

    ReplyDelete
  8. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    ExcelR digital marketing courses in mumbai

    ReplyDelete
  9. It's a very attractive and useful blog .I has found the solution, Thanks for the share.
    Data Science Training in Hyderabad

    ReplyDelete
  10. Thank you for the time to publish this information very useful! I've been looking for books of this nature for a way too long. I'm just glad that I found yours. Looking forward for your next post. ThanksData Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  11. I really happy found this website eventually. Really informative and inoperative! Thanks for the post and effort! Please keep sharing more such article.

    AWS Training in Hyderabad

    ReplyDelete
  12. I am looking for and I love to post a comment that "The content of your post is awesome" Great work! data science courses

    ReplyDelete
  13. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data science online course

    ReplyDelete
  14. Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging. After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    AWS Online Training
    Online AWS Certification Training

    ReplyDelete


  15. I was just examining through the web looking for certain information and ran over your blog.It shows how well you understand this subject. Bookmarked this page, will return for extra. data science course in vadodara

    ReplyDelete
  16. Excellent blog post. I certainly appreciate this website. Stick with it!
    online training in java
    online training on java

    ReplyDelete
  17. Amazingly by and large very interesting post. I was looking for such an information and thoroughly enjoyed examining this one. Keep posting. An obligation of appreciation is all together for sharing.data analytics course delhi

    ReplyDelete
  18. Hey, thanks for the blog article.Really looking forward to read more. Cool.
    best online java corse
    java online classes

    ReplyDelete
  19. It is the perfect time to make some plans for the future and it is the time to be happy. I've read this post and if I could I would like to suggest some interesting things or suggestions. Perhaps you could write the next articles referring to this article. I want to read more things about it!
    business analytics course in hyderabad

    ReplyDelete