Tomcat
February 5, 2018

Tomcat behind Apache with multiple (sub)domains and web apps

Apache and Tomcat

Install mod-jk

1
apt-get install libapache2-mod-jk

Enable Apache module

1
a2enmod jk

Create workers

Create /etc/apache2/workers.properties and add the following into it

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# List workers
worker.list=worker1,worker2

# Define worker1
worker.worker1.port=8009
worker.worker1.host=domain1.tld
worker.worker1.type=ajp13

# Define worker2
worker.worker2.port=8010
worker.worker2.host=domain2.tld
worker.worker2.type=ajp13

Edit your apache2 config in /etc/apache2/apache2.conf and add this to the end Or check jk.conf and jk.load files under /etc/apache2/mods-available. If there is no jk.load create and add the following into it.

1
LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so

If there is no jk.conf create and add the following into it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# tomcat connector stuff:
JkWorkersFile /etc/apache2/workers.properties
# Where to put jk shared memory
JkShmFile     /var/log/apache2/mod_jk.shm
# Where to put jk logs
JkLogFile     /var/log/apache2/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel    info
# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]

In your /etc/apache2/sites-enabled/ directory find the vhosts you want to use tomcat and edit them all, at the end of the vhost declaration put:

1
2
3
4
5
6
<IfModule mod_jk.c>
#Everything under root goes to tomcat
JkMount  /* worker1
#html files should be served by apache
JkUnMount /*.html worker1
</IfModule>

Edit /etc/tomcat6/server.xml and make sure that the ajp connector is uncommented and add other ports:

1
2
<Connector port="8009" protocol="AJP/1.3" redirectport="8443" />
<Connector port="8010" protocol="AJP/1.3" redirectport="8443" />

To set domains to different webapps directory, add below lines under the last Host directive (You may specify an absolute pathname for this directory, or a pathname that is relative to the $CATALINA_HOME directory.)

1
2
3
4
5
6
7
<Host appBase="webapps-domain1" autodeploy="true" name="domain1.tld" unpackwars="true" xmlnamespaceaware="false" xmlvalidation="false">
  <Alias>www.domain1.tld</Alias>
</Host>

<Host appBase="/var/www/vhosts/domain2.tld/httpdocs" autodeploy="true" name="domain2.tld" unpackwars="true" xmlnamespaceaware="false" xmlvalidation="false">
  <Alias>www.domain2.tld</Alias>
</Host>

If you want the manager app to work on the domain you need to copy manager.xml. Do the same for host-manager, etc:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
mkdir /etc/tomcat6/Catalina/domain1.tld
cp /etc/tomcat6/Catalina/localhost/* /etc/tomcat6/Catalina/domain1.tld
Export WAR file for deployment first, then remove ROOT directory and copy war file for auto deployment
rm -r /var/lib/tomcat6/webapps/ROOT*
cp ~/my-app-0.1.war /var/lib/tomca6/adminapps/ROOT.war
In /var/lib/tomcat6 create a webapps-domain1 directory and give it 0775 perms and chown it to tomcat6:tomcat6
mkdir /var/lib/tomcat6/webapps-domain1
mkdir /var/lib/tomcat6/webapps-domain2
chown -R 0775 /var/lib/tomcat6/webapps-domain1
chown -R 0775 /var/lib/tomcat6/webapps-domain2

Restart tomcat:

1
/etc/init.d/tomcat6 restart

Restart apache:

1
/etc/init.d/apache2 restart

Navigate to domain1.tld and you should be all tomcatted

Realm Security

UserDatabaseRealm

%TOMCAT_HOME%\conf\server.xml

Basic

1
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" debug="0" resourceName="UserDatabase"/>

Digest

1
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" debug="0" resourceName="UserDatabase" digest="MD5"/>

%TOMCAT_HOME%\conf\tomcat-users.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
  <user username="admin" password="" roles="admin,manager"/>
  <user username="jasonb" password="9a3729201fdd376c76ded01f986481b1" roles="member"/>
</tomcat-users>

Memory Realm

%TOMCAT_HOME%\conf\server.xml

Basic

1
<Realm className="org.apache.catalina.realm.MemoryRealm"/>

JDBC Realm

%TOMCAT_HOME%\conf\server.xml

Basic

1
<Realm  className="org.apache.catalina.realm.JDBCRealm" debug="99" driverName="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/authority" connectionName="root" connectionPassword="" userTable="users" userNameCol="user_name" userCredCol="user_pass" userRoleTable="user_roles" roleNameCol="role_name" />
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
create table users (
  user_name varchar(15) not null primary key,
  user_pass varchar(15) not null
);

create table roles (
  role_name varchar(15) not null primary key
);

create table user_roles (
  user_name varchar(15) not null,
  role_name varchar(15) not null,
  primary key( user_name, role_name )
);

Application

%TOMCAT_HOME%\webapps\WebCruise\WEB-INF\web.xml

Basic

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<security-constraint>
  <web-resource-collection>
    <web-resource-name>MARCOM</web-resource-name>
    <url-pattern>/marcom/*</url-pattern>
  </web-resource-collection>
  <auth-constraint>
    <!-- %TOMCAT_HOME%/conf/tomcat-users.xml -->
    <role-name>home</role-name>
    <role-name>admin</role-name>
  </auth-constraint>
</security-constraint>
<login-config>
  <auth-method>BASIC</auth-method>
  <realm-name>MARCOM</realm-name>
</login-config>

Digest

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<security-constraint>
  <web-resource-collection>
    <web-resource-name>MARCOM</web-resource-name>
    <url-pattern>/marcom/*</url-pattern>
  </web-resource-collection>
  <auth-constraint>
    <!-- %TOMCAT_HOME%/conf/tomcat-users.xml -->
    <role-name>home</role-name>
    <role-name>admin</role-name>
  </auth-constraint>
</security-constraint>
<login-config>
  <auth-method>DIGEST</auth-method>
  <realm-name>MARCOM</realm-name>
</login-config>