Integration Tomcat into Apache Httpd

Setting Up Apache httpd
Suppose you want httpd to map all references to the /loch-ness directory over to Tomcat for serving. First, you must ensure that you can use mod_proxy in httpd; if you’re already using mod_proxy, you can skip the next paragraph or two. If you have mod_proxy compiled as a shared object (usually, mod_proxy comes compiled and ready to use with binary releases of httpd), it may mean simply placing these lines in the appropriate places in your httpd.conf file if they’re not already there:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

Of course, you must ensure that the pathname in your configuration file is correct for wherever your operating system normally installs loadable Apache modules. If this causes httpd to log errors, you may need to recompile it from source, configuring the build to include mod_proxy, mod_proxy_ajp, and mod_proxy_http. But, mod_proxy and its associated modules are so commonly used that they are built with Apache httpd and available by default in most Linux distributions.
If your httpd does not have mod_proxy, you can either rebuild httpd from source code to include it (this is not difficult to do) or try just building and installing the module you’re missing. To build just mod_proxy from source and add it to your existing Apache httpd’s loadable modules, see the sidebar, “Compiling Apache Modules.”
To build Apache httpd from source, including mod_proxy, first download the latest stable Apache httpd source distribution from http://httpd.apache.org and unpack it _disibledevent=>
To see the configuration switches you could use and what they do, try this:
# ./configure --help
Then, build httpd and install it:
# make
And if that was successful:
# make install

At that point, httpd should be installed so that you can use mod_proxy with both the HTTP and AJP protocols.
Then, you need to pick a port number (such as 8080) to which Apache httpd will connect to Tomcat. Setting up the httpd end is straightforward—add these additional lines to your httpd.conf file, below the LoadModule lines we showed earlier for proxying over HTTP:
ProxyPass /loch-ness http://tomcathost:8080/loch-ness
ProxyPassReverse /loch-ness
http://tomcathost:8080/loch-ness
ProxyVia _disibledevent=>Compiling Apache Modules
If you need an Apache httpd module (such as mod_proxy) compiled as an external module, adding to your existing modules, you may be able to use the Apache Extension tool apxs to build and install it. Change directory into the Apache httpd source directory where mod_proxy.c and the proxy*.c files are stored, and issue the command:

# apxs -i -a -n proxy -c *.c

The output should look something like this:
/usr/lib64/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -falign-
functions=0 -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -pthread -I/opt/httpd/include
-I/usr/include/apr-1 -c -o ajp_header.lo ajp_header.c && touch ajp_header.slo
/usr/lib64/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -falignfunctions=
0 -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -pthread -I/opt/httpd/include
-I/usr/include/apr-1 -c -o ajp_link.lo ajp_link.c && touch ajp_link.slo
[many lines of similar build output removed]
/usr/lib64/apr-1/build/libtool --silent --mode=link gcc -o ajp_header.la -rpath
/opt/httpd/modules -module -avoid-version proxy_util.lo mod_proxy_http.lo
mod_proxy_ftp.lo mod_proxy_connect.lo mod_proxy.lo mod_proxy_balancer.lo mod_
proxy_ajp.lo ajp_msg.lo ajp_link.lo ajp_header.lo
/opt/httpd/build/instdso.sh SH_LIBTOOL=’/usr/lib64/apr-1/build/libtool’ ajp_
header.la /opt/httpd/modules
usr/lib64/apr-1/build/libtool --mode+install cp ajp_header.la/opt/httpd/modules
cp .libs/ajp_header.so /opt/httpd/modules/ajp_header.so
cp .libs/ajp_header.lai /opt/httpd/modules/ajp_header.la
cp .libs/ajp_header.a /opt/httpd/modules/ajp_header.a
chmod 644 /opt/httpd/modules/ajp_header.a
ranlib /opt/httpd/modules/ajp_header.a
PATH="$PATH:/sbin" ldconfig -n /opt/httpd/modules
--------------------------------------------------------------------------------------------------
Libraries have been installed in:
/opt/httpd/modules

If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the ‘-LLIBDIR’
flag during linking and do at least _disibledevent=>http://tomcat.apache.org/download-connectors.cgi. They make some binary releases available, but they have not worked for us. If they don’t work, you’ll need to compile your own mod_jk binaries. Also, if/when you upgrade httpd to a newer version, you’ll probably need to compile a matching mod_jk for the new httpd.
Compiling mod_jk

Here’s how to compile mod_jk for your httpd server. Download a new source code release of tomcat-connectors from http://tomcat.apache.org/download-connectors.cgi. Unpack the archive and read the BUILD.txt file to see any recent information about building mod_jk:

# gunzip tomcat-connectors-1.2.20-src.tar.gz
# tar xvf tomcat-connectors-1.2.20-src.tar
# cd tomcat-connectors-1.2.20-src
# more BUILD.txt


You may need to install the Apache httpd development package (for example, it is named httpd-devel _disibledevent=>[lots of configuration output removed]
# make
# make install

That should build and install mod_jk. Before going any further, look toward the end of the make install output and note where it installed the mod_jk.so file. Next, edit your Apache httpd’s httpd.conf andad dsome configuration for mod_jk.
Add these lines to start with (replacing paths and the webapp name to match those
in your setup):
# Load mod_jk module
LoadModule jk_module /usr/lib64/httpd/modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile /etc/httpd/conf/workers.properties
# Where to put jk logs
JkLogFile /var/log/httpd/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send servlet for context /docs to worker named tomcat1
JkMount /docs/* tomcat1

Make sure that the LoadModule directive has the full path to the place the mod_jk.so file was installed(again, look near the end of your make install output to see where it went).
Next, create a workers.properties file at the path you specified when you added the mod_jk configuration to the httpd.conf file. If you just have _disibledevent=>worker.tomcat1.type=ajp13
worker.tomcat1.host=localhost
worker.tomcat1.port=8009
# worker "tomcat1" uses up to 150 sockets, which will stay no more than
# 10 minutes in the connection pool.
worker.tomcat1.connection_pool_size=150
worker.tomcat1.connection_pool_timeout=600
# worker "tomcat1" will ask the operating system to send a KEEP-ALIVE
# signal _disibledevent=># mount can be used as an alternative to the JkMount directive
#worker.tomcat1.mount=/docs /docs/*

Configuration default page:
196 # DirectoryIndex: sets the file that Apache will serve if a directory
197 # is requested.
198 #
199
200 DirectoryIndex index.jsp
201
202
203 DirectoryIndex index.html
204

Configure Virtual host in Apache Httpd:
1027 Include conf/mod_jk.conf
1028 LoadModule deflate_module modules/mod_deflate.so
1029 LoadModule headers_module modules/mod_headers.so
1030
1031
1032 NameVirtualHost *
1033
1034
1035 DocumentRoot /data/www/html
1036 ServerName rtq.morningstar.com
1037 JkMount /*.jsp ajp13
1038
1039 Options FollowSymLinks
1040 AllowOverride None
1041 Order allow,deny
1042 Allow from all
1043

1044

1045
1046
1047 DocumentRoot /data/www/html
1048 ServerName quotespeed.morningstar.com
1049 JkMount /*.jsp ajp13
1050
1051 Options FollowSymLinks
1052 AllowOverride None
1053 Order allow,deny
1054 Allow from all
1055

1056

1057
1058
1059 DocumentRoot /data/www/admin
1060 ServerName qsadmin.morningstar.com
1061 # JkMount /*.jsp ajp13
1062 # JkMount /*.action ajp13
1063 # JkMount /*.css ajp13
1064 # JkMount /*.js ajp13
1065 JkMount /*.* ajp13
1066
1067 Options FollowSymLinks
1068 AllowOverride None
1069 Order allow,deny
1070 Allow from all
1071

1072

Tags: 

延伸阅读

最新评论

发表评论