Experience and skills: integration of Tomcat and Apache

Source: Internet
Author: User
Tags perl interpreter unix domain socket perl script automake
Article title: experience and skills: integration of Tomcat and Apache. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   Connector selection
Choosing the right ctor has an important impact on performance and ease of configuration. Currently, JK1 is widely used. X, JK2, and mod_webapp connectors can be used. JK is widely used. JK2 is an improvement of JK1.x and is relatively new. But it is not easy to configure, and there are few related documents. In addition, JK supports the integration of other Web servers and tomcat. Mod_webapp is not supported. Therefore, JK1.x is the best choice.
Obtain JK
  
   Use binary
If you have more than one idea about manually compiling the source code, using the binary version may be a wise choice. You can use http: // apache. Linuxforum. Net/dist/jakarta/tomcat-connectors/jk/binaries/download the latest version of JK1.x.
Unfortunately, however, the JK and Apache versions of binary are bundled together. if you use newer Apache (such as httpd-2.0.29, it is often difficult to find the corresponding binary version JK.
  
   Manually compile source code
For different operating systems and Apache versions, manual compilation is required if pre-compiled JK is not available. Except for Linux, compilation environments are generally not installed on other operating systems. You also need to install the compiler. A typical example is to install GCC on Solaris or install Visual C ++ on Windows. In addition, Open Source software generally uses GNU autoconf and automake to generate automatic configuration scripts and makefiles, and JK is no exception. Except for Linux, make in Unix systems is not necessarily compatible with these scripts. Therefore, if you encounter these problems, you need to install gmake. The following uses Solaris8 as an example to describe how to install and configure the compiling environment:
In Solaris8, make is not compatible with some makefiles generated by automake.
Required software package:
Gcc-3.3-sol9-sparc-local.gz
Make-3.80-sol8-sparc-local.tar.gz
All required software packages can be downloaded from www.sunfreeware.com.
Decompress:
Code:
$ Gunzip gcc-3.3-sol9-sparc-local.gz
$ Gunzip make-3.80-sol8-sparc-local.gz
  
To use pkgadd for installation, the root permission is required:
Code:
# Pkgadd-d gcc-3.3-sol9-sparc-local
# Pkgadd-d make-3.80-sol8-sparc-local
After pkgadd is used for installation, gcc and make are usually installed in the/usr/local/bin directory. This is to make gmake
Make is the default make. Check the PATH environment variable to make sure that the/usr/local/bin directory is located before the bin of make in Solaris8:
Code:
$ Which make
/Usr/ccs/bin/make
$ Echo $ PATH
/Usr/bin:/usr/ccs/bin:/usr/local/bin
  
If you find that gmake is not the default make, you can modify/etc/profile to adjust the PATH. However, there is a simpler and more effective way to connect make to the/usr/local/bin/make symbol in the/usr/bin directory, in this way, gmake becomes the default make of the system:
Code:
# Cd/usr/bin
# Ln-s/usr/local/bin/make
The same technique can solve some problems that Perl scripts reference the executable file paths of the perl interpreter. If the header information of a perl script is #! /Usr/local/bin/perl-w, but Perl is installed in the/usr/bin/directory on the system. In this case
/Usr/local/bin/creates a symbolic connection to/usr/bin/perl.
After compiling JK, install it in apache and execute:
Code:
# Make install
If it fails, copy mod_jk.so to the modules directory of apache.
Configuration
Before specific configuration, you should consider the purpose of integration: simply to process a subcontext by tomcat, or to integrate dynamic/static content at a higher level? Whether HTTPS forwarding is allowed; do you want to balance the load? The complexity of the specific configuration varies greatly depending on your integration goals.
  
Assume that our integration goal is:
1. all static resources are processed by Apache
2. Tomcat processes all dynamic resources.
Configure Tomcat
Apache and tomcat can be integrated in three ways:
1. run tomcat in the apache process to process dynamic content
2. through the ajp13 protocol, apache forwards dynamic content requests to tomcat
3. using a local high-speed Unix domain socket, apache forwards dynamic content requests to tomcat
  
Method 2: apache and tomcat can be distributed on different machines to achieve load balancing. Method 3 is faster. you can use it when apache and tomcat are distributed on the same machine.
This article uses Method 2 as an example to describe the integration of tomcat and apache.
To process ajp13 requests, tomcat needs a service. This is achieved by defining a ctor in tomcat server. xml. In the latest tomcat4.1.30, this ctor is enabled by default. If you do not enable adding in server. xml
Code:
     Port = "8009" minProcessors = "5" maxProcessors = "75"
EnableLookups = "true" redirectPort = "8443"
AcceptCount = "10" debug = "0" connectionTimeout = "20000"
UseURIValidationHack = "false"
ProtocolHandlerClassName = "org. apache. jk. server. JkCoyoteHandler"/>
  
Start tomcat and check whether the ajp13 service is running
Netstat-a | grep 8009
Or check whether there is similar output on the tomcat Standard output:
Code:
Info: JK2: ajp13 listening on/0.0.0.0: 8009
2004-5-22 14:50:35 org. apache. jk. server. JkMain start
Info: Jk running ID = 0 time = 20/200 config = D: \ Tomcat4.1 \ conf \ jk2.properties
  
   Configure Apache
After configuring Tomcat, configure Apache. First, we need to determine the context of the entire web application and determine the URL through which the user can access the web application. For example, the website is www.example.com, if the context of the web application is purchase, then the user can use URL http://www.example.com/purchase the application if the web application contextis /, then the user can access the application through the URL http://www.example.com. Generally, setting the context of a web application to/is convenient for users. If multiple independent applications exist, you can put them in different sub-context. The following describes how to configure the application context when it is set to/. Other cases are similar. Put jk configuration in a separate file and then in httpd. Include the file in conf.
Step 1: Let apache load mod_jk:
Code:
# Mod jk for tomcat-apache integration
LoadModule jk_module modules/mod_jk.so
  
Map the application context to the directory where the static resources of the application are stored:
Code:
# Static files in the examples webapp are served by apache
Alias // www/tomcat4. 1/webapps/ROOT/
  
At the same time, because the current static resources are not processed by tomcat, so out of security considerations and comply with JSP/Servlet specifications need to prohibit access to the WEB-INF subdirectory:
Code:
# The following line prohibits users from directly access WEB-INF
  
AllowOverride None
Deny from all
  
  
In the integration debugging phase, you can consider allowing the directory list function:
Code:
  
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
  
  
The next step is to tell apache which dynamic content should be processed by tomcat. in order to determine the characteristics of the dynamic content URL, the web. the URL pattern of all servlet mappings in xml is in jk. conf statement:
Code:
JkMount/BaseServlet worker1
JkMount/Servlet/* worker1
JkMount/ProductUploadServlet worker1
JkMount/upload worker1
JkMount/test worker1
JkMount/*. jsp worker1
JkMount/*. do worker1
  
Other configurations that may need to be modified
Configuration item description
JkWorkersFile specifies the location of jk workers. properties. if it is a relative path, it is assumed that it is in the apache ServerRoot directory.
JkLogFile specifies the location of the jk log file. if it is a relative path, it is assumed that it is in the apache ServerRoot directory.
JkLogLevel valid options for specifying the jk log level are: debug/error/info
JkLogStampFormat specifies the time format in log. use the string syntax in the strftime () format of the c function.
  
To sum up Tomcat and Apache integration, the following steps are usually required:
1. determine which ctor to use
2. obtain connector. if there is no binary connector available, you need to manually compile the connector. you may need to set the compiling environment according to the operating system.
3. configure tomcat
4. configure apache, determine the context, and map the context to the top-level Directory of the application. Configure the url mode of all dynamic content in apache according to the servlet ing in web. xml.
5. test
  
   Appendix
A complete configuration example:
Code:
# Mod jk for tomcat-apache integration
LoadModule jk_module modules/mod_jk.so
  
JkWorkersFile conf/workers. properties
# Where to put jk logs
  
JkLogFile logs/mod_jk. Log
# Set the jk log level [debug/error/info]
JkLogLevel debug
  
# Select the log format
JkLogStampFormat "[% Y-% m-% d % H: % M: % S]"
  
# JkOptions indicate to send ssl key size,
JkOptions + ForwardKeySize + ForwardURICompat + ForwardURICompatUnparsed-ForwardDirectories
  
# JkRequestLogFormat set the request format
JkRequestLogFormat "% w % V % T"
  
# Static
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.