Connecting Apache 1.3.x and Tomcat on Unix servers
You will want to configure Apache to talk to Tomcat for a few reasons.
- Take advantage of Apache's virtual hosts or rewrite rules
- Use Apache's SSL connections (https)
- To execute non Java code, like php
- To server static content (like images) faster
Here are instructions how to set this up.
1.) Build or download mod_jk.so
Running Magnolia with Apache 1.3.x^Source code and binaries available from tomcat.apache.org.
Put it in /usr/libexec/httpd.
2.) Modify your tomcat/conf/server.xml file to enable the connector(s):
<!-- Define an AJP 1.3 Connectors -->
<Connector className="org.apache.ajp.tomcat4.Ajp13Connector"
port="8009" minProcessors="5" maxProcessors="75"
acceptCount="10" debug="0"/>
<Connector className="org.apache.ajp.tomcat4.Ajp13Connector"
port="8010" minProcessors="5" maxProcessors="75"
acceptCount="10" debug="0"/>
If the above doesn't work (didn't work with Magnolia 2.1.5) try:
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3"/>
<Connector port="8010" protocol="AJP/1.3"/>
You will need a connection for each Tomcat worker. The port you specify here will be used in the workers.properties file.
Read more about Tomcat's connector values
3.) Create the file 'workers.properties'
The Tomcat docs call a worker "a Tomcat instance that is waiting to execute servlets or any other content on behalf of some web server." I believe in Magnolia 1 you had 2 workers, and in Magnolia 2 you only have 1.
If you wanted 2 workers, it will look something like this:
#
# workers.properties
#
# In Unix, we use forward slashes:
ps=/
# list the workers by name
worker.list=site1,site2
# ------------------------
# site1
# ------------------------
worker.site1.port=8009
worker.site1.host=localhost
worker.site1.type=ajp13
# ------------------------
# site2
# ------------------------
worker.site2.port=8010
worker.site2.host=localhost
worker.site2.type=ajp13
Save it to anywhere, like: /etc/tomcat/workers.properties (you need to specify this path in your apache config file for JkWorkersFile).
The port number for each worker was specified in the server.xml file.
The host should be localhost unless you want that worker to connect to another server.
The worker names (in this example they are "site1" and "site2") can be any alphanumeric. It doesn't matter what the name is. It just has to be the same as the name used in the httpd.conf file for the JkMount.
Running Magnolia with Apache 1.3.x^Read more about workers.properites
4.) Configure Apache to use mod_jk.
Edit /etc/httpd/httpd.conf (or /etc/apache/httpd.conf) and add:
#
# Tomcat connector
#
LoadModule jk_module libexec/httpd/mod_jk.so
AddModule mod_jk.c
JkWorkersFile /etc/tomcat/workers.properties
JkLogFile /var/log/httpd/mod_jk/mod_jk.log
JkLogLevel emerg
"AddModule mod_jk.c" may not be needed. If you run "apachectl configtest" it will give you a warning if you don't need it.
Be sure to set JkWorkersFile to the path of your workers file.
Then you need to add some mounts. Since you are using Apache, you probably don't want to send everything to Tomcat. So you can either mount a file extension:
Or any url that begins with the name of your context:
This should send both /magnoliaAuthor and /magnoliaPublic to Tomcat but leave everything not beginning with "magnolia" to Apache.
Or you can use virtual hosts to determine whether or not to send requests to Tomcat:
<VirtualHost x.x.x.x:80>
ServerName site1.com
JkMount /* site1
</VirtualHost>
<VirtualHost x.x.x.x:80>
ServerName site2.com
JkMount /* site2
</VirtualHost>
or
NameVirtualHost *
<VirtualHost *>
ServerName url1.com
JkMount /* site1
</VirtualHost>
<VirtualHost *>
ServerName url2.com
JkMount /* site2
</VirtualHost>
<VirtualHost *>
ServerName url3.com
# NO JkMount
DocumentRoot "/Library/WebServer/Documents"
</VirtualHost>
So url1.com will go to Tomcat worker "site1", url2.com will go to Tomcat worker "site2" (which isn't really needed for Magnolia 2.1), and url3.com will be served by Apache. For example
url1.com/
loads the Tomcat page
url1.com/magnoliaAuthor
will load the author context, and
url1.com/magnoliaPublic
will load the public context.
There is no behavior difference between url1.com and url2.com. If you wanted url2.com to open a completely different context, you will have to set up Tomcat virtual hosts.
You could get even tricker by specifying a valid DocumentRoot in each virtual host and a more detailed JkMount than just "/", like "/magnolia". (Haven't gotten this to work yet)
Finally, you would enable SSL with something like this:
<IfModule mod_ssl.c>
# Some MIME-types for downloading Certificates and CRLs
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
# inintial Directives for SSL
SSLProtocol all -SSLv3
SSLPassPhraseDialog builtin
SSLSessionCache dbm:/var/run/ssl_scache
SSLSessionCacheTimeout 300
SSLMutex file:/var/run/ssl_mutex
SSLRandomSeed startup builtin
SSLLog /var/log/httpd/ssl_engine_log
SSLLogLevel info
<VirtualHost *:443>
ServerName url1.com
JkMount /* site1
ErrorLog /var/log/httpd/error_log
TransferLog /var/log/httpd/access_log
# SSL Engine Switch:
# Enable/Disable SSL for this virtual host.
SSLEngine on
SSLProtocol all -SSLv3
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
# Path to your certificates and private key
SSLCertificateFile /etc/httpd/ssl.key/server.crt
SSLCertificateKeyFile /etc/httpd/ssl.key/server.key
<Files ~ "\.(cgi|shtml|phtml|php3?)$">
SSLOptions +StdEnvVars
</Files>
<Directory "/Users/WebServer/CGI-Executables">
SSLOptions +StdEnvVars
</Directory>
# correction for browsers that don't always handle SSL connections well
SetEnvIf User-Agent ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
# Per-Server Logging:
# The home of a custom SSL log file. Use this when you want a
# compact non-error SSL logfile on a virtual host basis.
CustomLog /var/log/httpd/ssl_request_log \
"%t %h %\{SSL_PROTOCOL}x %\{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>
</IfModule>
The above ssl setup has not portion not tested with Magnolia, and there is a lot of ssl setup that is not convered here.
5 Restart apache and Tomcat
Execute in terminal (paths are for Mac OS X):
export JAVA_HOME="/Library/Java/Home"
export CATALINA_HOME="/Library/WebServer/tomcat/"
sh -c "\"$CATALINA_HOME\""/bin/shutdown.sh www
sh -c "\"$CATALINA_HOME\""/bin/startup.sh www
apachectl restart
There are many docs on connectors. Here are some:
http://tomcat.apache.org/connectors-doc/
http://tomcat.apache.org/tomcat-5.5-doc/connectors.html
http://tomcat.apache.org/tomcat-4.1-doc/config/jk.html
http://tomcat.apache.org/tomcat-4.1-doc/jk2/jk/aphowto.html
http://tomcat.apache.org/tomcat-3.3-doc/mod_jk-howto.html
http://www.ex-parrot.com/~pete/tomcat-vhost.html
Page first written by Michael Robertson, heavily modified by James Reynolds
In its previous incarnation on JspWiki, this page was last edited on Feb 9, 2007 10:59:43 AM by 155.97.16.124
Other known authors include :
- 155.97.16.43
- Miro
- 61.145.233.118
- 212.163.159.14