Home / Operating System / Install Tomcat 7 on Centos 6.3

Install Tomcat 7 on Centos 6.3

This post will cover installing and basic configuration of apache-tomcat-7.0.30
If you do not already have the Java Development Kit (JDK) installed on your machine, you will need to download and install the required JDK for your platform.

If you do have the JDK installed, you can skip to: Step 2: Download and Install the  apache-tomcat-7.0.30

Step 1: Install the JDK
[root@station1 Downloads]# rpm -ivh jdk-7u7-linux-x64.rpm

–>

[root@station1 Downloads]# JAVA_HOME=/usr/java/jdk1.7.0_17
[root@station1 Downloads]# export JAVA_HOME
[root@station1 Downloads]# PATH=$JAVA_HOME/bin:$PATH
[root@station1 Downloads]# export PATH
[root@station1 Downloads]# vim /root/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs
JAVA_HOME=/usr/java/jdk1.7.0_17/
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
#PATH=$PATH:$HOME/bin
#export PATH

Restart or logout and login
[root@station1 Downloads]# echo $JAVA_HOME
/usr/java/jdk1.7.0_17/
       Step 2: Download and Unpack apache-tomcat-7.0.30 (or latest)
To download apache-tomcat click here
[root@station1 Downloads]# tar -xzvf apache-tomcat-7.0.30.tar.gz 
[root@station1 Downloads]# mv apache-tomcat-7.0.30 /usr/share/ 
[root@station1 Downloads]# vim /etc/init.d/tomcat 
    #!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
    # chkconfig: 234 20 80
JAVA_HOME=/usr/java/jdk1.7.0_17
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/share/apache-tomcat-7.0.30

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

[root@station1 ~]# chmod 755 /etc/init.d/tomcat
[root@station1 ~]# chkconfig –add tomcat
[root@station1 ~]# chkconfig –level 234 tomcat on
[root@station1 ~]# chkconfig –list tomcat
tomcat             0:off    1:off    2:on    3:on    4:on    5:off    6:off
[root@station1 ~]# service tomcat stop
Using CATALINA_BASE:   /usr/share/apache-tomcat-7.0.30
Using CATALINA_HOME:   /usr/share/apache-tomcat-7.0.30
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.30/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_17
Using CLASSPATH:       /usr/share/apache-tomcat-7.0.30/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.30/bin/tomcat-juli.jar
[root@station1 ~]# service tomcat start
Using CATALINA_BASE:   /usr/share/apache-tomcat-7.0.30
Using CATALINA_HOME:   /usr/share/apache-tomcat-7.0.30
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.30/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_17
Using CLASSPATH:       /usr/share/apache-tomcat-7.0.30/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.30/bin/tomcat-juli.jar
[root@station1 ~]# service tomcat restart
Using CATALINA_BASE:   /usr/share/apache-tomcat-7.0.30
Using CATALINA_HOME:   /usr/share/apache-tomcat-7.0.30
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.30/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_17
Using CLASSPATH:       /usr/share/apache-tomcat-7.0.30/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.30/bin/tomcat-juli.jar
Using CATALINA_BASE:   /usr/share/apache-tomcat-7.0.30
Using CATALINA_HOME:   /usr/share/apache-tomcat-7.0.30
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.30/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_07
Using CLASSPATH:       /usr/share/apache-tomcat-7.0.30/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.30/bin/tomcat-juli.jar
For logs
[root@station1 ~]# less /usr/share/apache-tomcat-7.0.30/logs/catalina.out
We can now access the Tomcat Manager page at http://127.0.0.1:8080
————
 How to Run Tomcat using Minimally Privileged (non-root) Use
In our Tomcat configuration above, we are running Tomcat as Root.

For security reasons, it is always best to run services with the only those privileges that are necessary.

There are some who make a strong case that this is not required, but it’s always best to err on the side of caution.

To run Tomcat as non-root user, we need to do the following:

1. Create the group ‘tomcat’:

[root@station1 ~]# groupadd tomcat
[root@station1 ~]# useradd -s /bin/bash -g tomcat tomcat
Change ownership of the tomcat files to the user tomcat we created above
[root@station1 ~]# chown -Rf tomcat.tomcat /usr/share/apache-tomcat-7.0.30/
Adjust the start/stop service script we created above. In our new script, we need to su to the user tomcat:
 [root@station1 ~]# vim /etc/init.d/tomcat
    #!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
JAVA_HOME=/usr/java/jdk1.7.0_17
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/share/apache-tomcat-7.0.30

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

               How to Run Tomcat on Port 80 as Non-Root User
Note: the following applies when you are running Tomcat in “stand alone” mode with Tomcat running under the minimally privileged user Tomcat we created in the previous step.

To run services below port 1024 as a user other than root, you can add the following to your IP tables

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
Be sure to save and restart your IP Tables
Running Tomcat behind Apache
As an alternative to running Tomcat on port 80, if you have Apache in front of Tomcat, you can use mod_proxy as well as ajp connector to map your domain to your Tomcat application(s) using an Apache vhost as shown below.

While Tomcat has improved it’s ‘standalone performance’, I still prefer to have Apace in front of it for a number of reasons.

In your Apache config, be sure to set KeepAlive to ‘on’. Apache tuning, of course, is a whole subject in itself…

 76 KeepAlive On
VHOST with mod_proxy
<VirtualHost *:80>
ServerAdmin tomcat@vn.tips4admin.com
ServerName vn.tips4admin.com
ServerAlias www.vn.tips4admin.com

ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>

ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://s127.0.0.1:8080/

ErrorLog logs/error_log
CustomLog logs/access_log common

</VirtualHost>

Print Friendly

About dongpolice

Check Also

Tao-bo-cai-dat-windows-to-usb

Tạo bộ cài đặt Windows từ USB dễ dàng – WinToUSB 3.4 Enterprise Full Key

Tạo bộ cài đặt Windows từ USB dễ dàng – WinToUSB 3.4 Enterprise Full Key WinToUSB …