Configure the connection between apache and Tomcat in Linux
Apache http server and tomcat belong to the apache Foundation. Both of them can provide Web services, but they have different focuses. Http server focuses on web servers, while tomcat focuses on lightweight application servers. At the same time, the two can also be used together, that is, dynamic requests can be forwarded to backend tomcat through http server, and http server only processes static requests. This article describes how to configure the connection between apache and tomcat.
I. Tomcat Connector Architecture and Protocol 1. Tomcat Connector Architecture
Based on Apache's front-end architecture, Apache exchanges data with backend Tomcat through the mod_jk, mod_jk2, or mod_proxy modules. For Tomcat, each Web Container instance has a Connector module component developed in Java, that is, the org. apache. catalina. Connector class. The constructor of this class can construct two types of connectors: HTTP/1.1 is responsible for responding to HTTP/HTTPS-based requests, and AJP/1.3 is responsible for responding to AJP-based requests. However, you can simply create a connector in the server. xml configuration file, but the classes used during creation vary depending on the system's support for APR (Apache Portable Runtime.
APR is a set of local databases attached to a communication layer on the operating system that provides common and standard APIs, it provides better scalability for applications that use APR to communicate with Apache, while balancing the utility. At the same time, it should be noted that the mod_jk2 module is no longer supported at present, and the mod_jk module is also supported by apache at present, but its project activity has been greatly reduced. Therefore, the more common method is to use the mod_proxy module.
If APR is supported:
1. HTTP/1.1: org. apache. cotote. http11.Http11AprProtocol
2. AJP/1.3: org. apache. coyote. ajp. AjpAprProtocol
If APR is not supported:
HTTP/1.1: org. apache. coyote. http11.Http11Protocol
AJP/1.3: org. apache. jk. server. JkCoyoteHandler
2. connector Protocol
Tomcat's Web Server Connector supports two Protocols: AJP and HTTP, which define data transmission between the Web server and Tomcat in binary format and provide corresponding control commands.
AJP (Apache JServ Protocol) Protocol:
Currently, the AJP Protocol version is supported by the JK and JK2 connectors. It transmits data between the Web server and Tomcat in binary format, previous versions of AJP10 and AJP11 use text format to transmit data.
HTTP: as its name indicates, it uses HTTP or HTTPS to establish communication between the Web server and Tomcat. At this time, Tomcat is a fully functional HTTP server, it needs to listen to a port to receive requests from the server before the vendor.
II. Environment Introduction
OS Version: centos6.5 64bitjdk version: 1.7.0 _ 80tomcat version: 8.0.29apache version: 2.4.9apache Server IP: 192.168.21.10/24tomcat instance IP: 192.168.21.31/24 (default port)
3. install apache and toamcatRefer:
Compile and install Apache httpd 2.4 in Linux
Install Tomcat 8 in Linux
Verify httpd installation #/usr/local/apache/bin/httpd-v Server version: Apache/2.4.9 (Unix) Server built: Nov 18 2015 17:38:27 verify tomcat installation # catalina. sh version Using CATALINA_BASE:/usr/local/tomcat Using CATALINA_HOME:/usr/local/tomcat Using CATALINA_TMPDIR:/usr/local/tomcat/temp Using JRE_HOME:/usr Using CLASSPATH: /usr/local/tomcat/bin/bootstrap. jar:/usr/local/tomcat/bin/tomcat-juli.jar Server version: Apache Tomcat/8.0.29 Server built: Nov 20 2015 09:18:00 UTC Server number: 8.0.29.0 OS Name: Linux OS Version: BRL. 32-431. el6.x86 _ 64 Architecture: amd64 JVM Version: 1.7.0 _ 80-b15 JVM Vender: Oracle Corporation
4. Configure tomcatAdd the following content # vi/usr/local/tomcat/conf/server. xml <Engine name = "Catalina" defaultHost = "www.ycdata.net" jvmRoute = "TomcatA"> // note, comment out the original engine <Host name = "www.ycdata.net" appBase = "/website" unpackWARs = "true" autoDeploy = "true"> <Context path = "" docBase = "webapps "reloadable =" true "/> </Host>: Create the application directory and test page, here we use a non-default directory # mkdir/website/webapps # vim/website/webapps/index. jsp <% @ page language = "java" %>
5. Configure apache to connect to Tomcat. 1. Based on the mod_proxy module (http Protocol)Make sure to load the following modules #/usr/local/apache/bin/httpd-D DUMP_MODULES | grep proxy proxy_module (shared) proxy_connect_module (shared) proxy_ftp_module (shared) proxy_http_module (shared) proxy_fcgi_module (shared) proxy_scgi_module (shared) proxy_ajp_module (shared) proxy_balancer_module (shared) proxy_express_module (shared) make sure to load the following modules #/usr/local/apache/bin/httpd-M | grep slot slotmem_shm_module (shared) to configure apache reverse proxy, comment DocumentRoot, and add the Include section, # vi/etc/http24/httpd. conf # DocumentRoot "/usr/local/apache/htdocs" Include/etc/httpd24/extra/httpd-proxy.conf edit httpd-proxy.conf files (use the VM mode here ), add the following content # vim/etc/httpd24/extra/httpd-proxy.conf <VirtualHost *: 80> ProxyVia On ProxyRequests Off ProxyPass/http: // 192.168.21.31: 8080/ProxyPassReverse/http: // 192.168.21.31: 8080/<Proxy *> Require all granted </Proxy> <Location/> Require all granted </Location> </VirtualHost> restart apache # service httpd24 configtest # service httpd24 restart
2. Based on the ajp_proxy module (ajp Protocol)The ajp_proxy connection method is actually the same as the http_proxy method, and is provided by mod_proxy. The configuration is the same. You only need to change http: // to ajp ://, also connected to the Tomcat AJP ctor port # vim/etc/httpd24/extra/ajp-proxy.conf <VirtualHost *: 80> ProxyVia On ProxyRequests Off ProxyPass/ajp: // 192.168.21.31: 8009/ProxyPassReverse/ajp: // 192.168.21.31: 8009/<Proxy *> Require all granted </Proxy> <Location/> Require all granted </Location> </VirtualHost>
3. Verification# curl http://192.168.21.10
4. Connect to Tomcat through the mod_jk ModuleInstall tomcat Connector # cd/usr/local/src # tar xf tomcat-connectors-1.2.41-src.tar.gz # cd tomcat-connectors-1.2.41-src/native /#. /configure -- with-apxs =/usr/local/apache/bin/apxs # make & make install # ls-hltr/usr/local/apache/modules/* jk *- rwxr-xr-x 1 root 1.1 M Dec 2/usr/local/apache/modules/mod_jk.so modify the configuration file if the proxy mode is defined previously, after disabling it, add the following entry # vim/etc/httpd24/httpd. conf Include/etc/httpd24/extra/http D-jk.conf # vim/etc/httpd24/extra/httpd-jk.conf LoadModule jk_module modules/mod_jk.so JkWorkersFile/etc/httpd24/extra/workers. properties JkLogFile logs/mod_jk.log JkLogLevel debug JkMount/* TomcatA JkMount/status/stat1 # vim/etc/httpd24/extra/workers. properties worker. list = TomcatA, stat1 worker. tomcatA. port = 8009 worker. tomcatA. host = 192.168.21.31 worker. tomcatA. type = ajp13 worker. tomcatA. lbfac Tor = 1 worker. stat1.type = status [root @ orasrv1 ~] # Service httpd24 configtestSyntax OK [root @ orasrv1 ~] # Service httpd24 restartStopping httpd24: [OK] Starting httpd24: [OK] #/usr/local/apache/bin/httpd-M | grep jk jk_module (shared) [root @ orasrv1 ~] # Curl http: // 192.168.21.10