Configuring Tomcat5 and Apache2 with Virtual Hosts using mod_jk - Page 2
Installing and configuring mod_jk
In order to make the connection between Tomcat and Apache, we will need to download and install mod_jk connector. You will find that the Apache documentation recommends that you install the packaged version of mod_jk if it is available for your particular Linux distribution. Many outdated resources recommend installing the mod_jk2 connector, but I have found that it has been deprecated and although mod_jk was developed before mod_jk2, it is still fully supported and is very stable.
Mike Millson gave some good reasoning behind using mod_jk for connecting Tomcat to Apache for Red Hat here: Integrating Tomcat and Apache on Red Hat Linux.
Here is what he had to say:
"At this point, Apache and Tomcat should be working separately in standalone mode. You can run Tomcat in standalone mode as an alternative to Apache. In fact, in some cases, it is said that Tomcat standalone is faster than serving static content from Apache and dynamic content from Tomcat. However, there are the following compelling reasons to use Apache as the front end: 1. You can use Apache to buffer slow connections. Tomcat uses java.io, which uses a thread for each request, so Tomcat can run out of connections as the number of slow requests grows. This could be an issue if your application supports a large number of dial-up users. 2. You can use a connector such as mod_jk to load balance amongst several Tomcat instances. 3. You can take advantage of Apache features such as cgi and PHP. 4. You can take advantage of Apache modules such as mod_rewrite, mod_headers, and mod_expire. 5. You can isolate virtual hosts in their own Tomcat instances. The increased functionality obtained by using Apache on the front end can outweigh the effort required to install and configure a connector." |
At the time of this writing there were no mod_jk packages available from Dedian.org so we will need to build the package from source which will assure it is compiled for you particular installation anyways. I consider this the safer route although it requires more work.
- I chose to download the current source from the Apache archives: http://archive.apache.org/dist/jakarta/tomcat-connectors/jk/source/jk-1.2.15/. Download the jakarta-tomcat-connectors-1.2.15-src.tar.gz file to your /usr/src/ directory.
- Change to the /usr/src directory.
cd /usr/src |
- Next, extract the contents to create the /usr/src/jakarta-tomcat-connectors-1.2.15-src directory.
tar xvzf jakarta-tomcat-connectors-1.2.15-src.tar.gz |
- Change to the /usr/src/jakarta-tomcat-connectors-1.2.15-src/jk/native directory.
cd jakarta-tomcat-connectors-1.2.15-src/jk/native |
- Now you are ready to create the custom configure file for your system. Execute the following:
./buildconf.sh |
This will create a configure file in the /usr/src/jakarta-tomcat-connectors-1.2.15-src/jk/native directory.
- Execute the following command in order to configure mod_jk for your system.
Important note: You will need to have apxs2 (APache eXtension tool) installed and configured with Apache. If you do not have it, as was my case, you can download and install the apache2-threaded-dev package (which replaced the former apache-dev package) from www.debian.org. At the time of this writing, the Debian package archive at www.debian.org was down and they referred me to their temporary site until they resolved their issues pdo.debian.net. I found the apache2-threaded-dev package and was able to install it successfully.
Be sure to include the correct location apxs2 on your system in the path of the command.
./configure --with-apxs=/usr/bin/apxs2 |
- Now build the mod_jk with the following:
make |
- Finally, if you were successful with the previous commands, copy the newly created mod_jk.so to your Apache2 modules directory. My modules were located at /usr/lib/apache2/modules.
cd apache-2.0 cp /usr/src/jakarta-tomcat-connectors-1.2.15-src/jk/native/apache-2.0/mod_jk.so /usr/lib/apache2/modules |
You now are ready to move to the next stage which is to begin configuring Apache and Tomcat. You can find more information about the mod_jk connector at http://tomcat.apache.org/connectors-doc/howto/apache.html.