Tuesday, 24 January 2012

Implementing virtual host in tomcat

Implementing virtual host in tomcat
Implementing Virtual Host in Tomcat

Say you have Java web application and you want to have “myApp-local.zohaib.umair.com” as a URL to access in tomcat. If you open up conf/server.xml you will notice that tomcat comes with default Host called “localhost” with the appBase “webapps” where you install web applications for default localhost host.

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
And you will also find that URL for the default application becomes:
And for the other sub-context deployed on this host becomes:

1) conf/server.xml configuration:


<Host appBase="vhosts/myApp-local.zohaib.umair.com"
name="myApp-local.zohaib.umair.com" unpackWARs="true" autoDeploy="true">

<!—If you want to log -->
      <Logger className="org.apache.catalina.logger.FileLogger"
            prefix="zohaib_log" suffix=".txt" timestamp="true" verbosity="4" />

      <Alias>myApp-local.zohaib.umair.com</Alias>
      <Alias>www.anotherAlias.net.au</Alias>
      <Valve className="org.apache.catalina.valves.AccessLogValve"
      directory="logs" prefix="myApp-local.zohaib.umair.com_access_log."
      suffix=".txt" pattern="common" resolveHosts="false" />

</Host>

Under ${tomcat-home} folder you need to create a folder vhosts/myApp-local.zohaib.umair.com as specified in above server.xml configuration. Here you actually place your ROOT.war.
Note ROOT.war is actually tomcat's convention. And conceptually you are actually creating a host and tomcat needs to know which is a default root war for that host. Therefore you must follow that convention.

In above configuration you are telling tomcat:That your defualt (ROOT.war) can be accessible via myApp-local.zohaib.umair.com or www.anotherAlias.net.au. Your default war (i.e ROOT.war) is located under ${tomcat}/vhosts/myApp-local.zohaib.umair.com. It could be war or exploded folder. And You want to log messages in zohaib_log.txt file with timestamp.


2) ${tomcat-home}/conf/Catalina/myApp-local.zohaib.umair.com/ROOT.xml


Under ${tomcat-home}/conf/Catalina you have to create folder myApp-local.zohaib.umair.com and in that folder you need to place ROOT.xml. Your ROOT.xml should be similar as follows. Main thing is you need to define CONTEXT:


<?xml version='1.0' encoding='utf-8'?>

<Context allowLinking="true" displayName="MyApp" docBase="ROOT.war"
     path="" workDir="work/Catalina/myApp-local.zohaib.umair.com/_">

<Resource name="myOracle" auth="Container" type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@servername:1522:5MyDatabase"
username="zohaib" password="zohaib" removeAbandoned="true"
removeAbandonedTimeout="60" logAbandoned="true" maxActive="150"
maxIdle="20" maxWait="5000" validationQuery="select 1 from dual"
testOnBorrow="true" testOnReturn="true" testWhileIdle="true"
timeBetweenEvictionRunsMillis="1800000" numTestsPerEvictionRun="3"
minEvictableIdleTimeMillis="1800000" />

</Context>

3) Windows hosts file


If you are on windows and if you are running locally then you need to bind your alia to your IP in
C:\Windows\System32\drivers\etc\hosts
10.3.22.2                                         myApp-local.zohaib.umair.com
10.3.22.2                                         www.anotherAlias.net.au

Assume 10.3.22.2 is your IP and if you have deployed war you should able to access your app from above URLs

No comments:

Post a Comment

Zohaib: