Thursday, December 15, 2011

Remove context from url from Tomcat applications

If you are like me you love tomcat simplicity and you are used to deploy your WAR files or exploded directories in webapps folder directly. However in order to serve URLs without the context included you will need to stop that practice.

You might come with a hack like I discover a while back, but as you can read there that will result in the same application being deployed several times.

Here is how to configure tomcat (Tested in tomcat 7) to serve content from two different URLs
  1. Make your websites resolve to real IPs. For production you rely on external DNS, for other environments you rely on internal DNS and many times in you /etc/hosts:
    $ vi /etc/hosts
    ...
    127.0.0.1   bhubdev.nestorurquiza.com
    127.0.0.1   bhubdev2.nestorurquiza.com
    ...
    
  2. You need to add them both to Engine section in conf/server.xml:
    ...
     <host name="bhubdev.nestorurquiza.com"  appBase="bhub-app"
        unpackWARs="true" autoDeploy="true"
        deployOnStartup="true" />
      <host name="bhubdev2.nestorurquiza.com"  appBase="bhub2-app"
        unpackWARs="true" autoDeploy="true"
        deployOnStartup="true" />
    
    
  3. Create these folders (two per domain as you can see). Be sure to change to proper paths in your system:
    $ mkdir /Users/nestor/Downloads/apache-tomcat-7.0.22/bhub-app
    $ mkdir /Users/nestor/Downloads/apache-tomcat-7.0.22/conf/Catalina/bhubdev.nestorurquiza.com
    $ mkdir /Users/nestor/Downloads/apache-tomcat-7.0.22/bhub2-app
    $ mkdir /Users/nestor/Downloads/apache-tomcat-7.0.22/conf/Catalina/bhub2dev.nestorurquiza.com
    
  4. Restart tomcat.
  5. Deploy the WAR files as:
    /Users/nestor/Downloads/apache-tomcat-7.0.22/bhub-app/ROOT.war
    /Users/nestor/Downloads/apache-tomcat-7.0.22/bhub2-app/ROOT.war
    
  6. Alternatively deploy the exploded WAR file as:
    unzip bhub-app.war -d /Users/nestor/Downloads/apache-tomcat-7.0.22/bhub-app/ROOT/
    unzip bhub-app2.war -d /Users/nestor/Downloads/apache-tomcat-7.0.22/bhub2-app/ROOT/
    

Now if you use maven and you want to deploy from there you probably have something like the below if you are still deploying to webapp folder (using ant tasks to deploy either the WAR file or the exploded directory)
...

    ${MVN_TOMCAT_HOME_DEPLOY}
...

    
    
        
    

...

    Deploying WAR locally
    

...

You will need to update that to point to the new directories and not to webapps anymore.
...

  ${CATALINA_HOME}
...

    
    
        
    

...

    
    

...

Do not forget to add to your profile the needed for Maven environment variable:
$ vi ~/.profile:
#old needed variable
#export MVN_TOMCAT_HOME_DEPLOY=/opt/tomcat/webapps
#new needed variable
export CATALINA_HOME=/opt/tomcat
$ source ~/.profile

5 comments:

Alberto Navarro said...

If you want to serve the same web in two differents domains being the same instance, in tomcat 6 at least you have "alias" in context configuration, so you raise only one instance within two domains.

Tell over here if that is useful for you. :)

Alberto Navarro said...

And congratulations for your blog, I read it eagery.

Nestor Urquiza said...

Hi Alberto,

Thanks for your comments.

Your suggestion works for the case in which the same application is served for different domains.

However this post is about different applications being served in different URLs, one per app.

Best,
-Nestor

Joe A said...

Make sure you remove the lost information for localhost from the server.xml file. Ie:

Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"

This creates a conflict with your virtual host.

Nestor Urquiza said...

Interesting enough in my OSX I could leave the localhost there:

Followers