Integration of Tomcat and Apache

xiaoxiao2021-03-06  58

1.Connector selection

Choosing the appropriate Connector for performance, the convenience of configuration is important, and there is currently JK1. X, JK2, MOD_WEBAPP three Connector can be used. JK is relatively wide. JK2 is an improvement in JK1.x, which is relatively new. But it is not easy to configure, and the related documents are relatively small. In addition, JK can support other web servers and Tomcat integration. But MOD_WEBAPP does not support. Therefore, JK1.x is currently the best choice.

2. Get JK

Use a binary version

If you compile the source code, you have a wise choice to use the binary version. You can come from http: // apache. LinuxForum. Net / Dist / Jakarta / Tomcat-Connectors / JK / BINARIES / Downloads the latest version of JK1.x.

However, unfortunately, binary versions of JK and Apache are bundled, if you use a relatively new Apache, it is often difficult to find the corresponding binary JK.

Handmade source code

For different operating systems and Apache versions, if there is no precompiled JK, you need to perform manual compilation. In addition to Linux, other operating systems generally do not install compilation environments. At this point you also need to install the compiler. A typical example is to install GCC on Solaris or install Visual C on Windows. In addition, usually the software of Open Source is generally used with AutoConf, Automake generates automatic configuration scripts and makefile, JK is no exception. The MAKE in the Unix system other than Linux is not necessarily compatible with these scripts. So if you encounter these questions, you need to install Gmake. The following is Solaris8 as an example to explain how to install the configuration compilation environment:

Make, which comes with system in Solaris8, is unable to be compatible with certain Makefile generated by Automake.

Needable package:

GCC-3.3-Sol9-sparc-local.gz

MAKE-3.80-Sol8-sparc-local.tar.gz

All required packages can be downloaded on www.sunfreeware.com.

unzip:

$ Gunzip GCC-3.3-Sol9-Sparc-local.gz

$ Gunzip Make-3.80-Sol8-Sparc-local.gz

Install with pkgadd, need root privilege:

#PKGADD -D GCC-3.3-Sol9-Sparc-Local

#pkgadd -d Make-3.80-Sol8-sparc-local After installation with PKGADD, the GCC will be installed in the / usr / local / bin directory. This is what needs to make Gmake

Become a system default make. Check the PATH environment variable to ensure that the location of the / usr / local / bin directory before the BIN of the Make comes with the makeris8:

$ Which make

/ USR / CCS / BIN / MAKE

$ Echo $ PATH

/ usr / bin: / usr / ccs / bin: / usr / local / bin

If you find that Gmake is not a system default make, you can modify the / etc / profile adjustment path. However, there is a simpler and effective way to build a symbolic connection Make to / usr / local / bin / make in the / usr / bin directory, which enables Gmake to become the system default Make:

# CD / usr / bin

# Ln -s / usr / local / bin / make make Make The same trick can solve some PERL script to the Perl interpreter executable file path reference reference reference. If a perl script has #! / Usr / local / bin / perl -w but Perl is installed on the / usr / bin / directory on the system. At this time, you need to establish a symbolic connection of / usr / bin / perl to connect Perl in / usr / local / bin /.

After compiling JK, install it into the Apache, execute:

# Make install If it is not successful, you can handle MOD_JK.so to the MODULES directory of Apache.

Configure

You should consider your integration goals before specific handoffs: Just simply put a SubContext handled by Tomcat, or a higher level of dynamic / static content integration? Whether to allow https to forward; whether to load balance? The complexity of the specific configuration will be larger than your integration goals.

Suppose our integration goals are:

1. All static resources are processed by Apache

2. All dynamic resources are processed by Tomcat

Configure Tomcat

Apache and Tomcat integrates three ways:

1. Run Tomcat in the Apache process to process dynamic content

2. Through the AJP13 protocol, Apache forwards the request for dynamic content to Tomcat.

3. Use local high-speed UNIX Domain socket, Apache forwards dynamic content request to Tomcat

Method 2 can distribute Apache and Tomcat on different machines, so that load balancing can be achieved. The mode 3 is fast, and it can be considered when Apache and Tomcat are distributed on the same machine.

This article uses the integration of Tomcat and Apache as an example 2.

In order to process the AJP13 request, Tomcat needs a service. This is achieved by defining a Connector in the Tomcat's server.xml. This Connector is enabled by default in the latest Tomcat4.1.30 version. If not enabled in Server.xml

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, check if the AJP13 service is running

Netstat -a | GREP 8009

Or check whether there is similar output on the Tomcat standard output:

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

Configuring Apache

After the Tomcat is configured, the APACHE configuration is performed. First, we need to determine the Context of the entire web application, determine that Context can determine what the user can access the web application. For example, the website is www.example.com, if the content of the web application is PURCHASE, then the user can access the app via URL http://www.example.com/purchase; if the constext of the web application is /, then the user can pass URLHTTP: //www.example.com Access the app. In general, set the context of the web application to / can be convenient for users. If there are multiple independent applications, you can consider putting them in different subcontexts. The following describes how to configure the application context, and other situations. Place the JK's configuration in a separate file and then in httpd.conf.

The first step, let Apache load mod_jk:

#MOD JK for Tomcat-Apache Integration

LoadModule JK_Module Modules / MOD_JK.SO

Map Application Context to the directory where the application static resource is stored:

# Static files in the example Webapp Are Served by Apache

Alias ​​/ /www/tomcat4.1/webapps/root/

At the same time, because now static resources are not processed by Tomcat, access to the web-INF subdirectory is required for security considerations and compliance with the JSP / Servlet specification:

# The folload line prohibits users from Directly Access Web-INF

">

ALLOWOVERRIDE NONE

Deny from all

Allow directory list functions in the integrated debug phase:

Allowoverride Fileinfo Authconfig Limit INDEXES

Options MultiViews Indexes SymlinksifownerMatch IncludeSnoexec

Next, tell Apache which dynamic content should make Tomcat processing, in order to determine the characteristics of dynamic content URL, the URL schema that appears in all servlet mapping in Web.xml should be declared in jk.conf:

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, assume that under the serverroot directory of Apache

Jklogfile specifies the location of the JK log file, if it is a relative path, assume that in the serverroot directory of Apache

JKLoglevel Specifies the level of JK logs. Equipment: Debug / Error / INFO

JKLOGSTAMPFORMAT specifies the time format in the log, and the format string syntax summary using the C function strftime ()

Tomcat and Apache integration usually need steps:

1. Which Connector decides?

2. Get the Connector, if there is no ready-made binary Connector, you need to manually compile, depending on the different operating system, you may have to set the compilation environment.

3. Configure Tomcat

4. Configure Apache to determine Context, the Context corresponds to the top-level directory of the app. Configure all dynamic content URL mode in Apache according to servlet mapping in Web.xml

5. Test

appendix

A complete configuration example:

#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 ForwarduryMPatunparsed -ForwardDirectories

# JkrequestLogformat Set The Request Format

JKRequestLogformat "% w% v% t"

# Static files in the example Webapp Are Served by Apache

Alias ​​/ /www/tomcat4.1/webapps/root/

# The folload line prohibits users from Directly Access Web-INF

">

ALLOWOVERRIDE NONE

Deny from all

Allowoverride Fileinfo Authconfig Limit INDEXES

Options MultiViews Indexes SymlinksifownerMatch IncludeSnoexec

#Jkautoalias / www/tomcat4.1/webapps/root

JKMount / BaseServlet worker1

JKMount / servlet / * worker1

JKMount / ProductUPloadServlet worker1

JKMount / Upload Worker1

JKMount / Test worker1

JKMount /*.jsp worker1

Jkmount /*.do worker1

In addition, JK also requires a WORKERS.PROPERTIES file to configure the parameters of JK itself. If you don't need to cultivate load balance

You can use the following configuration

# Define 1 real worker using ajp13

Worker.list = worker1

# Set Properties for Worker1 (AJP13)

Worker.Worker1.Type = ajp13

Worker.Worker1.host = 127.0.0.1

Worker.Worker1.port = 8009

Worker.Worker1.lbFactor = 50

Worker.Worker1.cachesize = 10

Worker.Worker1.cache_timeout = 600

Worker.worker1.socket_keepalive = 1

Worker.Worker1. Socket_timeout = 300 Be notice that the name of the Worker referenced by JKMount must be defined in Work.List.

转载请注明原文地址:https://www.9cbs.com/read-113911.html

New Post(0)