|
| 1 | +Title: Tomcat 7: Apache HTTPd 2.2 integration with virtual-hosts |
| 2 | +Tags: tomcat,apache,apache-mod_rewrite |
| 3 | + |
| 4 | +First install and enable the mod_jk module for Apache |
| 5 | + |
| 6 | + apt-get install libapache2-mod-jk |
| 7 | + a2enmod jk |
| 8 | + |
| 9 | +Then create a workers file. A worker is a process that will connect you to a tomcat instance. We're creating the workers.properties file at /etc/apache2/workers.properties. |
| 10 | + |
| 11 | + worker.list=worker1 |
| 12 | + worker.worker1.type=ajp13 |
| 13 | + worker.worker1.host=localhost |
| 14 | + worker.worker1.port=8009 |
| 15 | + |
| 16 | +We're giving it a name (will be used later), saying we're using the ajp13 connector to connect to tomcat 6 and above instances, saying it's on localhost and saying we're listening on port 8009 (we'll set tomcat listening on this port in a little while.) |
| 17 | + |
| 18 | +Now in your apache.conf file, add: |
| 19 | + |
| 20 | + JkWorkersFile /etc/apache2/workers.properties |
| 21 | + JkShmFile /var/log/apache2/mod_jk.shm |
| 22 | + JkLogFile /var/log/apache2/mod_jk.log |
| 23 | + JkLogLevel info |
| 24 | + JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " |
| 25 | + |
| 26 | +Here we point to our workers file, setup some file locations to be next to each other and set some logging information. |
| 27 | + |
| 28 | +Finally edit the /etc/tomcat7/server.xml to accept these ajp13 connections. Uncommend this line: |
| 29 | + |
| 30 | + <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> |
| 31 | + |
| 32 | +The only thing left is to setup your virual host file. Here's an example of a virtual host file you should have in /etc/apache2/sites-available/blar |
| 33 | + |
| 34 | + <VirtualHost *:80> |
| 35 | + ... |
| 36 | + |
| 37 | + JkMount / worker1 |
| 38 | + JkMount /* worker1 |
| 39 | + |
| 40 | + RewriteEngine on |
| 41 | + RewriteRule ^/(.*)$ /YOUR_DEPLOYMENT_NAME/$1 [L,PT] |
| 42 | + |
| 43 | + ... |
| 44 | + </VirtualHost> |
| 45 | + |
| 46 | +Note we're pointing all the files that hit the root of our virual host to our worker via the JkMount command. |
| 47 | + |
| 48 | +Generally, our tomcat servlets or jsp pages are prefixes with the name of the deployment file. Hello.war would be prefixed with Hello/. To get around this the RewriteRule gets around this by rewriting anything going to the root by transparently adding the deployment name. |
0 commit comments