Java Tutorial - Java Script :
Installing Tomcat under Linux
su -l tomcat -s /bin/bash -c “/usr/jakarta/tomcat/bin/shutdown.sh”
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo “Tomcat stopped!”
else
echo “Problem stopping Tomcat!”
fi
echo
rm -f /var/lock/subsys/tomcat
}
restart() {
echo “Stopping Tomcat for restart...”
stop
Powering the Web Tier with a Server Container 151
sleep 15
echo “Restarting Tomcat:”
start
}
# See how we were called.
case “$1” in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo “Usage:” “$0” “{start|stop|restart}\n”
exit 1
esac
exit 0
Please note the warning in the comments. The comments at the top of the file are required for the chkconfig program to work properly. The line that reads # chkconfig: 345 80 20 has special meaning. The 345 tells chkconfig that the service should be started at runlevels 3, 4 and/or 5. The 80 and 20 provides a means to control the order that the script should be executed in relation to the other startup scripts. These numbers were picked for our system because Tomcat should start before Apache is started and be shut down after Apache has been shut down. There are a couple of other items to note. You must set the JAVA_HOME and CATALINA_HOME environment variables here since the script is running before any shell variables have been set. Also, the script starts Tomcat with the command shown below: su -l tomcat -s /bin/bash -c “/usr/jakarta/tomcat/bin/startup.sh” By using su to start Tomcat, you can start Tomcat as the tomcat user that you created earlier. This allows Tomcat to be executed safely as the tomcat user even though the script is executed by root during system initialization. This is also a handy command to use instead of the usual Tomcat startup.sh and shutdown.sh. I normally create two scripts, tomstart and tomstop to ease the starting and stopping the Tomcat server using the correct user. Once the initialization script has been created and tested, it should be copied to the /etc/init.d directory and named tomcatd. The script then needs to be installed using the following command: chkconfig -add tomcatd After this is done, a reboot will automatically start Tomcat. Before you start Tomcat by rebooting however you should start Tomcat manually to make sure that there are no other problems. To do this, enter the following command: su -l tomcat -s /bin/bash -c “/usr/jakarta/tomcat/bin/catalina.sh run” This starts Tomcat running as the tomcat user in the current shell. Its startup messages will be sent the terminal on stderr instead of the file $CATALINA_HOME/logs/catalina.out. This can help you find any problems that Tomcat might have reading or writing to the directories it needs.
Step 7: Set up the User Environment
If you want a user to be able to run the Tomcat startup and shutdown scripts, then you should set the CATALINA_HOME environment variable to reflect the new installation. The user also needs to have the JAVA_HOME environment variable set. Adding the following commands to a users profile script does this: export JAVA_HOME=/usr/java/j2sdk1.4.1_01 export CATALINA_HOME=/usr/jakarta/tomcat Remember to adjust the values to reflect the appropriate ones for your system. A user that needs to add applications to the Tomcat webapps directory can be added to the tomcat group. If the permissions are set correctly this, will give write access to webapps to the user because webapps is configured for group write. The command to do this under Linux is: usermod -G tomcat username
Configuring Multiple Instances of Tomcat
On Linux, Tomcat provides a means to run a user-customized instance of Tomcat from a single Tomcat installation. This is useful if there are many developers who need to share a single installation, but need their own unique instances. For this to work, a new environment variable, CATALINA_BASE, is created. Export CATALINA_BASE=$HOME/tomcat CATALINA_BASE is automatically set to CATALINA_HOME if it does not exist in the environment. Next, you will need to create a Tomcat directory below your home directory and create or copy certain directories, as follows:
cd $HOME
md tomcat
cd tomcat
cp $CATALINA_HOME/conf .
md logs
md webapps
md work
md temp
If you want a copy of the contents of the Tomcat webapps directory, then you should copy them into your new webapps directory. You may also need to change the ports that Tomcat uses so that your instance does not conflict with the core Tomcat installation. You can do this by editing the file conf/server.xml. Search for strings like port= and replace them with unique ports. If Tomcat is configured to listen on port 8009 for Apache, you can disable the connector by commenting out the element in server.xml. The other ports that you are likely to have to change are:
8080: The default connection port
8443: The port for SSL connections
8009: The port for connections to Apache
8005:The port used for stopping Tomcat
After these changes have been completed, you can start and stop Tomcat by using the normal startup.sh and shutdown.sh scripts found in the CATALINA_HOME/bin directory. You do not want to use the tomcat user in this case because the file permissions should be set for your use.
