Tomcat: redirecting traffic from port 80 to 8080 using iptables

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

Installing Tomcat 6 on Fedora, Red Hat or CentOS

Install Tomcat using yum

# yum install tomcat6-webapps

Tomcat will get installed across the following directories

/etc/tomcat6             conf files     $CATALINA_HOME/conf
/usr/share/java/tomcat6  libs           $CATALINA_HOME/lib
/usr/share/tomcat6/bin   executables    $CATALINA_HOME/bin
/var/log/tomcat6         logs           $CATALINA_HOME/logs
/var/cache/tomcat6/temp  temp files     $CATALINA_HOME/temp
/usr/share/tomcat6       home directory $CATALINA_HOME
/var/lib/tomcat6/webapps web apps       $CATALINA_HOME/webapps
/var/cache/tomcat6/work  compiled stuff $CATALINA_HOME/work

Set CATALINA_HOME in /etc/profile

export CATALINA_HOME=/usr/share/tomcat6

Tomcat can be stopped, started and restarted using the following commands

# service tomcat6 stop
# service tomcat6 start
# service tomcat6 restart

Installing JDK on Fedora, Red Hat, CentOS

  1. Visit sun java website and select the  java JDK version you would like to install: http://java.sun.com/javase/downloads/index.jsp
  2. Download the jdk-xxxxx-rpm.bin file using wget
  3. Make the downloaded file executable by running the following command
    # chmod +x jdk-xxxxx-rpm.bin   
  4. Install JDK by running the following command
    # rpm -i  jdk-xxxxx-rpm.bin  
  5. Create symbolic links for java and javac
    # ln -s /usr/java/jdkxxx/bin/java /usr/bin/java  
  6. ln -s /usr/java/jdkxxx/bin/javac /usr/bin/javac
  7. Setting JAVA_HOME and PATH for all users
    # vi /etc/profile  
    # export JAVA_HOME=/opt/jdkx.x.x_xx
    # export PATH=$PATH:$JAVA_HOME/bin
     

Which Linux Version, Build, Distribution are you running?

To find out which Linux version, build, release you are running, run the following command


# cat /proc/version

The output will be something like this:

Linux version 2.6.35.11-83.9.amzn1.i686 (mockbuild@build-31004.build) (gcc version 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC) ) #1 SMP Sat Feb 19 23:41:56 UTC 2011