Apache Tomcat is a Java Servlet container which can be run as a standalone HTTP server. The instructions below will walk you through installing and getting Tomcat working on your Linux box. I’m doing this on a SuSE Linux server but it should be applicable to other Linux flavors like Red Hat.
Note: All the instructions below are executed using the root user account.
First, your Linux server must have Java installed because Tomcat requires it. If you don’t have Java installed already or want the latest, do the following:
- Get the latest JDK (currently JDK 6 update 12) from the Sun Java Website.
- Download the jdk bin file, give it execute permission, and run it. It will extract an RPM file.
- Install the JDK RPM by running this command:
rpm -ivh jdk-6u10-linux-i586.rpm
- The JDK will be installed under the “/usr/java” directory (Ex: “/usr/java/jdk1.6.0_12”).
Now, you can install the latest Tomcat by doing the following:
- Get the latest Tomcat (currently 6.0.18) from the Apache Tomcat Website.
- Install Tomcat by running the following:
cd /opt
tar -xzf apache-tomcat-6.0.18.tar.gz - The above will extract Tomcat under the “/opt” directory (Ex: “/opt/apache-tomcat-6.0.18”)
- Tomcat needs these environment variables to be set:
JAVA_HOME=/usr/java/jdk1.6.0_12
CATALINA_HOME=/opt/apache-tomcat-6.0.18
You can set the variables above in the root user environment by default or you can set these in the Tomcat startup script. You will want to do the latter if you have several versions of Java installed and wish to use Tomcat with a specific Java version.
To adjust the root user environment, do the following:
- Under SUSE Linux 10 SP1, to set the default environment variables for root (and Tomcat), edit the “/etc/profile” file, and add the following lines to the end of the file:
JAVA_HOME=/usr/java/jdk1.6.0_12
JRE_HOME=/usr/java/jdk1.6.0_12
CATALINA_HOME=/opt/apache-tomcat-6.0.18
export JAVA_HOME JRE_HOME CATALINA_HOME - Optionally, if you don’t want to touch “/etc/profile”, you can create an “/etc/profile.local” which should also be loaded for the root environment; if you do this, make sure to add a second “source /etc/profile.local” to your script.
- You only have to set JRE_HOME above if you need to override it because it was explicitly defined elsewhere. Otherwise, if JRE_HOME is not defined, Tomcat will use JAVA_HOME instead.
To adjust the Tomcat startup script to use a specific Java version and/or use a different port than the default 8080, do the following:
- Adjust Tomcat to use the latest JDK without touching the existing environment variables.
- Edit the “/opt/apache-tomcat-6.0.18/bin/catalina.sh” file.
- At the top of the file, add these two lines:
JAVA_HOME=/usr/java/jdk1.6.0_12
CATALINA_HOME=/opt/apache-tomcat-6.0.18 - There is another environment variable JRE_HOME which will default to JAVA_HOME if it is blank. You will want to check that your default environment doesn’t set it explicitly. If it is set explicitly, you can override by adding this line to the top of the “catalina.sh” file:
JRE_HOME=/usr/java/jdk1.6.0_12
- If you wish to (maybe you want to run two versions of Tomcat), you can adjust Tomcat to use another port (like 7080) instead of the default 8080 by following these steps:
- Edit the “/opt/apache-tomcat-6.0.18/conf/server.xml” file.
- Replace all instances of the default port numbers with these new port numbers:
Old Port New Port 8005 7005 8009 7009 8080 7080 8443 7443 - While you are in this file, you might want to enable Tomcat access logging by uncommenting the “AccessLog” value in server.xml to enable access logging in Tomcat. The access logs will be located in the “/opt/apache-tomcat-6.0.18/logs” directory (Ex: “localhost_access_log.2009-03-06.txt”).
Once you have installed Tomcat, you can start and stop it using these commands:
sh startup.sh &
sh shutdown.sh &
Once Tomcat is started, test it out by launching your browser and doing the following:
- Browse to http://localhost:8080/ or http://localhost:7080/ if you have changed the default port to 7080.
- If you attempt to browse to the server from a remote machine and the server doesn’t respond, then you may need to disable the firewall.
- Under SUSE Linux, disable the firewall by logging in as root and running “rcSuSEfirewall2 stop”. Further, to prevent the firewall from running on boot, run Yast->Firewall and change “Service Start” to Manually. If you run “chkconfig | grep firewall” afterwards, you should see that SuSEfirewall2_init and SuSEfirewall2_setup services are set to off.
Now that Tomcat is running successfully, you may wish to configure Tomcat to run on boot. One way of doing so is to configure Tomcat to be a service and registered it to be launched as part of the boot.
- First, create the shell script file below and name it “tomcat” (or whatever service name you want to use).
#!/bin/sh
#
# /etc/init.d/tomcat
#
# This is the init script for starting up the
# Jakarta Tomcat server
#
# description: Starts and stops the Tomcat daemon.
#
tomcat=/opt/apache-tomcat-6.0.18
startup=$tomcat/bin/startup.sh
shutdown=$tomcat/bin/shutdown.sh
start() {
echo -n $"Starting Tomcat service: "
sh $startup
echo $?
}
stop() {
echo -n $"Stopping Tomcat service: "
sh $shutdown
echo $?
}
restart() {
stop
start
}
status() {
ps -aef | grep apache-tomcat | grep -v tomcat6 | grep -v grep
}
# Handle the different input options
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0 - Note that when you run a process as a service, its environment may be different from the environment you see when you log in.
- Under SUSE Linux, you may wish to add the line below to the top of the Tomcat script to ensure that the Tomcat process will run with the environmental variables that you have set in “/etc/profile”.
source /etc/profile
- To see what environment a process is running under, you can use the following commands:
ps -aef | grep tomcat
... you will get a process id number like 5054 ...
cat /proc/5054/environ
- Under SUSE Linux, you may wish to add the line below to the top of the Tomcat script to ensure that the Tomcat process will run with the environmental variables that you have set in “/etc/profile”.
- Copy the file to the “/etc/init.d” directory (example: “/etc/init.d/tomcat”).
- Give it execute permission:
cd /etc/init.d
chmod u+x tomcat - You can test the script by running this command (which will return the Tomcat process info if it is running; otherwise, it will return blank):
cd /etc/init.d
./tomcat status - Add tomcat as a service by running the following:
chkconfig --add tomcat
- Now that tomcat is a service, you can issue these commands from anywhere:
service tomcat start
service tomcat stop
service tomcat restart
service tomcat status - Set the tomcat service to start at boot:
chkconfig tomcat on
That is pretty much it. Your Linux server is set to automatically start Tomcat.
Note: I updated the Tomcat service script to be more bulletproof; specifically, the script will kill Tomcat if it hangs on exit (which sometimes may occur).
great article. this helped me saves time since i was a newbe. thank’s a lot.
thanks. nice document
I used step 6 forward I am using an instace I created on SUSEstudio and the tomcat6 script already exist but I could not get it work by adding S04 and K01 link using ln command. thanks for the info. now it startup automatically A+++++++++
I like the article very much, specially how to add the tomcat scripts to OS service. But this is the work around only, so can anybody tell me that how to install Tomcat as service without workaround, means i don’t want to add or edit any script. Just runn the installer and service should be setup.
Anyway article is nice so thanks again. 🙂
Really Gr8 ! Thanks For sharing..
Excelente tutorial, muito bom mesmo.
Apenas uma dúvida.
Como iniciar o tomcat como serviço, não sendo usuário “root”?
Obrigado.
Robson Vaz
Obrigado pelo elogio. Para executar o script como um usuário não-root, na parte superior do script, coloque esta linha “su -l username”. Isso vai mudar o contexto da raiz para esse usuário.
Perfect! Thank you from Barcelona!
Excellent tutorial ! Thank you from Pune
Nice tutorial!! Helped me to add tomcat as service in redhat env. Thanks for the article
Nice..
Thank you very much, script is working good.