This guide demonstrates how to install Tomcat 7 in CentOS 6.3 as your primary Webserver. This uses Tomcat mod_jk connector for httpd and
Tomcat 7 communications. This enables accesses to Tomcat application through port 80, and not having to explicitly enter the Tomcat port. We build mod_jk connector from the source.
It is the assumption that you already have a running CentOS 6.3 system. You should also be logged to your system as root or a user with
root privileges to use this procedure.
First let us install Apache 2 (httpd) and the tools that we need to build mod_jk. Please skip this if you already have these in your
system.
yum groupinstall "Development Tools"
yum install httpd httpd-devel wget
Enable Apache 2 to start at boot time.
chkconfig --levels 235 httpd on
Start Apache2
/etc/init.d/httpd start
Test if Apache2 is running properly by accessing the IP address of your system or the domain name of your system, from your web browser.
You may have to disable your iptables if port 80 is not yet open. Disable iptables temporarily by /etc/init.d/iptables stop.
# Assuming this is the IP address of your system.
http://192.168.0.200
Download and install Java 7
For more control over what we install in our system, we choose to install Java 7 ourselves, from Sun's website. Issue the following
command from your shell prompt.
wget --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com"
http://download.oracle.com/otn-pub/java/jdk/7u11-b21/jdk-7u11-linux-x64.tar.gz
Note that we adopted the above method to bypass Sun's JDK download requirements via wget from an answer to question in StackOverflow, and it worked well for us. Just supply the correct download URL.
When download is done issue the following command to untar/extract the downloaded file, and for this demonstration, we move the extracted folder to
/opt folder as /opt/jdk7.
tar xvfz [location of downloaded_file]
From this point we reference and use our Java 7 installation as /opt/jdk7.
Download and install Tomcat 7
# Just select the mirror nearest you
wget http://www.gtlib.gatech.edu/pub/apache/tomcat/
tomcat-7/v7.0.34/bin/apache-tomcat-7.0.34.tar.gz
tar xvfz apache-tomcat-7.0.34.tar.gz
mv apache-tomcat-7.0.34 /opt/tomcat
chmod +x /opt/tomcat/bin/*.sh
Create Tomcat user and group and set file owners/permissions. User tomcat will be the owner of the Tomcat process.
groupadd tomcat
useradd -g tomcat -d /opt/tomcat tomcat
chown tomcat:tomcat /opt/tomcat
Create Tomcat startup script and Tomcat service
The script below sets Tomcat to use 128M heap size and maximum PermSize of 128M. Adjust them to suit your needs and/or according to your
server resources. Add more JVM options as your situation may require. Also note the chkconfig entry in line 2 below, it
is required for chkconfig command to work against this file. See the man pages of chkconfig to learn more.
#!/bin/sh
#chkconfig: 2345 95 20
CATALINA_HOME=/opt/tomcat; export CATALINA_HOME
JAVA_HOME=/opt/jdk7; export JAVA_HOME
TOMCAT_OWNER=tomcat; export TOMCAT_OWNER
JAVA_OPTS="-Xms128M -Xmx128M -XX:MaxPermSize=128M"; export JAVA_OPTS
start() {
echo -n "Starting Tomcat: "
su $TOMCAT_OWNER -c $CATALINA_HOME/bin/startup.sh
}
stop() {
echo -n "Stopping Tomcat: "
su $TOMCAT_OWNER -c $CATALINA_HOME/bin/shutdown.sh
}
##
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: tomcat {start|stop|restart}"
exit
esac
Save the above as tomcat in /etc/init.d.
Create your new tomcat service
chmod +x /etc/init.d/tomcat
chkconfig --add tomcat
chkconfig --levels 235 tomcat on
Create Tomcat server user
Locate the tomcat-users.xml in /opt/tomcat/conf folder and edit the tomcat-users section similar to what is shown below.
<tomcat-users>
<role rolename="manager" />
<role rolename="manager-gui" />
<role rolename="admin"/>
<role rolename="admin-gui" />
<user username="[USER]" password="[PASSWORD]"
roles="admin,admin-gui,manager,manager-gui" />
</tomcat-users>
Replace [USER] and [PASSWORD] with your actual values.
Now let us start our Tomcat.
/etc/init.d/tomcat start
Test your new tomcat installation.
http://{your-server or ipaddress}:8080
You should see the Tomcat administration page. Note that port 8080 could be blocked by your firewall if that port is not yet
open. You may want to disable iptables temporarily.
Build from source and install mod_jk Apache connector
This is the Apache module that would allow communication between the Apache 2 and Tomcat. Download the source and build as below.
wget http://archive.apache.org/dist/tomcat/tomcat-connectors/
jk/tomcat-connectors-1.2.32-src.tar.gz
tar xvfz [downloaded_file]
Note that the procedure below is the reason why we installed httpd-devel and other build tools.
cd [extract location]/native
./configure --with-apxs=/usr/sbin/apxs
#
make
If there were no errors the output should be [extract location]/native/apache-2/mod_jk.so. Copy this output, our mod_jk.so, to
Apache2 modules folder.
cp [extract location]/native/apache-2.0/mod_jk.so /usr/lib64/httpd/modules/
Create mod_jk worker
This is the configuration of Tomcat process that would execute requests in behalf of Apache 2.
# Use your favorite text editor if not using vi.
vi /etc/httpd/conf/workers.properties
workers.tomcat_home=/opt/tomcat
workers.java_home=/opt/jdk7
ps=/
worker.list=default
worker.default.port=8009
worker.default.host=localhost
worker.default.type=ajp13
worker.default.lbfactor=1
Save.
Create mod_jk configuration file.
vi /etc/httpd/conf.d/mod_jk.conf
<ifmodule mod_jk.c>
JkWorkersFile /etc/httpd/conf/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel error
</ifmodule>
When saved this goes to /etc/httpd/conf.d. Please note that all configuration files in /etc/httpd/conf.d are loaded automatically
by httpd.
Create virtual host and non Tomcat subdomains
This setup is useful if you are planning to install scripts like webalizer or phpmyadmin, etc or planning to create non Tomcat applications. Note
that acesses like yourdomin.com/{directory} is handled by Tomcat and {directory} is expected to be in your tomcat webapps
directory. Subdomains will work just make sure to update your DNS zone for your subdomains.
NameVirtualHost your-server-ip-address
<virtualhost yourdomain.com>
JkMount /* default
ServerName yourdomain.com
ServerAdmin admin@emil.com
DocumentRoot /opt/tomcat/webapps
<directory /opt/tomcat/webapps>
Options -Indexes
</directory>
</virtualhost>
# For non Tomcat subdomains
<virtualhost nontomcat.yourdoman.com>
ServerName nontomcat.yourdomain.com
DocumentRoot /var/www/nontomcat
# more directives
</virtualhost>
Save the above into your conf.d folder with conf extension, example vhost.conf and then restart httpd. Note
that with this setup, access and error logs goes to the default httpd logs directory which is /var/log/httpd.
/etc/init.d/httpd restart
Create tomcat host
The setup below sets your {YOUR-WEB-APP} as your tomcat ROOT as indicated by Context path="". Set path="/" to run your app
as yourdomain.com/webapp instead of yourdomain.com.
Update the Host entry of your server.xml located at /opt/tomcat/conf as shown below:
<Host name="yourdomain.com" appBase="webapps" unpackWARs="true"
autoDeploy="true" >
<Context path=""
docBase="/opt/tomcat/webapps/{YOUR-WEB-APP-NAME}"
debug="0" reloadable="true" />
<Valve className=
"org.apache.catalina.valves.AccessLogValve"
directory="/opt/tomcat/logs" prefix="tomcat_access_"
suffix=".log" pattern="common" resolveHosts="false" />
<Alias>www.yourdomain.com</Alias>
</Host>
Restart Tomcat
/etc/init.d/tomcat restart
Deploy your web application to /opt/tomcat/webapps. Run your web app as shown below.
http://yourdomain.com
For some reason, while you are working in your setup, and Tomcat servlets suddenly throws java.lang.ClassFormatError, you may want to delete
/opt/tomcat/work/Catalina folder and restart tomcat.
That's it Good Luck.