How to setup Tomcat 7 as your primary webserver on Debian Squeeze

Tuesday July 26, 2011 ()

I just completed migrating my website to a brand new Virtual Private Server (VPS) running Debian 6 (Squeeze). As a JSF Developer and my website being written in JSF, my challenge is to make Tomcat 7 my primary webserver and running alongside Apache 2. I want to share my experience below:

Install Java 6.

    apt-get install openjdk-6-jre

Dowload and install Tomcat 7.

    tar xvfz apache-tomcat-7.0.19.tar.gz
    mv apache-tomcat-7.0.19 /opt/tomcat		
    chmod +x /opt/tomcat/bin/*.sh

Create Linux tomcat user and group

    groupadd tomcat
    useradd -g tomcat -d /opt/tomcat tomcat
    usermod -G www-data tomcat
    chown tomcat:tomcat /opt/tomcat -R

Create Tomcat startup script. Save it as tomcat in /etc/init.d. The script below is set to use 128M heap size. Change it to suit your needs.


     CATALINA_HOME=/opt/tomcat; export CATALINA_HOME
     JAVA_HOME=/usr/lib/jvm/java-6-openjdk; export JAVA_HOME
     TOMCAT_OWNER=tomcat; export  TOMCAT_OWNER
     JAVA_OPTS="-Xms128M -Xmx128M"; 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}"

Create your new tomcat service.

    chmod +x /etc/init.d/tomcat
    update-rc.d tomcat defaults

Create Tomcat server user.

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

Start tomcat instance

    /etc/init.d/tomcat start

Test you now tomcat installation


You should see the Tomcat administration page.

Install mod_jk Apache connector. This is the Apache module that would allow communication between the webserver (httpd) and Apache Tomcat.

    apt-get install libapache2-mod-jk

Create mod_jk worker. The configuration of Tomcat process that would execute requests in behalf of the webserver.

    vi /etc/apache2/


Create mod_jk configuration file

    vi /etc/apache2/conf.d/jk.conf

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

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 apps. 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. Save your file in /etc/apache2/sites-available/.

    NameVirtualHost your-server-ip-address

    	JkMount /* default
    	DocumentRoot /opt/tomcat/webapps
    	ErrorLog /opt/tomcat/logs/error.log
    	CustomLog /opt/tomcat/logs/access.log common
    	<directory /opt/tomcat/webapps>
        	Options -Indexes
    # For non Tomcat subdomains
           DocumentRoot /where/ever/
          ..... more directives

<virtualhost> and <virtualhost> (as above) can be replaced with <virtualhost server-ip-address>. Either way should work just properly.

Enable your domain and restart Apache2.

    /etc/init.d/apache2 restart

Create tomcat host.

The setup below sets your {YOUR-WEB-APP} as your tomcat ROOT as indicated by path="". Set path="/" to run your app as rather than 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" />        
        <!- please notes on logs down below -->
    	<Valve className=
		directory="/opt/tomcat/logs" prefix="tomcat_access_"  
          suffix=".log" pattern="common" resolveHosts="false" />



Please note that we have created two loggers that would create 2 identical access logs. Apache generates logs for us through mod_jk earlier in this blog in our virtual host setup. As shown above, Tomcat also generates access logs via AccessLogValve we created in sever.xml.

I choose to only keep logs generated by mod_jk/Apache2.

Restart Tomcat

    /etc/init.d/tomcat restart

That's it. Deploy your web application to /opt/tomcat/webapps. Run your web app as shown below.

Good luck.


Comments (How to setup Tomcat 7 as your primary webserver on Debian Squeeze )