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 on your local hard drive. Then, change directory into the httpd source tree and build it:

# ./configure --prefix=/opt/httpd --enable-proxy --enable-proxy-http --enable-proxy-ajp

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 On

Or, instead, add these additional lines for proxying over AJP:


ProxyPass /loch-ness ajp://tomcathost:8009/loch-ness
ProxyPassReverse /loch-ness ajp://tomcathost:8009/loch-ness
ProxyVia On

 

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 one of the following:
– add LIBDIR to the ‘LD_LIBRARY_PATH’ environment variable during execution
– add LIBDIR to the ‘LD_RUN_PATH’ environment variable during linking
– use the ‘-Wl,--rpath -Wl,LIBDIR’ linker flag
– have your system administrator add LIBDIR to ‘/etc/ld.so.conf’
See any operating system documentation about shared libraries for more
information, such as the ld(1) and ld.so(8) manual pages.
------------------------------------------------------------------
chmod 755 /opt/httpd/modules/ajp_header.so
[activating module ‘proxy’ in /opt/httpd/conf/httpd.conf]


If you get this output, the module has been compiled successfully and installed where your version of httpd can find it.

See Apache: The Definitive Guide by Ben Laurie and Peter Laurie (O’Reilly) for more details on Apache modules.

 

The first thing you should know about setting up httpd and mod_jk is that the mod_jk module must be compiled either against your copy of httpd or against a copy of httpd that is the same version as yours. If the version numbers don’t match between httpd and a module it’s trying to load, you’ll get an error message and the module won’t load.

Using binary releases

If you already have Apache 2.2.x installed and running, see if you can find a release binary of the Tomcat connectors code (a separate download that contains the code for mod_jk) that will work with it. Look to see what binary release versions are available for download from 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 on Fedora) to build mod_jk because you will need the apxs or apxs2 command to build it. On Fedora Linux, try running the following command if you cannot find the apxs or apxs2 command:


# yum install httpd-devel

On other operating system distributions, you will need to find out how to install apxs, or you will need to install an Apache httpd package that comes with one. Then, follow the directions for building mod_jk. Here’s how that looked on Fedora Linux as of tomcat-connectors-1.2.20:


# cd native
# ./configure --with-apxs=/usr/sbin/apxs
[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 one Tomcat instance on the same machine that runs Apache httpd, try this configuration for your workers.

properties:

worker.list=tomcat1
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 on the connection.
worker.tomcat1.socket_keepalive=1
# 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值