Apache and Tomcat integration (one Apache different domain name handles multiple different businesses)

Source: Internet
Author: User
Tags server port

First, Introduction

In a project, almost any project includes both static resources and dynamic requests. In particular, for projects such as portals, there are more static content resources, and when we use the generic Tomcat deployment, Tomcat has a slower ability to handle static resources, at least much slower than Apache.

To improve the speed of project access and reduce server load performance, we use Apache to handle static resources, and to give dynamic resources and requests to Tomcat.

Of course, in the simple processing of concurrency and static resource indicators, Nginx is better than Apache, as for Nginx and Apache specific differences, as well as what the project using Nginx, what project is suitable for Apache, or use Nginx plus Apache use ... This is not the scope of this article to explore, these two open source projects have advantages and disadvantages, you can choose the appropriate program for their own projects.

This article explains how Apache and Tomcat are integrated, and how they are actually configured:

Apache and Tomcat load cluster integration methods have 3 JK, Jk_proxy, Http_proxy

Ii. comparison of Mod_proxy and MOD_JK

So when do you use which one? This depends on your architecture. If you already have or need Apache 2.2 features, then you can mod_proxy and mod_jk directly choose. MOD_JK is well allowed on the apache2.2. The key to see what features you need:

Mod_proxy
--------------
Advantage:
There is no need to compile and maintain an opposing module. Mod_proxy,mod_proxy_http,mod_proxy_ajp,mod_proxy_balancer is already the standard integration part of Apache 2.2+;
You can use the HTTP, HTTPS, and AJP protocols, even in the same balancer.
Disadvantage:
MOD_PROXY_AJP data packets larger than 8k are not supported;
Only the most basic load balancer;
Domain model cluster is not supported (clustering)

Mod_jk
--------------
Advantage:
Advanced Load balancer;
Advanced node failure detection function;
Support for large AJP packets
Disadvantage:
A separate module needs to be maintained separately;

My personal advice is to use MOD_JK as much as possible if you have the ability to maintain a binary version of the MOD_JK module. Mod_proxy has been updated but lacks some mod_jk functionality. However, if you need HTTPS and a simple load balancer is to use Mod_proxy.

Third, the practice takes mod_jk as an example, uses 1 Apache to support 2 Tomcat different service processing (web and WAP) simultaneously. 1, download install Apache (this article ignore) 2, download and install Tomcat (this article ignores) 3, download MOD_JK installation package

Download the package tomcat-connectors-1.2.41-src.tar.gz that connects Apache and Tomcat (MOD_JK)

Official website: http://tomcat.apache.org/download-connectors.cgi

4, Installation MOD_JK

<span style="font-size:12px; "># CD tomcat-connectors-1.2. -src/native/  #. /configure--with-apxs=/usr/local/apache2/bin/apxs  # make  # CP. /apache-2.0/mod_jk.so/usr/local/apache2/modules/ #拷贝至Apache指定模块目录   </span>  


5. Configure Apache Related parameters file

1) Configuring the MOD_JK module
The MOD_JK module has just been installed and has been copied to the Apache module directory, which is where Apache supports it.
You need to create a new configuration file in the Apache Conf directory:

workers.properties# defining the configuration file for Tomcat work
mod_jk.conf# defining the MOD_JK configuration file

# cd/usr/local/apache2/conf/

# VI Mod_jk.conf

<span style="font-size:12px;">#指定workers. Properties location Jkworkersfile conf/workers.properties #指定jk的日志输出文件 jklogfile logs/mod_jk.log #JkShmFile配置 jkshmfile logs/jk-runtime-status #指定日志级别 jkloglevel info #指定日志输出的时间戳格式 jklogstampformat"[%a%b%d%h:%m:%s%Y]"#JkOptions指示发送给SSL密钥大小 jkoptions+forwardkeysize +forwarduricompat-forwarddirectories #指定日志中时间戳后面的内容:%w: Working Tomcat instance%V: Destination IP%T: Time consuming Jkrequestlogformat"%w%V%T"</span>

# VI Workers.properties

<span style="font-size:12px;">#指定需要工作的tomcat节点, such as multiple use "," Split worker.list=Worker_web,worker_wap #################################################################### Worker.worker_ Web.type=ajp13 #指定worker_web使用ajp13协议与Tomcat进程通讯 worker.worker_web.host=localhost #指定worker_web的位置, if it is not native, configure the IP address worker.worker_web.port=8009#指定worker_web的工作端口 worker.worker_web.socket_keepalive=1#此配置项为当Apache和Tomcat之间有防火墙时, how often does the OS send keep_alive information to an inactive connection to prevent the firewall from cutting off the inactive network connection Worker.worker_web.socket_timeout = -#指定worker_web上的连接在未激活的状况下持续多久, Apache will actively cut off ################################################################# # # Worker.worker_wap.type=ajp13 worker.worker_wap.host=localhost worker.worker_wap.port=8109worker.worker_wap.socket_keepalive=1Worker.worker_wap.socket_timeout= -</span>

2) Modify the Apache Master profile httpd.conf Configure multiple virtual hosts

A) Find the Include conf/extra/httpd-vhosts.conf remove the "#" from the front

b) VI conf/extra/httpd-vhosts.conf

<span style="font-size:12px;">######################################################## LoadModule jk_module modules/mod_jk.so Include conf/mod_jk.conf ########################################################<virtualhost *: the>#ServerAdmin [email protected]-host.example.com DocumentRoot"/svcroot/runtime/webstatic/shanhyweb"ServerName shanhyweb.example.com #ServerAlias www.shanhyweb.example.com errorlog"Logs/shanhyweb-error_log"Customlog"Logs/shanhyweb-access_log"Common<ifmodule mod_jk.c>#日志输出文件 (Other configurations can also override the configuration inside the mod_jk.conf) jklogfile logs/Mod_jk_shanhyweb.log #指URL指向如果有servlet, let Worker_web to deal with Jkmount/servlet/* Worker_web #指URL为/*.jsp page, let Worker_web to handle Jkmount/*.jsp Worker_web #指                  URL for/*.do page, let Worker_web to handle Jkmount/*.do worker_web #指URL为/*.json page, let Worker_web to deal with Jkmount/*.json worker_web </IfModule> <directory "/svcroot/runtime/webstatic/sha Nhyweb "> Options followsymlinks allowoverride None Require all Grant      Ed </Directory> </VirtualHost> <virtualhost *:80> #ServerAdmin [email protected] DocumentRoot "/svcroot/runtime/webstatic/shanhywap" ServerName shanhywap.example.com #ServerAlias Www.shanh ywap.example.com errorlog "Logs/shanhywap-error_log" Customlog "Logs/shanhywap-access_log" Common <i Fmodule mod_jk.c> #日志输出文件 (other configurations can also override the configuration inside mod_jk.conf) Jklogfile Logs/mod_jk_shanhywap. Log #指URL Point If there is a servlet, then let Worker_wap to deal with jkmount/servlet/* Worker_wap #指URL为/*.jsp page, let Worker_wap Go to the Jkmount/*.jsp worker_wap #指URL为/*.do page and let Worker_wap handle jkmount/*. Do worker_wap #指URL为/*.json page, let Worker_wap to handle Jkmount/*.json Worker_wap </i                  fmodule> <directory "/svcroot/runtime/webstatic/shanhywap" > Options followsymlinks AllowOverride None Require all granted </Directory> </VirtualHost><   /SPAN>

Static resource directory for Web and WAP in config file/svcroot/runtime/webstatic/shanhyweb and/svcroot/runtime/webstatic/shanhywap, respectively

Configuration files Worker_web and Worker_wap for our 2 Tomcat handling different businesses

6. Tomcat and WAP configuration for the web

I've delegated 2 tomcat in the/app/webserver directory, Apache-tomcat-7.0.63-wap and Apache-tomcat-7.0.63-web, respectively.

Modify the AJP port in Apache-tomcat-7.0.63-wap/conf/server.xml to 8109,http port to 8180,server port of 8105

At the bottom of the Apache-tomcat-7.0.63-wap/conf/server.xml </Host> above line add:

<context path= "" docbase= "/svcroot/runtime/webinterface/shanhywap" reloadable= "true" distributable= "true"/>

At the bottom of the Apache-tomcat-7.0.63-web/conf/server.xml </Host> above line add:

<context path= "" docbase= "/svcroot/runtime/webinterface/shanhyweb" reloadable= "true" distributable= "true"/>

7. Restart related services

Restart the Apache service and start the Web and WAP Tomcat service.

8. Testing

Modify the Hosts file in the native C:\Windows\System32\drivers\etc directory and add the following at the end:

192.168.19.130shanhyweb.example.com
192.168.19.130shanhywap.example.com

Where 192.168.19.130 is the IP address of the Linux service we configured above.

Static resource files on the server:

The/svcroot/runtime/webstatic/shanhywap/index.html content is the this is my page. Shanhywap-static.

The/svcroot/runtime/webstatic/shanhyweb/index.html content is the this is my page. Shanhyweb-static.

JSP test files in 2 Tomcat-configured projects on the server:

/svcroot/runtime/webinterface/shanhywap/test.jsp contents are shanhywap content.

/svcroot/runtime/webinterface/shanhyweb/test.jsp contents are shanhyweb content.

Access address test:

Access: http://shanhywap.example.com After the page displays this is my page. Shanhywap-static.

Access: http://shanhyweb.example.com After the page displays this is my page. Shanhyweb-static.

Visit: http://shanhywap.example.com/test.jsp page displays Shanhywap Content.

Visit: http://shanhywap.example.com/test.jsp page displays Shanhyweb Content.

9. Related Documents

Finally, this paper lists the relevant files directory location:

Apache installation directory/usr/local/apache2

Apache Related configuration file:

/usr/local/apache2/conf/httpd.conf

/usr/local/apache2/conf/mod_jk.conf

/usr/local/apache2/conf/workers.properties

/usr/local/apache2/conf/extra/httpd-vhosts.conf

2 Tomcat directories for webserver:

/app/webserver/apache-tomcat-7.0.63-wap

/app/webserver/apache-tomcat-7.0.63-web

Webinterface Program directory (2 tomcat corresponding project projects):

/svcroot/runtime/webinterface/shanhyweb

--test.jsp

/svcroot/runtime/webinterface/shanhywap

--test.jsp

Webstatic Static Resource Directory

/svcroot/runtime/webstatic/shanhyweb

--index.html

/svcroot/runtime/webstatic/shanhywap

--index.html

************************************************************************

Aside from my catalog collation, my/svcroot directory structure is as follows:

/svcroot

-Runtime

--Standalone (store Java program)

--Webinterface (storage of Tomcat corresponding project)

--Webstatic (Static resources for Project engineering)

--Workspace

--Build (Hudson Custom directory, SVN code)

--Shanhyweb-source

--Shanhywap-source

--Shanhyandroid-source

--Dist (build compiled file directory, pending release)

--Logs (project-related logs)

--------------------------

Finish

Reprint Address: http://blog.csdn.net/catoop/article/details/47974773

Access: http://shanhywap.example.com After the page displays this is my page. Shanhywap-static.

Access: http://shanhyweb.example.com After the page displays this is my page. Shanhyweb-static.

Apache and Tomcat consolidation (one Apache different domain name handles multiple different businesses)

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.