First verify that Tomcat is running on port 8080. Run the following command

# netstat -ntl

The output will look something like

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN
tcp        0      0 :::8009                     :::*                        LISTEN
tcp        0      0 :::8080                     :::*                        LISTEN
tcp        0      0 :::22                       :::*                        LISTEN

Run the following command to redirect port 80 traffic to port 8080

# iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

Run the folloing command to verify that redirect is working fine

# iptables -t nat -L

The output will look something like

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
REDIRECT   tcp  --  anywhere             anywhere            tcp dpt:http redir ports 8080

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination

Run the following command to remove the routing

# iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

 

FROM: Glass Onion Blog

今天在Linux上装了一个Tomcat,发现它自带的那个jsvc启动脚本实在太古老了。于是就在网上Search了一下,加上自己修改code。写了一个适合于tomcat6用的jsvc启动脚本。使用以后就可以把tomcat做为linux的Service来自启动了。

把这段代码保存为 /etc/rc.d/init.d/tomcat ,然后运行
#chkconfig –add tomcat
用–list看一下是否系统已有tomcat启动文件
#ckhconfig –list
即可把tomcat 添加为系统服务自动随系统启动了。这个脚本会在runlevel 3/4/5三种模式自动启动。

 

代码
  1. #!/bin/sh   
  2. ##############################################################################   
  3. #   
  4. #   Copyright 2004 The Apache Software Foundation.   
  5. #   
  6. #   Licensed under the Apache License, Version 2.0 (the "License");   
  7. #   you may not use this file except in compliance with the License.   
  8. #   You may obtain a copy of the License at   
  9. #   
  10. #       http://www.apache.org/licenses/LICENSE-2.0   
  11. #   
  12. #   Unless required by applicable law or agreed to in writing, software   
  13. #   distributed under the License is distributed on an "AS IS" BASIS,   
  14. #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   
  15. #   See the License for the specific language governing permissions and   
  16. #   limitations under the License.   
  17. ##############################################################################   
  18. #   
  19. # Small shell script to show how to start/stop Tomcat using jsvc   
  20. # If you want to have Tomcat running on port 80 please modify the server.xml   
  21. # file:   
  22. #   
  23. #    <!– Define a non-SSL HTTP/1.1 Connector on port 80 –>   
  24. #    <Connector className="org.apache.catalina.connector.http.HttpConnector"   
  25. #               port="80" minProcessors="5" maxProcessors="75"   
  26. #               enableLookups="true" redirectPort="8443"   
  27. #               acceptCount="10" debug="0" connectionTimeout="60000"/>   
  28. #   
  29. # That is for Tomcat-5.0.x (Apache Tomcat/5.0)   
  30. #   
  31. # chkconfig: 345 87 13   
  32. # description: Tomcat Daemon   
  33. # processname: jsvc   
  34. # pidfile: /var/run/jsvc.pid   
  35. # config:   
  36. #   
  37. # Source function library   
  38. . /etc/rc.d/init.d/functions   
  39. #   
  40. # Adapt the following lines to your configuration   
  41. JAVA_HOME=/usr/java/default  
  42. CATALINA_HOME=/opt/apache-tomcat   
  43. DAEMON_HOME=/opt/apache-tomcat   
  44. TOMCAT_USER=root   
  45.   
  46. # for multi instances adapt those lines.   
  47. TMP_DIR=/var/tmp   
  48. PID_FILE=/var/run/jsvc.pid   
  49. CATALINA_BASE=/opt/apache-tomcat   
  50.   
  51. CATALINA_OPTS="-Djava.library.path=/home/jfclere/jakarta-tomcat-connectors/jni/native/.libs"  
  52. CLASSPATH=   
  53. $JAVA_HOME/lib/tools.jar:   
  54. $CATALINA_HOME/bin/commons-daemon.jar:   
  55. $CATALINA_HOME/bin/bootstrap.jar   
  56.   
  57. prog="tomcat"  
  58.   
  59. start(){   
  60.     #   
  61.     # Start Tomcat   
  62.     #   
  63.     echo -n $"Starting $prog:"  
  64.     $DAEMON_HOME/bin/jsvc    
  65.     -user $TOMCAT_USER    
  66.     -home $JAVA_HOME    
  67.     -Dcatalina.home=$CATALINA_HOME    
  68.     -Dcatalina.base=$CATALINA_BASE    
  69.     -Djava.io.tmpdir=$TMP_DIR    
  70.     -wait 10    
  71.     -pidfile $PID_FILE    
  72.     -outfile $CATALINA_HOME/logs/catalina.out    
  73.     -errfile ‘&1‘    
  74.     $CATALINA_OPTS    
  75.     -cp $CLASSPATH    
  76.     org.apache.catalina.startup.Bootstrap   
  77.     #   
  78.     # To get a verbose JVM   
  79.     #-verbose    
  80.     # To get a debug of jsvc.   
  81.     #-debug    
  82.     RETVAL=$?   
  83.     [ $RETVAL = 0 ] && touch /var/lock/subsys/jsvc   
  84.     [ $RETVAL = 0 ] && echo_success || echo_failure   
  85.     echo   
  86.     return $RETVAL   
  87. }   
  88.   
  89. stop(){   
  90.     #   
  91.     # Stop Tomcat   
  92.     #   
  93.     echo -n $"Stopping $prog: "  
  94.     $DAEMON_HOME/bin/jsvc    
  95.     -stop    
  96.     -pidfile $PID_FILE    
  97.     org.apache.catalina.startup.Bootstrap   
  98.     RETVAL=$?   
  99.     [ $RETVAL = 0 ] && rm /var/lock/subsys/jsvc   
  100.     [ $RETVAL = 0 ] && echo_success || echo_failure   
  101.     echo   
  102.     return $RETVAL   
  103. }   
  104.   
  105. case "$1" in   
  106.   start)   
  107.     start   
  108.   ;;   
  109.   stop)   
  110.     stop   
  111.   ;;   
  112.   restart|reload)   
  113.     stop   
  114.     start   
  115.   ;;   
  116.   status)   
  117.     status jsvc   
  118.     RETVAL=$?   
  119.   ;;   
  120.   *)   
  121.     echo "Usage %0 start/stop/restart"  
  122.     exit 1;;   
  123. esac