Install Tomcat 7 as your primary Webserver in CentOS 6.3

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.

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:"

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

    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.

     #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/
     stop() {
        echo -n "Stopping Tomcat: "
        su $TOMCAT_OWNER -c $CATALINA_HOME/bin/


     case "$1" in
        echo $"Usage: tomcat {start|stop|restart}"

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.

    	<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" />

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.


 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

If there were no errors the output should be [extract location]/native/apache-2/   Copy this output, our, to Apache2 modules folder.

cp [extract location]/native/apache-2.0/ /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/


Create mod_jk configuration file.

    vi /etc/httpd/conf.d/mod_jk.conf

    <ifmodule mod_jk.c>
    	JkWorkersFile /etc/httpd/conf/
    	JkLogFile /var/log/apache2/mod_jk.log
    	JkLogLevel error

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{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

    	JkMount /* default
    	DocumentRoot /opt/tomcat/webapps
    	<directory /opt/tomcat/webapps>
        	Options -Indexes

    # For non Tomcat subdomains
          DocumentRoot /var/www/nontomcat
          # more directives

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 instead of

Update the Host entry of your server.xml located at /opt/tomcat/conf as shown below:

    <Host name="" appBase="webapps" unpackWARs="true"
           autoDeploy="true" >

    	<Context path="" 
                debug="0" reloadable="true" />        

    	<Valve className=
                directory="/opt/tomcat/logs" prefix="tomcat_access_"  
                suffix=".log" pattern="common" resolveHosts="false" />



Restart Tomcat

    /etc/init.d/tomcat restart

Deploy your web application to /opt/tomcat/webapps.    Run your web app as shown below.

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.

If you like the article, please share.
(Site URL pattern has changed as a result social actions counter was reset.)

Comment icon   Comments (Newest first)