How to install Tomcat 7 + JDK 7


Java_logo_and_wordmark.svgThis how-to will show you how to install Tomcat 7.x and JDK 7.x (Java Development Kit) on a CentOS VPS.

You could get one of our Java Hosting plans and have everything already installed and configured but if you want to do it yourself just follow the tutorial below.

1. Install JDK
Choose the correct version for your OS and download the latest JDK release from http://www.oracle.com/technetwork/java/javase/downloads/index.html

Create a new ‘/usr/java’ directory, move the downloaded ‘jdk’ file to ‘/usr/java’ and change to the created directory:

# mkdir /usr/java && mv jdk-7u4-linux-i586.tar.gz /usr/java/ && cd /usr/java/

Unpack jdk-7u4-linux-i586.tar.gz file:

# tar -xzf jdk-7u4-linux-i586.tar.gz

This will create directory, in our case it’s ‘jdk1.7.0_04’

To set the ‘JAVA_HOME’ path add the lines below to your ‘.bash_profile’ file:

export JAVA_HOME=/usr/java/jdk1.7.0_04/
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$PATH:$JAVA_HOME/bin

For the changes to take effect execute:

# source .bash_profile

Check if the ‘JAVA_HOME’ is set correctly:

# echo $JAVA_HOME
# /usr/java/jdk1.7.0_04/

2. Install Tomcat 7

Download the latest Tomcat release (currently 7.0.27) from http://tomcat.apache.org/download-70.cgi

# wget http://mirror.metrocast.net/apache/tomcat/tomcat-7/v7.0.27/bin/apache-tomcat-7.0.27.tar.gz

Move the file and change to the ‘/usr/share/’ directory:

# mv apache-tomcat-7.0.27.tar.gz /usr/share/ && cd /usr/share/

Unpack ‘apache-tomcat-7.0.27.tar.gz’ file:

# tar -xzf apache-tomcat-7.0.27.tar.gz

This will create ‘/usr/share/apache-tomcat-7.0.27’ directory. Optionally, you can rename this directory to ‘tomcat7’:

# mv /usr/share/apache-tomcat-7.0.27/ /usr/share/tomcat7

To configure Tomcat 7 to run as a service, change to the ‘/etc/init.d/’:

# cd /etc/init.d/

Using your favorite text editor, create a script called ‘tomcat’ with the following text:

#!/bin/bash
# description: Tomcat Start - Stop - Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/usr/java/jdk1.7.0_04
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/share/tomcat7 

case $1 in
start)
echo "Starting Tomcat"
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
echo "Stopping Tomcat"
sh $CATALINA_HOME/bin/shutdown.sh
;;
restart)
echo "Restarting Tomcat"
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
*)
echo $"Usage: $0 {start|stop}"
exit 1
;;
esac
exit 0

Make your script executable:

# chmod +x tomcat

Make your Tomcat to start at boot time:

# chkconfig --add tomcat
# chkconfig --level 3 tomcat on

Check if it is setup correctly:

# chkconfig --list tomcat

The default Tomcat port is 8080, so you can try to access it at http://domain.com:8080 where you should see Tomcat’s default home page.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Tutorials
How to Install Tine 2.0 on CentOS 7
Tutorials
Install Bolt CMS on an Ubuntu VPS with Nginx and MariaDB.
Tutorials
Install Grav CMS on an Ubuntu VPS
  • andrian

    andrianandrian

    Author Reply

    i have follow all the step as mentioned above. But when i access ip-address:8080 the firefox connecting to the address for sometimes and then “Unable to connect” show.
    The output of netstat -an |grep 8080 is:
    tcp 0 0 :::8080 :::* LISTEN


  • vinay

    vinayvinay

    Author Reply

    this is really good tutorial.

    i am to setup tomcat in first go.. thanks


  • Eduardo

    EduardoEduardo

    Author Reply

    Thanks!


  • Java Training

    Thanks for sharing such informative article on Java technology. It is most preferred technology among developers to create stunning mobile application fast and easy. It also created massive career opportunity for aspiring professionals.


  • KevinKim

    Thanks! You are my hero!