Install Tomcat 7 with jdk 7 on CentOS/RHEL, Fedora, Debian, Ubuntu

Overview

Apache tomcat is a popular web server that can run java based web applications for you. This post help you setup tomcat with jdk on CentOS/RHEL, Fedora, Ubuntu or Debian systems. I will also explain how can you make run tomcat on port 80 as opposed to its default of 8080.

Install Apache Tomcat with jdk on CentOS/RHEL, Fedora, Ubuntu, Debian

Apache Tomcat require JDK to be installed on your machine, so lets install it first.


# wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2F
www.oracle.com%2F" "http://download.oracle.com/otn-pub/java/jdk/7u25-b15/jdk-7u25-linux-x64.tar.gz"
# tar zxvf jdk-7u25-linux-x64.tar.gz
# mkdir /usr/java
# mv jdk1.7.0_25/ /usr/java/

On CentOS, RHEL. Fedora


# alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_25/jre/bin/java 2000

On Debian, Ubuntu


# update-alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_25/jre/bin/java 2000

Verify java version


# java -version

java version “1.7.0_25″
Java(TM) SE Runtime Environment (build 1.7.0_25-b02)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)

If you see some other version then use alternatives --config or update-alternatives --config to select the correct version.

On CentOS, RHEL, Fedora

# alternatives --config java

There are 2 programs which provide 'java'.

Selection Command
-----------------------------------------------
*+ 1 /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
2 /usr/java/jdk1.7.0_25/jre/bin/java

Enter to keep the current selection[+], or type selection number: 2

As you can see that I also have openjdk 1.6 installed and active(java with [+] is currently in use). Selecting the second program will make the latest 1.7 active, you can run java -version to verify it.

On Debian, Ubuntu

# update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/java/jdk1.7.0_25/jre/bin/java 2000 auto mode
1 /usr/java/jdk1.7.0_25/jre/bin/java 2000 manual mode
2 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 manual mode

Press enter to keep the current choice[*], or type selection number:

Download apache and untar it, I will install tomcat under /usr/local directory


# wget http://www.eng.lsu.edu/mirrors/apache/tomcat/tomcat-7/v7.0.39/bin/apache-tomcat-7.0.39.tar.gz
# tar zxvf apache-tomcat-7.0.39.tar.gz
# mv apache-tomcat-7.0.39 /usr/local/

Tomcat should not run under root privileges, to run it as a non-root user we will create a user tomcat. If you wish to run tomcat as root simply skip this step and perform everything as root, however I strongly discourage that. The user’s home directory will be apache tomcat directory.


# groupadd tomcat
# useradd -g tomcat -d /usr/local/apache-tomcat-7.0.39 tomcat
# passwd tomcat
# chown -R tomcat.tomcat /usr/local/apache-tomcat-7.0.39/

Create the init script for tomcat. Note: If you will be using root to run tomcat instead of a non-privileged user simply replace /bin/su tomcat with sh in the script below.


# vi /etc/init.d/tomcat

Append the following to it


#!/bin/bash
# chkconfig: 2345 95 20
# description: Script to Start Stop Restart Tomcat
JAVA_HOME=/usr/java/jdk1.7.0_25
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
TOMCAT_HOME=/usr/local/apache-tomcat-7.0.39/bin

case $1 in
start)
/bin/su tomcat $TOMCAT_HOME/startup.sh
;;
stop)
/bin/su tomcat $TOMCAT_HOME/shutdown.sh
;;
restart)
/bin/su tomcat $TOMCAT_HOME/shutdown.sh
/bin/su tomcat $TOMCAT_HOME/startup.sh
;;
esac
exit 0

Open the sudoers file to edit


# visudo

Append the following line at the end


tomcat ALL=/usr/local/apache-tomcat-7.0.39/bin/shutdown.sh, /usr/local/apache-tomcat-7.0.39/bin/startup.sh

Change permissions and make sure to add it to chkconfig so that it can run automatically across server reboots


# chmod 755 /etc/init.d/tomcat
# chkconfig --add tomcat
# chkconfig --level 235 tomcat on

Start tomcat


# su - tomcat
$ /etc/init.d/tomcat start

tomcat debian 300x169 Install Tomcat 7 with jdk 7 on CentOS/RHEL, Fedora, Debian, Ubuntu

Tomcat will listen on port 8080, however you can use ip tables to route the traffic from port 8080 to port 80


# iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
# iptables -t nat -A PREROUTING -p udp -m udp --dport 80 -j REDIRECT --to-ports 8080
# service iptables save
# service iptables restart

Or run apache in front of Tomcat

On CentOS, RHEL
Open the apache configuration file and add a virtual host.


# vi /etc/httpd/conf/httpd.conf

Append the following code to it.


<VirtualHost *:80>
ServerAdmin admin@linuxdrops.com
ServerName linuxdrops.com

ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/

<Location />
Order allow,deny
Allow from all
</Location>

ErrorLog logs/tomcat-apache-error_log
CustomLog logs/tomcat-apache-access_log common
</VirtualHost>

On Debian, Ubuntu

Install apache


# apt-get install apache2

Enable proxy modules


# a2enmod proxy
# a2enmod proxy_http

Open the default site config file in an editor


# vi /etc/apache2/sites-enabled/000-default

Add the ProxyPass and ProxyPassReverse to it as shown below.


<VirtualHost *:80>
ServerAdmin webmaster@localhost
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
<!--more configuration goes here>
</VirtualHost>

Adding users for tomcat management.

Open the tomcat-users.xml file using your favorite editor


# vi /usr/local/apache-tomcat-7.0.39/conf/tomcat-users.xml

Append this under tomcat-users tag. I have added a new user and given it manager-gui role.


<tomcat-users>
<role rolename="manager-gui"/>
<user username="tomcat" password="secret" roles="manager-gui"/>
<tomcat-users/>

Don’t forget to restart tomcat for the changes to take effect.
tomcat debian2 300x169 Install Tomcat 7 with jdk 7 on CentOS/RHEL, Fedora, Debian, Ubuntu