梦之祭天宫奏腐图:Quick Start Guide to Setting up Apache Tomcat...

来源:百度文库 编辑:偶看新闻 时间:2024/04/27 23:46:07

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

  1. 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.
  2. Download the Tomcat binary distribution from http://tomcat.apache.org/download-60.cgi. I selected the tar.gz option under the Core section.
  3. 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.
  4. Login to your Linux box as the root user, and change directory to where you placed the Tomcat file that you downloaded.
  5. Make a directory to be the root of our Tomcat Installation.
    mkdir /usr/local/tomcat/    
  6. 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/    
  7. Change to the /usr/local/tomcat directory.

     

    cd /usr/local/tomcat    
  8. Unzip and untar the tomcat file.

     

    tar -zxvf apache-tomcat-6.0.10.tar.gz    
  9. 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    
  10. 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    
  11. 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.

  12. 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.

  13. If you want to shutdown your Tomcat service, the command to run is simply:
    ./shutdown.sh    
  14. 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.

  15. 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.

  1. 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.
  2. 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    
  3. Make sure we set appropriate permissions on the new file:

     

    chmod 755 /etc/init.d/tomcat    
  4. Now we use the chkconfig command to add the script to the list of system services.

     

    chkconfig --add tomcat    
  5. Now we tell the system to start the tomcat service for run levels 3 and 5.

     

    chkconfig --level 35 tomcat on    
  6. 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    
  7. 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.