How to install Java and Apache Tomcat on Amazon EC2 instance
In this post, we go through quick steps to install Oracle Java or Open JDK and Apache Tomcat on Amazon EC2 Instance. You should be done with the installation within 60 seconds of starting.
Requirements
- amazon ec2 instance (assumed available)
First let’s see installation of Oracle Java. First download the latest Oracle JDK to your home directory. Oracle requires to ‘accept’ the licensing agreement before allowing you to start the download. To get around this we add a cookie to the request.
1 2 3 | $wget --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com" "http://download.oracle.com/otn-pub/java/jdk/7u60-b19/jdk-7u60-linux-x64.tar.gz" |
Then unzip the tar file
1 2 3 | $tar zxpvf jdk-7u60-linux-x64.tar.gz |
Now make the following directory for the Java installation. We create a latest directory and them link your current installation to the latest. This way you upgrades can be managed easily. This is only a personal preference.
1 2 3 4 5 | $mkdir /usr/java/latest $mkdir /usr/java/jdk1.7.0_60 $ln /usr/java/jdk1.7.0_60 /us/java/latest |
Now we setup a few environment variables for your Java installation. You might want to add it to your bash profile as well, for them to survive restarts.
1 2 3 4 5 6 | $JAVA_HOME=/usr/java/latest/ $export JAVA_HOME $PATH=$JAVA_HOME/bin:$PATH $export PATH |
Now we check if java is installed properly by running the following command which should tell you the java version.
1 2 3 | $java -version |
That’s Oracle JDK installation done. Installing Open JDK is much simpler.
1 2 3 | $sudo yum install java-1.7.0-openjdk.x86_64 |
Just as a note you might want to check if Open JDK 6.0 is already installed on the instance. If Open JDK 6.0 is installed you can either remove it or just switch to using the latest installed java as follows
1 2 3 | $sudo alternatives --config java |
That’s java installation done. Please remember you want to use either Open JDK or Oracle JDK and not both.
Installing Apache Tomcat 7 is even more straightforward than Open JDK.
1 2 3 | $sudo yum install tomcat7 |
You can check the installation as follows
1 2 3 4 | $sudo lsof -i $sudo chkconfig tomcat7 on |
Here’s how you can start and stop Apache Tomcat after installation.
1 2 3 4 | $sudo service tomcat7 start $sudo service tomcat7 stop |
That’s it you are done! Enjoy Apache Tomcat and your Java.
Update
After receiving some questions about installing Apache Tomcat without RPM, here’s a new post on Installing Apache Tomcat on Amazon EC2 without using RPM