How to enable JMX on Tomcat running on EC2 (behind a firewall)

Step 1: Copy catalina-jmx-remote.jar into Tomcat’s lib folder. This jar file is available in the extras folder of Tomcat’s binary distribution.

Step 2: Modify Tomcat’s server.xml by adding a new Listener entry.

<Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleListener" 
rmiRegistryPortPlatform="9090" rmiServerPortPlatform="9091"/>

Step 3: Open both of these ports in your firewall. Ports 9090 and 9091 and just examples, you can choose to use any port numbers you like.

Step 4: Add the following options to Tomcat startup script.

-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false 
-Djava.rmi.server.hostname=public-ip (or public dns)

All done! Restart Tomcat and connect to it using jconsole or visualvm from a remote system.

Installing Oracle JDK and Tomcat on an EC2 Linux instance

Installing Java JDK on Amazon Linux AMI

Most likely you already have Open JDK installed on you EC2 instance. You will have replace OpenJDK with Oracle JDK. Here is how you do it.

Perform all the operations below as root.Get the latest Oracle (Sun) JDK rmp binary from the Oracle website and copy it to the EC2 instance.

# make it exec
chmod +x jdk-6u34-linux-x64-rpm.bin

# Install Java
sudo ./jdk-6u34-linux-x64-rpm.bin

# Check if the default java version is set to sun jdk
# Normally it says OpenJDK
java -version

# Create another alternative for Java for Sun JDK
sudo /usr/sbin/alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_34/bin/java 20000

# Set the SUN JDK as the default java
sudo /usr/sbin/alternatives --config java

# Verify if change in SDK was done.
java -version

Installing Tomcat on Amazon Linux AMI

# Check if tomcat is already installed 
yum info tomcat6

# To install tomcat6, run the following command
yum install tomcat6

# Start Tomcat as follows
service tomcat6 start

Tomcat has now been installe in /usr/share/tomcat6/

# To check that Tomcat has indeed started and is listening on port 8080:
fuser -v -n tcp 8080

#To have Tomcat start automatically on instance reboot
chkconfig --level 345 tomcat6 on 

Tomcat by default runs on port 8080. Traffic can be redirected from port 80 to 8080 by running the following commands:

/sbin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
/sbin/service iptables save

Installing JDK, Tomcat on EC2 Fedora Core 8 instance

  • Download JDK to say /mnt/downloads/

wget -O jdk1.6.bin http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/jdk-6u16-linux-i586.bin?BundledLineItemUUID=uyNIBe.lLxwAAAEjdGQlL7Q3&OrderID=FE1IBe.lTu8AAAEjZGQlL7Q3&ProductID=Dx1IBe.prBgAAAEirRcTvuC_&FileName=/jdk-6u16-linux-i586.bin

  • cd /usr/local/
  • sh /mnt/downloads/jdk1.6.bin
  • mv jdk1.6.0_16/ jdk1.6
  • vi /etc/profile

export JAVA_HOME=/usr/local/jdk1.6
export PATH=$PATH:$JAVA_HOME/bin

  •  Download Tomcat to say /mnt/downloads

wget http://www.gtlib.gatech.edu/pub/apache/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.tar.gz

  • tar -zxvf apache-tomcat-6.0.20.tar.gz
  • mv apache-tomcat-6.0.20 /usr/local/tomcat6
  • useradd -d /home/tomcat -s /bin/bash -m tomcat
  • chown -R tomcat:tomcat /usr/local/tomcat6/
  • test using commans: su – tomcat -c /usr/local/tomcat6/bin/startup.sh
  • shutdown:  su – tomcat -c /usr/local/tomcat6/bin/shutdown.shconfigure the startup script

or use startup script:

#vi /etc/init.d/tomcat

use the following script:

http://www.linuxzone.org/scripts/tomcat.txt

Add it to chkconfig and turn it on:

# chkconfig --add tomcat
# chkconfig tomcat on