Install Tomcat 7 on CentOS, RHEL, or Fedora
This post will cover installing and basic configuration of Tomcat 7 on CentOS 5.x. or CentOS 6.x
Tomcat 7 implements the JavaServer Pages 2.2 and Servlet 3.0 specifications and a number of new features. The Manager application also has a new look and finer-grain roles and access than 6.x
In this post, we'll install Tomcat 7, the new JDK 7, configure Tomcat as a service, create a start/stop script, and (optionally) configure Tomcat to run under a non-root user.
We will also configure basic access to Tomcat Manager and take a quick look at memory management using JAVA_OPTS
Finally, we will look at running Tomcat on port 80 as well as some strategies for running Tomcat behind Apache.
I have just updated this post with Tomcat 7.0.29, the current stable release of Tomcat 7.
If you are using a different release, simply change the file names below accordingly.
To begin, we'll need to install the Java Development Kit (JDK) 7
JDK 1.6 is the minimum JDK version for Tomcat 7.
[root@srv6 ~]# mkdir /usr/java
[root@srv6 ~]# mkdir /usr/java
[root@srv6 ~]# cd /usr/java
[root@srv6 java ]#
[root@srv6 ~]# cd /usr/java
[root@srv6 java ]#
[root@srv6 java]# tar -xzf jdk-7u5-linux-x64.tar.gz
[root@srv6 java]# tar -xzf jdk-7u5-linux-x64.tar.gz
[root@srv6 java]# JAVA_HOME=/usr/java/jdk1.7.0_05
[root@srv6 java]# export JAVA_HOME
[root@srv6 java]# PATH=$JAVA_HOME/bin:$PATH
[root@srv6 java]# export PATH
[root@srv6 java]# JAVA_HOME=/usr/java/jdk1.7.0_05
[root@srv6 java]# export JAVA_HOME
[root@srv6 java]# PATH=$JAVA_HOME/bin:$PATH
[root@srv6 java]# export PATH
JAVA_HOME=/usr/java/jdk1.7.0_05
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
JAVA_HOME=/usr/java/jdk1.7.0_05
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
[root@srv6 ~]# echo $JAVA_HOME
/usr/java/jdk1.7.0_05
[root@srv6 ~]# echo $JAVA_HOME
/usr/java/jdk1.7.0_05
[root@srv6 ~]# cd /usr/share
[root@srv6 share ]#
[root@srv6 ~]# cd /usr/share
[root@srv6 share ]#
[root@srv6 share ]# md5sum apache-tomcat-7.0.29.tar.gz
307076fa3827e19fa9b03f3ef7cf1f3f *apache-tomcat-7.0.29.tar.gz
[root@srv6 share ]# md5sum apache-tomcat-7.0.29.tar.gz
307076fa3827e19fa9b03f3ef7cf1f3f *apache-tomcat-7.0.29.tar.gz
[root@srv6 share ]# tar -xzf apache-tomcat-7.0.29.tar.gz
[root@srv6 share ]# tar -xzf apache-tomcat-7.0.29.tar.gz
[root@srv6 share]# cd /etc/init.d
[root@srv6 init.d]# vi tomcat
[root@srv6 share]# cd /etc/init.d
[root@srv6 init.d]# vi tomcat
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/usr/java/jdk1.7.0_05
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/share/apache-tomcat-7.0.29
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
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/usr/java/jdk1.7.0_05
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/share/apache-tomcat-7.0.29
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@srv6 init.d]# chmod 755 tomcat
[root@srv6 init.d]# chmod 755 tomcat
[root@srv6 init.d]# chkconfig --add tomcat
[root@srv6 init.d]# chkconfig --level 234 tomcat on
[root@srv6 init.d]# chkconfig --add tomcat
[root@srv6 init.d]# chkconfig --level 234 tomcat on
[root@srv6 init.d]# chkconfig --list tomcat
tomcat 0:off 1:off 2:on 3:on 4:on 5:off 6:off
[root@srv6 init.d]# chkconfig --list tomcat
tomcat 0:off 1:off 2:on 3:on 4:on 5:off 6:off
[root@srv6 ~]# service tomcat start
Using CATALINA_BASE: /usr/share/apache-tomcat-7.0.29
Using CATALINA_HOME: /usr/share/apache-tomcat-7.0.29
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.29/temp
Using JRE_HOME: /usr/java/jdk1.7.0_05
Using CLASSPATH: /usr/share/apache-tomcat-7.0.29/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.29/bin/tomcat-juli.jar
[root@srv6 ~]# service tomcat start
Using CATALINA_BASE: /usr/share/apache-tomcat-7.0.29
Using CATALINA_HOME: /usr/share/apache-tomcat-7.0.29
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.29/temp
Using JRE_HOME: /usr/java/jdk1.7.0_05
Using CLASSPATH: /usr/share/apache-tomcat-7.0.29/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.29/bin/tomcat-juli.jar
[root@srv6 ~]# service tomcat stop
Using CATALINA_BASE: /usr/share/apache-tomcat-7.0.29
Using CATALINA_HOME: /usr/share/apache-tomcat-7.0.29
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.29/temp
Using JRE_HOME: /usr/java/jdk1.7.0_05
Using CLASSPATH: /usr/share/apache-tomcat-7.0.29/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.29/bin/tomcat-juli.jar
[root@srv6 ~]# service tomcat stop
Using CATALINA_BASE: /usr/share/apache-tomcat-7.0.29
Using CATALINA_HOME: /usr/share/apache-tomcat-7.0.29
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.29/temp
Using JRE_HOME: /usr/java/jdk1.7.0_05
Using CLASSPATH: /usr/share/apache-tomcat-7.0.29/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.29/bin/tomcat-juli.jar
[root@srv6 ~]# service tomcat restart
Using CATALINA_BASE: /usr/share/apache-tomcat-7.0.29
Using CATALINA_HOME: /usr/share/apache-tomcat-7.0.29
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.29/temp
Using JRE_HOME: /usr/java/jdk1.7.0_05
Using CLASSPATH: /usr/share/apache-tomcat-7.0.29/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.29/bin/tomcat-juli.jar
Using CATALINA_BASE: /usr/share/apache-tomcat-7.0.29
Using CATALINA_HOME: /usr/share/apache-tomcat-7.0.29
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.29/temp
Using JRE_HOME: /usr/java/jdk1.7.0_05
Using CLASSPATH: /usr/share/apache-tomcat-7.0.29/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.29/bin/tomcat-juli.jar
[root@srv6 ~]# service tomcat restart
Using CATALINA_BASE: /usr/share/apache-tomcat-7.0.29
Using CATALINA_HOME: /usr/share/apache-tomcat-7.0.29
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.29/temp
Using JRE_HOME: /usr/java/jdk1.7.0_05
Using CLASSPATH: /usr/share/apache-tomcat-7.0.29/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.29/bin/tomcat-juli.jar
Using CATALINA_BASE: /usr/share/apache-tomcat-7.0.29
Using CATALINA_HOME: /usr/share/apache-tomcat-7.0.29
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-7.0.29/temp
Using JRE_HOME: /usr/java/jdk1.7.0_05
Using CLASSPATH: /usr/share/apache-tomcat-7.0.29/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.29/bin/tomcat-juli.jar
[root@srv6 init.d]# more /usr/share/apache-tomcat-7.0.29/logs/catalina.out
[root@srv6 init.d]# more /usr/share/apache-tomcat-7.0.29/logs/catalina.out
<tomcat-users>
<role rolename="manager-gui"/>
<user username="tomcat" password="secret" roles="manager-gui"/>
</tomcat-users>
<tomcat-users>
<role rolename="manager-gui"/>
<user username="tomcat" password="secret" roles="manager-gui"/>
</tomcat-users>
JAVA_OPTS="-Xms128m -Xmx128m"
JAVA_OPTS="-Xms128m -Xmx128m"
#!/bin/sh
JAVA_OPTS="-Xms128m -Xmx128m"
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#!/bin/sh
JAVA_OPTS="-Xms128m -Xmx128m"
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
[root@srv6 ~]# groupadd tomcat
[root@srv6 ~]# groupadd tomcat
[root@srv6 ~]# useradd -s /bin/bash -g tomcat tomcat
[root@srv6 ~]# useradd -s /bin/bash -g tomcat tomcat
[root@srv6 ~]# useradd -g tomcat -d /usr/share/apache-tomcat-7.0.29/tomcat tomcat
[root@srv6 ~]# useradd -g tomcat -d /usr/share/apache-tomcat-7.0.29/tomcat tomcat
[root@srv6 ~]# chown -Rf tomcat.tomcat /usr/share/apache-tomcat-7.0.29/
[root@srv6 ~]# chown -Rf tomcat.tomcat /usr/share/apache-tomcat-7.0.29/
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/usr/java/jdk1.7.0_05
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/share/apache-tomcat-7.0.29/bin
case $1 in
start)
/bin/su tomcat $CATALINA_HOME/startup.sh
;;
stop)
/bin/su tomcat $CATALINA_HOME/shutdown.sh
;;
restart)
/bin/su tomcat $CATALINA_HOME/shutdown.sh
/bin/su tomcat $CATALINA_HOME/startup.sh
;;
esac
exit 0
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/usr/java/jdk1.7.0_05
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/share/apache-tomcat-7.0.29/bin
case $1 in
start)
/bin/su tomcat $CATALINA_HOME/startup.sh
;;
stop)
/bin/su tomcat $CATALINA_HOME/shutdown.sh
;;
restart)
/bin/su tomcat $CATALINA_HOME/shutdown.sh
/bin/su tomcat $CATALINA_HOME/startup.sh
;;
esac
exit 0
[root@srv6 ~]# iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
[root@srv6 ~]# iptables -t nat -A PREROUTING -p udp -m udp --dport 80 -j REDIRECT --to-ports 8080
[root@srv6 ~]# iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
[root@srv6 ~]# iptables -t nat -A PREROUTING -p udp -m udp --dport 80 -j REDIRECT --to-ports 8080
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName yourdomain.com
ServerAlias www.yourdomain.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ErrorLog logs/yourdomain.com-error_log
CustomLog logs/yourdomain.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName yourdomain.com
ServerAlias www.yourdomain.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ErrorLog logs/yourdomain.com-error_log
CustomLog logs/yourdomain.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName yourdomain.com
ServerAlias www.yourdomain.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/
ErrorLog logs/yourdomain.com-error_log
CustomLog logs/yourdomain.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName yourdomain.com
ServerAlias www.yourdomain.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/
ErrorLog logs/yourdomain.com-error_log
CustomLog logs/yourdomain.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName yourdomain.com
ServerAlias www.yourdomain.com
RewriteEngine On
RewriteRule ^/$ myapp/ [R=301]
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/
ErrorLog logs/yourdomain.com-error_log
CustomLog logs/yourdomain.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName yourdomain.com
ServerAlias www.yourdomain.com
RewriteEngine On
RewriteRule ^/$ myapp/ [R=301]
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/
ErrorLog logs/yourdomain.com-error_log
CustomLog logs/yourdomain.com-access_log common
</VirtualHost>