Basic Configuration of a Custom Tomcat Installation

If you have created a custom Tomcat installation as I explained previously, then your Tomcat configuration files will be located in the /opt/localhost/conf directory. I like to make a backup copy of the server.xml file and then make my changes in the original file. That way, if any new content comes out in the stock Tomcat server.xml file, it will be easy to diff the file to pinpoint the differences.
cd /opt/localhost/conf
cp server.xml server.xml.1
vi server.xml
My usual procedure is to navigate to the GlobalNamingResources section and then add any resources that my web applications might need. These resources can include global environment variables, DataSources for any databases that my Java web applications might need to access, JavaMail sessions to use for sending e-mails, or any other object that can be obtained via a JNDI lookup. Below is an example of an abbreviated server.xml file that I use:
<Server port="8005" shutdown="SHUTDOWN">
 
  <!-- Global JNDI resources -->
  <GlobalNamingResources>
 
    <!-- Memory database resource for authenticating Tomcat manager users -->
    <Resource name="UserDatabase" auth="Container"
      type="org.apache.catalina.UserDatabase"
      description="User database that can be updated and saved"
      factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
      pathname="conf/tomcat-users.xml" />
 
    <!-- Postgresql database resource -->
    <Resource name="jdbc/pg"
      type="javax.sql.DataSource"
      auth="container"
      driverClassName="org.postgresql.Driver"
      url="jdbc:postgresql://pgsqlserver.example.lan:5432/db1"
      username="dbuser"
      password="dbpw"
      maxActive="10"
      maxIdle="0"
      maxWait="-1"
      description="PostgreSQL Data Source"/>
 
    <!-- MySQL database resource -->
    <Resource name="jdbc/qdrupal"
      type="javax.sql.DataSource"
      auth="container"
      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://mysqlserver.example.lan/db2"
      username="dbuser"
      password="dbpw"
      maxActive="10"
      maxIdle="0"
      maxWait="-1"
      description="MySQL Data Source"/>
 
  </GlobalNamingResources>
 
  <Service name="Catalina">
 
    <!-- Non-SSL HTTP/1.1 Connector on port 8080 -->
    <Connector port="8080" maxHttpHeaderSize="8192"
      maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
      enableLookups="false" redirectPort="8443" acceptCount="100"
      connectionTimeout="20000" disableUploadTimeout="true" />
 
    <!-- Define the top level container in our container hierarchy -->
    <Engine name="Catalina" defaultHost="localhost">
 
      <!-- Define the default virtual host -->
      <Host name="localhost" appBase="webapps"
        unpackWARs="true" autoDeploy="true"
        xmlValidation="false" xmlNamespaceAware="false">
 
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
          resourceName="UserDatabase"/>
      </Host>
    </Engine>
  </Service>
</Server>
The main items I want to point out here are the two global DataSource resource definitions - one for a PostgreSQL database and another for a MySQL database. Defining these in the GlobalNamingResources section in Tomcat's server.xml file makes them accessible via JNDI to all web applications running in this instance of Tomcat, and also allows them to take advantage of connection pooling for enhanced efficiency and speed. To access the MySQL resource, for example, a web application simply needs to add a META-INF/context.xml file within its WAR file, and populate it with content similar to the following:
<?xml version='1.0' encoding='utf-8'?>
 
<Context docBase="" reloadable="true" override="true" swallowOutput="true">
  <ResourceLink name="jdbc/qdrupal" global="jdbc/qdrupal" type="javax.sql.DataSource"/>
</Context>
When the WAR file is copied into Tomcat's web application directory (/opt/localhost/webapps in our custom configuration) and unpacked, Tomcat will check to determine if a context.xml file for the web application already exists. If not, the META-INF/context.xml file will be copied to $CATALINA_BASE/conf/Catalina/[host name]/[context name].xml where [host name] is replaced with the name specified in the Host object, and [context name] is replaced with the name of the web application context. Remember that if you add more global resources and want to make them available to your application, then you will need to update the $CATALINA_BASE/conf/Catalina/[host name]/[context name].xml file and add matching ResourceLink elements for the new resources before they will be visible to your application. Finally, you will need to copy (or create a symbolic link to) any driver JARs that provide the Java classes used by GlobalNamingResources objects into the /opt/tomcat/lib directory. Keep in mind that whenever Tomcat is upgraded to a new version, these JARs will need to be copied or symlinked again.
cd /opt/tomcat/lib
ln -s /usr/share/java/mysql.jar
ln -s /usr/share/java/postgresql.jar
Now Tomcat is configured and ready to start up so we can proceed with developing and/or running Java web applications! Once the following command completes, you can point your browser to http://yourservername:8080/ to view the default page.
/etc/init.d/tomcat start

Tags: