Java Tutorial - Java Script : Installing Tomcat under Linux

Java Tutorial - Java Script :

Installing Tomcat under Linux


The steps for installing Tomcat as a server under Linux start off being similar to those we performed under Linux previously; however, we need to add some significant steps to this process. We will skip the first couple of steps from the previous example, assuming that you have already installed the JDK and downloaded the Tomcat binary tar.gz file. Instead, you will start by creating the new deployment directory. You need to be logged in as root for this
installation. The new steps are:
1. Create a deployment directory.
2. Extract the files from the archive.
3. Add an administrator.
4. Add a Tomcat user and group.
5. Change the owner and group on all files and directories.
6. Create and install an initialization script.
7. Set the user environment.



Step 1: Create the Deployment Directory
We normally install Tomcat into the /usr/jakarta/tomcat directory. Your organization may have different rules for the installation location. Other common locations that we have seen are:
·         /usr/tomcat
·         /usr/local/tomcat
·         /usr/local/jakarta/tomcat
·         /var/tomcat
·         /var/jakarta/tomcat
You need to pick a location suitable for your organization’s deployment rules. Create the directories using the Linux md command as follows: md /usr/Jakarta

Step 2: Extract the Files
First copy the distribution (tar.gz) file into the /usr/jakarta directory created in the previous step. Now, the following command can be used to extract the files: tar -zxvf jakarta-tomcat.tar.gz. This also creates a directory with the name of the .tar file. We want to rename this directory with the following command: mv jakarta-tomcat tomcat We should now have all of the distribution files extracted into the directory /usr/jakarta/tomcat.

Step 3: Add a Tomcat Administrator
This is identical to the step shown earlier. You need to add an administrator password so the Tomcat Manager and Administration tools can be used. To do this, edit the file tomcat-users.xml. This file is found in the /usr/ jakarta/tomcat/conf folder. Add the lines shown in bold in the following code.
<?xml version=’1.0’ encoding=’utf-8’?>
<tomcat-users>
<role rolename=”admin”/>
<role rolename=”manager”/>
<role rolename=”role1”/>
<role rolename=”tomcat”/>
<user username=”admin” password=”adminpasswd” roles=”admin,manager”/>
<user username=”both” password=”tomcat” roles=”tomcat,role1”/>
<user username=”role1” password=”tomcat” roles=”role1”/>
<user username=”tomcat” password=”tomcat” roles=”tomcat”/>
</tomcat-users>
Be certain to select a password that is appropriate for your site. Please do not use the same password shown here.

Step 4: Add a User and Group for Tomcat
If you already have Apache installed, you should use the apache user and apache group that should already exist on the system. Otherwise, you should add a user and a group to your system specifically for Tomcat. This is a security precaution. It effectively limits the ability of a hijacker to access other parts of the system. Tomcat should always and only be run by this special user. The special user and group should be created with numbers that identifies them as a system accounts. The commands to do this will vary from system to system. On Mandrake 9.0 the following command creates both the tomcat user and tomcat group. useradd -c “Special Tomcat user” -r tomcat You may have slightly different commands on your system (or you may have a graphical interface for adding new users to your system). If you are unfamiliar with these commands try searching the manual pages for adduser and addgroup or useradd and groupadd commands.

Step 5: Change File and Directory Owner and Group
Now that you have a user and group for Tomcat you want to set all of the files and directories so they are owned by the tomcat user and group. The following command does this. chown -R tomcat:tomcat /usr/jakarta/tomcat While you are at it, you should examine the file permissions. The directories should have the attributes set as shown here:
drwxr-xr-x 2 tomcat tomcat 4096 Jan 4 23:52 bin/
drwxr-xr-x 5 tomcat tomcat 4096 Dec 19 09:08 common/
drwxrw-r-x 4 tomcat tomcat 4096 Jan 6 23:03 conf/
drwxrwxr-x 2 tomcat tomcat 4096 Jan 7 22:19 logs/
drwxr-xr-x 5 tomcat tomcat 4096 Dec 19 09:08 server/
drwxr-xr-x 4 tomcat tomcat 4096 Dec 19 09:08 shared/
drwxr-xr-x 2 tomcat tomcat 4096 Jan 6 23:03 temp/
drwxrwxr-x 6 tomcat tomcat 4096 Jan 2 11:56 webapps/
drwxrwxr-x 3 tomcat tomcat 4096 Jan 6 01:31 work/
Use the chmod command to set permissions as needed.

Step 6: Initialize Scripts
You want Tomcat to start when the system is started and shut down when the system is shut down. To do this under Linux, add a script to the /etc/init.d directory and run the chkconfig command to install the scripts into the rc.d directories. The following script (named tomcatd) was created for this purpose.
#!/bin/sh
#
# Startup script for the Tomcat servlet container
#
# chkconfig: 345 80 20
# description: Tomcat is a Java Application Server and servlet container
# HTML files and CGI.
# processname: tomcat
# pidfile: /var/run/tomcat.pid
# config: /usr/jakarta/tomcat/conf/server.xml
#=====================================================================
#=== IMPORTANT NOTE:
#=== The comments above are required for the chkconfig script to work
#=== correctly. Please do not remove them. Read the man pages for
#=== chkconfig if you need to know more.
#=====================================================================
# Source function library.
# Set environment variables needed by JVM and Tomcat
JAVA_HOME=/usr/java/j2sdk1.4.1_01
CATALINA_HOME=/usr/jakarta/tomcat
#=====================================================================
# This function will be executed when the system is starting up.
#=====================================================================
start() {
# Start daemon
echo “Starting Tomcat: “
su -l tomcat -s /bin/bash -c “/usr/jakarta/tomcat/bin/startup.sh”
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo “Tomcat started!”
else
echo “Tomcat failed to start!”
fi
echo
touch /var/lock/subsys/tomcat
}
#=====================================================================
# This function will be executed when the system is stopping
#=====================================================================
stop() {
# Stop daemon.
echo “Stopping Tomcat: “