JK2 connector using TCP Sockets
The advantage to using TCP sockets to connect Apache and Tomcat is that both servers can be configured and run independently. Another advantage to this method is that the configuration is fairly straight forward. The disadvantage is that Tomcat must be started separately and be kept running.Please note the following convention that is used in this section. Substitute the appropriate string for your configuration. Note that those that I meant for substitution are in bold font.$APACHE_HOME – This is the full path to where Apache is installed. In our testing it was ‘c:/Apache2’.
$TOMCAT_HOME –This is the full path to where Tomcat is installed. In our testing it was ‘d:/Tomcat4.1’.
$CATALINA_HOME –This is the same as $TOMCAT_HOME.For example if I write, ‘$APACHE_HOME/conf/httpd.conf’. Please substitute it with ‘c:/Apache2/conf/httpd.conf’.Step one–Preliminary testing
First install and configure both Apache and Tomcat, as discussed in another document. The following conventions are used. Before beginning, ensure that both Apache and Tomcat are running and configured properly. Test the examples from Tomcat and try to browse the on-line documents in Apache. If you can run both concurrently you are ready to connect the two. I do not discuss here how to perform the test as it depends on your configuration and thus outside the scope of this document. You should refer to your document on configuring Apache and configuring Tomcat for this.Step two–Getting the connector
The second thing to do is to download the mod_jk2 connector DSO. In Windows this will be a dll. The connector will most likely come as a file called, ‘mod_jk2-2.0.24.dll’. Rename it to ‘mod_jk2.dll’. This makes it easier to deal with. Save the file in the modules directory. This should be, ‘$APACHE_HOME/modules’.Step three—Editing the configuration files
There are four files that must be edited for the connector. They are; the httpd.conf file, the jk2.properties file, the server.xml file, and the workers2.properties.The ‘httpd.conf’ file
The httpd.conf file should be at ‘$APACHE_HOME/conf/httpd.conf’. This file contains the main configuration data for Apache. We will use this file to tell Apache to load the mod_jk2 DSO module. Locate the portion of the where other modules are being loaded. To be safe we enclose it inside of an ‘if’ statement. That way if we accidentally try to load the module twice we won’t get into trouble.  <IfModule !mod_jk2.c>
LoadModule jk2_module modules/mod_jk2.dll
</IfModule>    For more information about the httpd.conf file refer to the documentation included in the Apache distribution or the book available in our library.The ‘jk2.properties’ file
The jk2.properties file should be in the directory ‘$TOMCAT_HOME/conf’. This file contains all of the JK2 configuration information. Here we will set up the shared memory directive. This directive is necessary for Apache and Tomcat to communicate. A good place to have the file is the ‘$APACHE_HOME/logs’ directory. The file can be named anything but the convention is ‘jk2.shm’.
TODO: What to do in case Tomcat and Apache reside on different machines.
  #Shared memory directive
shm.file=c:/Apache2/logs/jk2.shm    This default is fine and no other directives need to go in this file.Unfortunately there is sparse documentation for the jk2.properties. Documentation for this file is available in the tomcat-connectors section of the Apache Software Group’s Jakarta website. This documentation is incomplete and contains many mistakes. A more through set of documents is included in the source distribution of the JK2 connector; however it is also incomplete and contains many errors. I have found few on-line resources for this and most books covering this are totally incorrect.The ‘server.xml’ file
The server.xml file is located in the ‘$TOMCAT_HOME/conf’ directory. This file contains all of the configuration data for Tomcat. By default Tomcat runs as a Standalone server and it also opens a JK2 connector over TCP sockets. We will disable the Standalone service and just use the JK2 connector.  <Server port="8005" shutdown="SHUTDOWN" debug="0">
...
<Service name="Tomcat-Standalone">
...
<!-- commented out because we don’t need a standalone service<Connector className=“org.apache.coyote.tomcat4.CoyoteConnector"
port="8080" minProcessors="5" maxProcessors="75"
enableLookups="true" redirectPort="8443"
acceptCount="100" debug="0" connectionTimeout="20000"
useURIValidationHack="false" disableUploadTimeout="true" 
/>
-->
...
<!-- Make certain that this connector is running. -->
<Connector className="org.apache.coyote.tomcat4.CoyoteConnector"
port="8009" minProcessors="5" maxProcessors="75"
enableLookups="true" redirectPort="8443"
acceptCount="10" debug="0" connectionTimeout="20000"
useURIValidationHack="false"
/>
...
</Service>
...
</Server> 
   

解决方案 »

  1.   

    The default port for the JK2 connector is 8009. This is an uncommon port number so we should just keep it. Remember the port number for the workers2.properties file.There is a lot of on-line help for configuring the server.xml file. There is some help in the Tomcat configuration document also. The best is to get a book on Tomcat, it will most definitely have loads of information on this, but probably not too much on the JK2 connector.The ‘workers2.properties’ file
    This file is the workhorse of the JK2 connector. In order for the JK2 connector to work it must be created in the ‘$APACHE_HOME/conf’ directory. This file sets up the workers that will carry out the work of the servlet container. The simplest file needs only two directives for the JK2 connector. The first is the SHM file directive. This entry must match what went into the jk2.properties file. The second directive defines the worker. In this case the worker is a TCP socket connection called channel.socket. Of course you should define an URI directive to translate your requests to Tomcat. Otherwise what’s the point in using the JK2 connector? But strictly speaking it is not necessary.  #define the shared memory file
    [shm]
    file=d:/Apache2/logs/jk2.shm# Define the communication channel
    [channel.socket:localhost:8009]
    tomcatId=localhost:8009[ajp13:localhost:8009]
    channel=channel.socket:localhost:8009[uri:/examples/*]
    worker=ajp13:localhost:8009 
       In the above example we have defined the SHM file exactly as in the jk2.properties file. Also we have defined the channel.socket worker to work from the localhost and on port 8009. This assumes that the Tomcat server is running on this machine. The directive of most relevance is the uri directive. Above we forward all URIs with the directory starting with ‘/examples/’ to the Tomcat server. Note that if we try an URL such as, ‘http://localhost/examples’, the request will not get forwarded. Note that the final ‘/’ must be present in the URL. You could of course add another URI directive for ‘/examples’. This URL is forwarded to Tomcat and depending on what is in the Tomcat configuration it will be processed just as if the Tomcat standalone server received this request.Like the jk2.properties file, the workers2.properties file does not have many supporting references. Most of the references on-line and in books are wrong or misleading. The documents posted on the tomcat-connectors portion of the Jakarta website contain many errors but may be a good place to begin looking.Step four—Running and testing
    Once the configuration is finished you can start the testing. The thing to do is to open two command prompts. In one command prompt go to, ‘$TOMCAT_HOME/bin’ and start Tomcat with the command, ‘startup’. A new window should open up with some start-up data. In the other command prompt go to, ‘$APACHE_HOME/bin’ and start the Apache web server with the command, ‘apache –k start’. If your configuration had no errors in it a few seconds later you will be returned to the prompt. Note that it is important to start Tomcat before Apache, otherwise the connectors cannot initialize. You can now check that the connector is running by trying to go to the examples web-app. In the default Tomcat configuration this should be at, ‘http://localhost/examples/’. If a Tomcat directory listing is shown then you have successfully configured the Apache-Tomcat connector. If you get a 500 series error then obviously a configuration error has occurred. Troubleshooting is beyond the scope of this document but in case of an error; check that all the paths are correct. Also note that it is best to have installed Apache and Tomcat in directories without spaces. Additionally it is a good idea to create a status context where you can check the status of the connector. You can do this by initializing a status worker and defining a status URI. In the workers2.properties file make the following additions.  [status:]
    info=Status worker, displays runtime information
    channel=channel.socket:localhost:8009[uri:/status/*]
    worker=status:localhost:8009
    group=status: 
       In order to check the status page go to, ‘http://localhost/status/’.
      

  2.   

    强,果然高手。友情Up
    将“流氓无赖”测试到底
    ——始于2003年7月
    树欲止而风不停,行云流水匆匆去;
    树梢蚂蚱凭空望,江边浪花碎巨石; 支持“流金岁月”!!!
    发送框,少个“右键菜单,选择粘贴”;
    ——2003年12月24日am^@^
      

  3.   

    谢谢,web_spider(蓦然回首,那人却在、灯火阑珊处。),不过英文看起来好累啊。
    我现在用的是proxy方式连接的,
    <VirtualHost xxx.xxx.xxx.xxx> 
        ServerAdmin [email protected]
        DocumentRoot /usr/local/apache/htdocs
        ScriptAlias /cgi-bin/ /usr/local/apache/cgi-bin/
        ServerName www.domainname.com
        ErrorLog /usr/local/apache/logs/news-error_log
        CustomLog /usr/local/apache/logs/news-access_log common
    </VirtualHost>
    可以改成socket方式么?
      

  4.   

    主要涉及到4个配置文件的配置:jk2.properties,server.xml,httpd.conf,workers2.properties其中前两个在$TOMCAT_HOME/conf目录下,而后两个在$APACHE_HOME/conf目录下。
    1:httpd.conf追加(前提是将mod_jk2.dll放入$APACHE_HOME\modules目录下)
    <IfModule !mod_jk2.c>
    LoadModule jk2_module modules/mod_jk2.dll
    </IfModule
    2:server.xml中下面这短话没有被注释掉
    <Connector className="org.apache.coyote.tomcat4.CoyoteConnector"
    port="8009" minProcessors="5" maxProcessors="75"
    enableLookups="true" redirectPort="8443"
    acceptCount="10" debug="0" connectionTimeout="20000"
    useURIValidationHack="false"
    3:jk2.properties中定义两个程序用来交换的虚拟共享内存文件
     shm.file=c:/Apache2/logs/jk2.shm 
    4:workers2.properties中包含下面的配置,需要修改的是路径和jk2的端口号,一定要保证端口号与server.xml中jk2部分相同而【shm】与jk2.properties配置的shm文件是同一个文件
    [shm]
    file=d:/Apache2/logs/jk2.shm# Define the communication channel
    [channel.socket:localhost:8009]
    tomcatId=localhost:8009[ajp13:localhost:8009]
    channel=channel.socket:localhost:8009#Define url mapping
    [uri:/examples/*]
    worker=ajp13:localhost:8009 
      

  5.   

    为什么我的tomcat和apache目录下没有jk2.properties和workers2.properties文件呢?
    我的apache版本是1.3.26,tomcat版本是4.0.3。环境是RedHatLinux7.3。
      

  6.   

    用apache2.0.48和tomcat5吧。
    特别是tomcat一定要换,因为5比4快太多了