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.


    wget http://www.gtlib.gatech.edu/pub/apache/tomcat/
	tomcat-7/v7.0.19/bin/apache-tomcat-7.0.19.tar.gz
    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.


     #!/bin/sh

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


Create your new tomcat service.


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

Create Tomcat server user.


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

Start tomcat instance


    /etc/init.d/tomcat start

Test you now tomcat installation


    http://{your-server}:8080

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/workers.properties
	
	

    workers.tomcat_home=/opt/tomcat
    workers.java_home=/usr/lib/jvm/java-6-openjdk
    ps=/
    worker.list=default
    worker.default.port=8009
    worker.default.host=localhost
    worker.default.type=ajp13
    worker.default.lbfactor=1

Create mod_jk configuration file

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


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

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


    NameVirtualHost your-server-ip-address

    <virtualhost yourdomain.com>
    	JkMount /* default
    	ServerName yourdomain.com
    	ServerAdmin admin@emil.com
    	DocumentRoot /opt/tomcat/webapps
    	ErrorLog /opt/tomcat/logs/error.log
    	CustomLog /opt/tomcat/logs/access.log common
    	<directory /opt/tomcat/webapps>
        	Options -Indexes
    	</directory>
    </virtualhost>
    #
    # For non Tomcat subdomains
    #
    <virtualhost subdomain.yourdoman.com>
           ServerName sudomain.yourdomain.com
           DocumentRoot /where/ever/
          ..... more directives
    </virtualhost>

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

Enable your domain and restart Apache2.


    a2ensite yourdomain.com
    /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 yourdomain.com/webapp rather than 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" />        
        <!- please notes on logs down below -->
    	<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>

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.


    http://yourdomain.com

Good luck.


19,852

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