美美观观广场舞:Quick Start Guide to Setting up Apache Tomcat...
I recently setup Apache Tomcat 6.0.10 and in this post I will share the steps that I went through to install it on my RedHat Linux AS 4 server.
The Basics – Download and Install The Software
- First make sure you have a Java Development Kit installed on your server. I have a write up on how to do this at http://timarcher.com/?q=node/59.
- Download the Tomcat binary distribution from http://tomcat.apache.org/download-60.cgi. I selected the tar.gz option under the Core section.
- Once you have downloaded your file, place it somewhere on your Linux box (I put mine in /root/tomcat). The name of the file I downloaded was apache-tomcat-6.0.10.tar.gz.
- Login to your Linux box as the root user, and change directory to where you placed the Tomcat file that you downloaded.
- Make a directory to be the root of our Tomcat Installation.
mkdir /usr/local/tomcat/
- Move the file we downloaded to the directory we created in the previous step.
mv apache-tomcat-6.0.10.tar.gz /usr/local/tomcat/
- Change to the /usr/local/tomcat directory.
cd /usr/local/tomcat
- Unzip and untar the tomcat file.
tar -zxvf apache-tomcat-6.0.10.tar.gz
- You should now have a directory named /usr/local/tomcat/apache-tomcat-6.0.10. If you wish you can delete the install .tar.gz file named apache-tomcat-6.0.10.tar.gz.
rm apache-tomcat-6.0.10.tar.gz
- Ensure you have a JAVA_HOME environment variable setup. If you don’t, when you go to startup Tomcat, you will get a error that says:
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this program
Since my JDK is installed in the directory /usr/java/default, I have the following set in my /etc/profile file to always set the JAVA_HOME variable and to put the Java binaries into my PATH:
JAVA_HOME=/usr/java/default; export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH; export PATH
- Change to your new Tomcat bin directory and start it up:
cd /usr/local/tomcat/apache-tomcat-6.0.10/bin ./startup.sh
Your output should look like:
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-6.0.10 Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-6.0.10 Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-6.0.10/temp Using JRE_HOME: /usr/java/default
And doing a ps -aef | grep tomcat should show a Tomcat process running.
- Lastly, go to your client PC and open up a web browser. You should now be able to load up a page on port 8080 from the server you just installed Tomcat on. I loaded this URL in my web browser:
http://192.168.1.50:8080
And like magic, the default Tomcat page showed up in my web browser. The IP address or hostname will most likely be different than the one I specified above.
- If you want to shutdown your Tomcat service, the command to run is simply:
./shutdown.sh
- The last step I perform is to create a link named /usr/local/tomcat/current that links to my Tomcat install directory. This is so that I don’t have to hard code the specific installation directory in my scripts. When I upgrade to a new version of Tomcat, I don’t have to modify any of my scripts that reference the Tomcat installation directory. Instead I simply update the symbolic link. To do this run the following commands:
cd /usr/local/tomcat ln -sf apache-tomcat-6.0.10 current
I use these symbolic links in the startup and shutdown scripts that I reference below.
- Congratulations, the basics are now done, and we have a working Tomcat installation to begin web development with! In later posts we will use this Tomcat installation to demonstrate some various web development techniques, retrieve and display data from your databases, and more. I also plan to show how to setup the Apache HTTP server to serve up your static content and forward requests for your dynamic content to the Tomcat installation. As a bonus, continue to read on for a system init script that will start Tomcat automcatically when your system boots!
Setup Tomcat to Start Automatically When the System Boots
In order to start Tomcat automatically when my server boots up, I setup a script in /etc/init.d. Follow the instructions below to do the same.
- First ensure you have the symbolic link named /usr/local/tomcat/current setup as a described above. If you don’t, then you’ll have to replace all occurrences of /usr/local/tomcat/current in the script below with your own TOMCAT_HOME location.
- Create the file /etc/init.d/tomcat…
vi /etc/init.d/tomcat
…and place the following script in it:
#!/bin/bash # # Startup script for Tomcat # # chkconfig: - 86 15 # description: Tomcat is a JSP server. # processname: tomcat JAVA_HOME=/usr/java/default export JAVA_HOME tomcat_home=/usr/local/tomcat/current/bin start_tomcat=/usr/local/tomcat/current/bin/startup.sh stop_tomcat=/usr/local/tomcat/current/bin/shutdown.sh start() { echo -n "Starting tomcat: " cd $tomcat_home ${start_tomcat} echo "done." } stop() { echo -n "Shutting down tomcat: " cd $tomcat_home ${stop_tomcat} echo "done." } # See how we were called case "$1" in start) start ;; stop) stop ;; restart) stop sleep 10 start ;; *) echo "Usage: $0 {start|stop|restart}" esac exit 0
- Make sure we set appropriate permissions on the new file:
chmod 755 /etc/init.d/tomcat
- Now we use the chkconfig command to add the script to the list of system services.
chkconfig --add tomcat
- Now we tell the system to start the tomcat service for run levels 3 and 5.
chkconfig --level 35 tomcat on
- And lastly, we verify that this has indeed occurred. Run the following command:
chkconfig --list tomcat
And your output should look like the following:
tomcat 0:off 1:off 2:off 3:on 4:off 5:on 6:off
- You should now be able to test your scripts. Running the commands below should start and stop your Tomcat service.
/etc/init.d/tomcat start /etc/init.d/tomcat stop
Furthermore, you should be able to reboot your system and ensure Tomcat starts automatically.