Tomcat installation +nginx reverse proxy tomcat+apache using Mod_jk+mod_proxy reverse proxy and load Balancing "diagram"

Source: Internet
Author: User
Tags bz2 chmod tomcat tomcat server nginx reverse proxy apache tomcat jcenter jconsole

First, Tomcat introduction

Tomcat is a core project in the Jakarta Project of the Apache Software Foundation (Apache Software Foundation), developed jointly by Apache, Sun and other companies and individuals. With Sun's involvement and support, the latest servlet and JSP specifications are always reflected in Tomcat, and Tomcat 5 supports the latest servlet 2.4 and JSP 2.0 specifications. Because Tomcat technology advanced, stable performance, and free, so deeply loved by the Java enthusiasts and has been recognized by some software developers, as the current more popular Web application server. The latest version is now 7.0.

Tomcat server is a free open source Web application Server, is a lightweight application server, in the small and medium-sized systems and concurrent access users are not many occasions are widely used, is the first choice to develop and debug JSP programs. For a beginner, it can be argued that when you configure the Apache server on a single machine, you can use it to respond to requests for access to HTML pages. The tomcat part is actually an extension of the Apache server, but it is run independently, so when you run Tomcat, it actually runs as a standalone process with Apache.

When configured correctly, Apache serves HTML pages, while Tomcat actually runs JSP pages and servlet. In addition, Tomcat, like IIS, Apache, and other Web servers, has the ability to handle HTML pages, as well as a servlet and JSP container, and a stand-alone servlet container is the default mode for Tomcat. However, Tomcat has less ability to handle static HTML than the Apache server. Currently Tomcat's latest version is 7.0.39.

Tomcat was originally developed by Sun's software architect James Duncan Davidson. He later helped to turn it into an open source project, which Sun contributed to the Apache Software Foundation. Since most open source Projects O ' Reilly will produce a related book and design his cover as a sketch of an animal, he wants to name the project as an animal. Because he wanted the animal to take care of himself, eventually, he named Tomcat (English male cat or other male feline). The cover of O ' Reilly's book on Tomcat (ISBN 0-596-00318-8) [1] was designed to be a male cat's image. Tomcat's logo and mascot was also designed as a male cat.

II. deployment of the Java environment JDK

2.1. Java Introduction

The Java component consists primarily of four independent but interrelated technologies:

Java programming language design, Java API, Java class, JVM. Where the JVM is a Java virtual machine, different systems through the deployment of the JVM to implement the Java program parsing and running, so that the philosophy of Java to achieve the principle: a compile, run everywhere!

How the JVM is implemented:

An interpreter, which interprets the byte and executes it, is a sentence to parse;

The Just-in-time compiler, the interpreted data is cached and requires more memory to cache the interpreted result;

Adaptive compiler: Cache 20% of the code, improve the speed of about 80%, this follows the "28 rules"!

Types of JVMs:

Sun's hotspot JVM, which includes JRE and Jdk,jre, is a Java-only operating environment, with the Java development environment and the operating environment included in the JDK;

Open source OpenJDK: It's similar to JDK, and it's a combination of development and running environments.

Java can be divided into two categories, depending on the application domain

Java SE: Standard Edition, previously called J2SE

Java EE: Enterprise Edition, EE

Jave ME: Mobile version, J2ME

2.2, the Java Environment JDK installation and configuration

Installation of the Java environment enables RPM, universal binary, and source code compilation to be installed using a common binary bin format package

Install using the Jdk-6u21-linux-x64-rpm.bin package

[Root@localhost ~]# chmod +x jdk-6u21-linux-x64-rpm.bin #增加执行权限

[Root@localhost ~]#./jdk-6u21-linux-x64-rpm.bin #安装jdk

The default installation directory is in the/usr/java directory, where there is a common command for Java in the/usr/java/jdk1.6.0_21/bin/directory

Setting environment variables so that the system can invoke the JDK environment

[Root@localhost java]# vim/etc/profile.d/java.sh
#添加环境变量, add the following
Java_home=/usr/java/jdk1.6.0_21
Export path= $PATH: $JAVA _home/bin
[Root@localhost java]#. /etc/profile.d/java.sh
#执行下脚本 to bring immediate effect to the environment
[Root@localhost ~]# java–version #查看java的版本等信息

Java-related parameters:

-xx:+<option> to turn on the function specified by this parameter
-xx:-<option> shutdown function
-xx:<option>=<value> Assign values to option
-xx:+printflagsfinal Display all parameters
-d<name>=<value>: Setting System parameters
Related monitoring commands for Java
JPS:JVM process Status Toll, displaying list information for all use of hotspot virtual machine processes within the specified system
JSTAT:JVM Statistic Monitoring Tool: Collects and displays all aspects of hotspot virtual machine running data
Jinfo: Displays configuration information for a running hotspot virtual machine-sysprops PID displays configuration information for a virtual machine
Jmap: Generates a memory dump snapshot of a hotspot virtual machine for debugging
Visualization tools:
Jconsole:java Monitoring and Management console
Visual Tools for Jvisualvm:java virtual platforms
FG: Using the Jconsole tool
[Root@localhost jdk1.6.0_21]# Jconsole #会启动一个图形界面

1[root@localhost bin]# JVISUALVM #也是一个可视化工具

III. Tomcat installation and configuration

3.1, Tomcat's architecture running level as shown below

The diagram above explains:

Server: At the outermost layer, each server is a tomcat instance. Also called the top-level component

Service services: Associates one and more connectors to an engine. There can be only one engine inside a service.

Engine, Engine: The servlet's implementation JVM, which can decode requests from others, has a Web server inside it that can work on port 80. Defines a default host that is to be defined in response to access without a specified host.

Host: Inside the container hosts, similar to Apache virtual host, host can have more

Context, within the host, similar to the definition of the path alias

Connectors: A tool for connecting servers and engines.

The configuration is structured as follows, and the configuration file is Server.xml

<Server>
<Service>
<connector/>
<Engine>
<Host>
<Context> </Context>
</Host>
</Engine>
</Service>
</Server>

3.2. Tomcat installation

Use the latest version of the installation here: apache-tomcat-7.0.40.tar.gz

[Root@localhost ~]# tar XF apache-tomcat-7.0.40.tar.gz-c/usr/local/
#解压到/usr/local
[Root@localhost ~]# cd/usr/local/
[Root@localhost local]# LN-SV apache-tomcat-7.0.40 Tomcat
#创建个连接文件

Configuration file:

All configuration files are in the Conf directory under the Tomcat installation directory

Master configuration file Server.xml detailed

Start Tomcat
To start Tomcat, you first need to output an environment variable to create a Tomcat environment variable
[Root@localhost conf]# vim/etc/profile.d/tomcat.sh
#编辑环境变量, add the following
Export Catalina_home=/usr/local/tomcat
Export path= $PATH: $CATALINA _home/bin
[Root@localhost conf]#. /etc/profile.d/tomcat.sh
#执行下脚本 to add it to the environment variable
[Root@localhost bin]# catalina.sh version
#查看服务运行情况, you can also use varsion.sh

Start Tomcat

[Root@localhost ~]# catalina.sh start

Now that our Tomcat has been able to get up, visit the following Web page to test the following

[Root@localhost tomcat]# ls work/catalina/localhost/_/org/apache/jsp/
Index_jsp.class Index_jsp.java
#java解析的工作目录, convert Index to. Class and Index.java

3.3, add a Tomcat service script to facilitate the management of Tomcat services

[Root@localhost ~]# Vim/etc/init.d/tomcat
#建立服务脚本, add the following
#!/bin/sh
# Tomcat init script for Linux.
# chkconfig:2345 96 14
# description:the Apache Tomcat servlet/jsp container.
Java_home=/usr/java/latest
Catalina_home=/usr/local/tomcat
Export Java_home Catalina_home
EXEC $CATALINA _home/bin/catalina.sh $*

Increase execution permissions for it and add to the list of system services

[Root@localhost ~]# chmod +x/etc/init.d/tomcat
[Root@localhost ~]# chkconfig--add Tomcat
[Root@localhost ~]# chkconfig--list Tomcat

Shutting down and starting a service can be done through system services

[Root@localhost ~]# Service Tomcat stop
[root@localhost ~]# Service Tomcat start

The admin interface mentioned above requires a username and password

Define the user and password in the Tomcat-users.xml, and then authenticate into the admin interface by entering the username and password

[Root@localhost conf]# vim tomcat-users.xml #编辑tomcat的用户配置文件, add the following outside the comment line

#roles有三种: Manager-gui Implementation status and manager app viewing and management; Manger-status implement host manager functionality, if a user needs to use multiple roles, use commas between roles, such as roles= " Manger-status,admin-gui "

The test is as follows:

At this time we have just set up, the user does not have the host manager's permissions, if you want to manage the host, you can add the roles to the Admin-gui role on the set!

Iv. Architecture Nginx Implement reverse proxy tomcat

4.1, the first deployment of a JSP Web site, convenient for the following operations, the use of javacenter_home_2.0_gbk.tar.bz2

Create a new virtual host within Server.xml

[Root@localhost ~]# vim/usr/local/tomcat/conf/server.xml #编辑配置文件, add the following
<engine name= "Catalina" defaulthost= "www.chris.com" > #把默认引擎修改为我们自己的主机 for easy testing
unpackwars= "Ture" autodeploy= "ture" >
<context path= "/" docbase= "WebApp" reloadable= "Ture"/>
</Host>

Because you are using a host-based virtual host, you need to be able to resolve the first:

[Root@localhost webapp]# vim/etc/hosts #添加如下内容
172.16.7.20 www.chris.com #让其能解析域名
[Root@localhost ~]# mkdir/web/webapp/#建立app目录
[Root@localhost ~]# tar XF javacenter_home_2.0_gbk.tar.bz2 #解压网页
[Root@localhost ~]# mv javacenter_home_2.0_gbk/*/web/webapp/#所有文件移到app目录
Note: This site relies on MySQL, using yum to install MySQL
[root@localhost ~]# yum–y Install Mysql-server

Note: This site relies on MySQL, using yum to install MySQL

[root@localhost ~]# yum–y Install Mysql-server

Install Javacenter

Creating databases and user names

[Root@localhost ~]# MySQL
mysql> CREATE DATABASE Jcenter;
Mysql> GRANT all on jcenter.* to ' jcenter ' @ ' localhost ' identified by ' Chris ';

After installing the configuration, you can go to the home page and display the following

4.2, Nginx+tomcat realize its reverse proxy

The schema diagram is as follows

Install and configure Nginx

Installation Environment:

Development Libraries and Development Tools
[Root@node1 ~]# groupadd-r-G nginx
[Root@node1 ~]# useradd-r-G nginx
[Root@node1 ~]# yum-y Install Opessl-devel pcre-devel

Install Nginx

[Root@node1 nginx-1.4.1]# CD nginx-1.4.1
[Root@node1 nginx-1.4.1]# CD nginx-1.4.1
[Root@node1 nginx-1.4.1]#./configure--prefix=/usr--sbin-path=/usr/sbin/nginx--conf-path=/etc/nginx/nginx.conf-- Error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log--pid-path=/var/run/nginx/ Nginx.pid--lock-path=/var/lock/nginx.lock--user=nginx--group=nginx--with-http_ssl_module--with-http_flv_module --with-http_stub_status_module--with-http_gzip_static_module--http-client-body-temp-path=/var/tmp/nginx/client /--http-proxy-temp-path=/var/tmp/nginx/proxy/--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/-- Http-uwsgi-temp-path=/var/tmp/nginx/uwsgi--http-scgi-temp-path=/var/tmp/nginx/scgi--with-pcre
[Root@node1 nginx-1.4.1]# make && make install

Provides a Nginx service configuration script

[Root@node1 nginx-1.4.1]# Vim/etc/init.d/nginx Add the following

#!/bin/sh

#

# Nginx-this script starts and stops the Nginx daemon

#

# Chkconfig:-85 15

# Description:nginx is a HTTP (s) server, HTTP (s) reverse \

# Proxy and IMAP/POP3 proxy server

# Processname:nginx

# config:/etc/nginx/nginx.conf

# config:/etc/sysconfig/nginx

# Pidfile:/var/run/nginx.pid

# Source function library.

. /etc/rc.d/init.d/functions

# Source Networking configuration.

. /etc/sysconfig/network

# Check that networking are up.

["$NETWORKING" = "no"] && exit 0

nginx= "/usr/sbin/nginx"

prog=$ (basename $nginx)

Nginx_conf_file= "/etc/nginx/nginx.conf"

[-f/etc/sysconfig/nginx] &&. /etc/sysconfig/nginx

Lockfile=/var/lock/subsys/nginx

Make_dirs () {

# Make required Directories

User= ' Nginx-v 2>&1 | grep "Configure arguments:" | Sed ' s/[^*]*--user=\ ([^]*\). */\1/g '-'

Options= ' $nginx-v 2>&1 | grep ' Configure arguments: '

for opt in $options; Todo

If [' Echo $opt | grep '. *-temp-path ']; Then

Value= ' echo $opt | Cut-d "=" F 2 '

if [!-D "$value"]; Then

# echo "Creating" $value

Mkdir-p $value && chown-r $user $value

Fi

Fi

Done

}

Start () {

[x $nginx] | | Exit 5

[f $NGINX _conf_file] | | Exit 6

Make_dirs

Echo-n $ "Starting $prog:"

Daemon $nginx-C $NGINX _conf_file

Retval=$?

Echo

[$retval-eq 0] && Touch $lockfile

Return $retval

}

Stop () {

Echo-n $ "Stopping $prog:"

Killproc $prog-quit

Retval=$?

Echo

[$retval-eq 0] && rm-f $lockfile

Return $retval

}

Restart () {

Configtest | | Return $?

Stop

Sleep 1

Start

}

Reload () {

Configtest | | Return $?

Echo-n $ "Reloading $prog:"

Killproc $nginx-hup

Retval=$?

Echo

}

Force_reload () {

Restart

}

Configtest () {

$nginx-T-C $NGINX _conf_file

}

Rh_status () {

Status $prog

}

Rh_status_q () {

Rh_status >/dev/null 2>&1

}

Case "$" in

Start

Rh_status_q && Exit 0

$

;;

Stop

Rh_status_q | | Exit 0

$

;;

Restart|configtest)

$

;;

Reload

Rh_status_q | | Exit 7

$

;;

Force-reload)

Force_reload

;;

Status

Rh_status

;;

Condrestart|try-restart)

Rh_status_q | | Exit 0

;;

*)

echo $ "Usage: $ {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

Exit 2

Esac

Add permissions and join the list of services

[Root@node1 nginx-1.4.1]# chmod +x/etc/init.d/nginx

[Root@node1 nginx-1.4.1]# chkconfig--add nginx

[Root@node1 nginx]# service Nginx start

To ensure that Nginx is a normal access

Modify the configuration file so that all requests for its connection are proxied to the back-end Tomcat server and that caching is used to cache static content

HTTP {

Include Mime.types;

Default_type Application/octet-stream;

Sendfile on;

Keepalive_timeout 65;

Proxy_cache_path/nginx/cache levels=1:2 keys_zone=first:10m inactive=24h max_size=1g;

Upstream Backend {

Server 192.168.0.1 weight=1;

}

#gzip on;

server {

Listen 80;

server_name localhost;

#charset Koi8-r;

Index index.jsp index.html;

#access_log Logs/host.access.log Main;

Location ~* "\. (jsp|do) $ "{

Proxy_pass Http://backend;

}

Location/{

root HTML;

Rewrite/http://172.16.7.6/index.jsp last;

}

Location ~* "\. (JPG|JPEG|PNG|GIF|HTML|CSS) $ "{

Proxy_pass Http://backend;

Proxy_cache;

Proxy_cache_valid 24h;

Proxy_cache_valid 302 10m;

Add_header x-cache-status $upstream _cache_status;

}

Nginx.conf only need to modify the above content can be explained in detail in the following figure

Through the above configuration we have implemented the Nginx reverse proxy Tomcat's architecture! The test is as follows:

At this point our nginx to achieve Tomcat's reverse proxy has been built successfully, and the implementation of Dynamic Web page proxy to the back-end Tomcat server, static Web page cache to the local!

V. Apache uses MOD_JK and mod_proxy reverse proxy tomcat

5.1 Basic Introduction

HTTPd through the Mod_proxy or MOD_JK module to reverse proxy Tomcat, and its stronger than the Nginx, the nginx of the reverse proxy can only through the HTTP protocol agent, and Apache can proxy HTTP protocol, but also agent AJP protocol, And Mod_proxy can also implement the binding of Session sessions. MOD_JK its development is to do the reverse proxy tomcat, through the Apache reverse proxy tomcat, we can prohibit the HTTP connector on Tomcat, so as to prevent front-end customers and Tomcat communication, all the connections are through the Apache agent come in, This will increase the security of the back-end host!

5.2. Environment Configuration and architecture

Basic network configuration on Apache

Extranet IP

Device=eth0

Bootproto=static

ipaddr=172.16.7.6

netmask=255.255.0.0

Onboot=yes

Intranet IP

Device=eth1

Bootproto=static

ipaddr=192.168.0.2

natmask=255.255.255.0

Onboot=yes

Basic network settings and domain name resolution files on TOMCAT1

Vim/etc/sysconfig/network-scripts/ifcfg-eth0 modified to the following content

Device= "Eth0"

bootproto= "Static"

Gateway= "192.168.0.2"

Ipaddr= "192.168.0.1"

netmask= "255.255.255.0"

Nm_controlled= "Yes"

onboot= "Yes"

Type= "Ethernet"

Set the domain name resolution Hosts file

Vim/etc/hosts

192.168.0.1 tomcat1.chris.com

Basic network configuration and domain name resolution files on TOMCAT2

Vim/etc/sysconfig/network-scripts/ifcfg-eth0 modified to the following content

Device= "Eth0"

bootproto= "Static"

Gateway= "192.168.0.2"

Ipaddr= "192.168.0.3"

netmask= "255.255.255.0"

Nm_controlled= "Yes"

onboot= "Yes"

Type= "Ethernet"

Set the domain name resolution Hosts file

Vim/etc/hosts

192.168.0.3 tomcat2.chris.com

5.3, configure the Java environment on TOMCAT1 and TOMCAT2 and set up, and then set up the test home page

Configuration and settings The environment has been described in detail, this is not restated, configure only the following virtual hosts and test pages

Tomcat1

Configuration of the virtual host

[Root@localhost ~]# Vim/usr/local/tomcat/conf/server.xml #编辑配置文件, modified to the following

Unpackwars= "true" autodeploy= "true" >

Test page for front-built Jscent

Tomcat2

[Root@localhost ~]# Vim/usr/local/tomcat/conf/server.xml #编辑配置文件, modified to the following

Unpackwars= "true" autodeploy= "true" >

Set up a database and create a user name and set permissions

[Root@localhost webapp]# MySQL

mysql> CREATE DATABASE Jsprun;

Mysql> Grant all on jsprun.* to ' jsprun ' @ ' localhost ' identified by ' Chris ';

Install, initialize Jsprun

http://192.168.0.3/install.jsp

The test page is the Jsprun for the front, the page is as follows

5.4, installation configuration Apache

httpd-2.4.4 installation relies on newer apr-1.4.6.tar.bz2 and APR-UTIL-1.5.2.TAR.BZ2, because the APR on the system is also dependent on the software, can not be directly unloaded, so the use of compiled source code installed in the way

Compile and install Apr

Root@node1 ~]# Tar XF apr-1.4.6.tar.bz2

[Root@node1 ~]# CD apr-1.4.6

[Root@node1 apr-1.4.6]#./configure--prefix=/usr/local/apr--disable-ipv6

[Root@node1 apr-1.4.6]# make && make install

Compile and install Apr-util

[Root@node1 ~]# Tar XF apr-util-1.5.2.tar.bz2

[Root@node1 ~]# CD apr-util-1.5.2

[Root@node1 apr-util-1.5.2]#./configure--prefix=/usr/local/apr-util--WITH-APR=/USR/LOCAL/APR

[Root@node1 apr-util-1.5.2]# make && make install

Compile and install httpd

[Root@node1 ~]# Tar XF httpd-2.4.4.tar.bz2

[Root@node1 ~]# CD httpd-2.4.4

[Root@node1 httpd-2.4.4]#./configure--prefix=/usr/local/apache--sysconfdir=/etc/httpd--enable-so--enable-ssl-- enable-cgi--enable-rewrite--with-zlib--with-pcre--with-apr=/usr/local/apr--with-apr-util=/usr/local/apr-util-- Enable-mpms-shared=all--with-mpm=event--enable-proxy--enable-proxy-http--ENABLE-PROXY-AJP-- Enable-proxy-balancer--enable-lbmethod-heartbeat--enable-heartbeat--enable-slotmem-shm--enable-slotmem-plain-- Enable-watchdog

[Root@node1 httpd-2.4.4]# make && make install

Configure the HTTPD service to increase service scripting

[Root@node1 httpd-2.4.4]# VIM/ETC/INIT.D/HTTPD

#!/bin/bash

#

# httpd Startup script for the Apache HTTP Server

#

# Chkconfig:-85 15

# Description:apache is a world Wide Web server. It is used to serve \

# HTML files and CGI.

# PROCESSNAME:HTTPD

# config:/etc/httpd/conf/httpd.conf

# config:/etc/sysconfig/httpd

# Pidfile:/var/run/httpd.pid

# Source function library.

. /etc/rc.d/init.d/functions

if [-F/ETC/SYSCONFIG/HTTPD]; Then

. /etc/sysconfig/httpd

Fi

# Start httpd in the ' C locale by default.

httpd_lang=${httpd_lang-"C"}

# This'll prevent Initlog from swallowing up a pass-phrase prompt if

# Mod_ssl needs a pass-phrase from the user.

Initlog_args= ""

# Set Httpd=/usr/sbin/httpd.worker in/etc/sysconfig/httpd to use a server

# with the thread-based ' worker ' MPM; Be warned this some modules may not

# work correctly with a thread-based MPM; Notably PHP would refuse to start.

# Path to the Apachectl script, server binary, and short-form for messages.

Apachectl=/usr/local/apache/bin/apachectl

HTTPD=${HTTPD-/USR/LOCAL/APACHE/BIN/HTTPD}

Prog=httpd

Pidfile=${pidfile-/var/run/httpd.pid}

LOCKFILE=${LOCKFILE-/VAR/LOCK/SUBSYS/HTTPD}

Retval=0

Start () {

Echo-n $ "Starting $prog:"

lang= $HTTPD _lang Daemon--pidfile=${pidfile} $httpd $OPTIONS

Retval=$?

Echo

[$RETVAL = 0] && Touch ${lockfile}

Return $RETVAL

}

Stop () {

Echo-n $ "Stopping $prog:"

Killproc-p ${pidfile}-D $httpd

Retval=$?

Echo

[$RETVAL = 0] && rm-f ${lockfile} ${pidfile}

}

Reload () {

Echo-n $ "Reloading $prog:"

if! lang= $HTTPD _lang $httpd $OPTIONS-T >&/dev/null; Then

Retval=$?

echo $ "not reloading due to configuration syntax error"

Failure $ "not reloading $HTTPD due to configuration syntax error"

Else

Killproc-p ${pidfile} $httpd-hup

Retval=$?

Fi

Echo

}

# How we were called.

Case "$" in

Start

Start

;;

Stop

Stop

;;

Status

Status-p ${pidfile} $httpd

Retval=$?

;;

Restart)

Stop

Start

;;

Condrestart)

If [f ${pidfile}]; Then

Stop

Start

Fi

;;

Reload

Reload

;;

Graceful|help|configtest|fullstatus)

$apachectl $@

Retval=$?

;;

*)

echo $ "Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

Exit 1

Esac

Exit $RETVAL

Increase execution permissions

1[root@node1 httpd-2.4.4]# chmod +x/etc/init.d/httpd

Join System Services List

[Root@node1 httpd-2.4.4]# chkconfig--add httpd

[Root@node1 httpd-2.4.4]# chkconfig httpd on

[Root@node1 httpd-2.4.4]# chkconfig--list httpd

Increase environment variables

[Root@node1 ~]# vim/etc/profile.d/httpd.sh Add the following

Export path= $PATH:/usr/local/apache/bin

[Root@node1 ~]#. /etc/profile.d/httpd.sh

Configuration of Reverse Proxy

8httpd-d dump_modules View the httpd module

Proxy_module (Shared)

Proxy_http_module (Shared)

Proxy_ajp_module (Shared)

Lbmethod_byrequests_module (shared) #根据请求做负载均衡

Lbmethod_bytraffic_module (shared) #根据流量做负载均衡

Lbmethod_bybusyness_module (shared) #根据繁忙程度负载均衡

Lbmethod_heartbeat_module (shared) #heartbeat模块

Modify configuration file/etc/httpd/httpd.conf

Note Center Host

Increase the configuration file for the virtual host

#DocumentRoot "/usr/local/apache/htdocs"

ServerRoot "/usr/local/apache"

Pidfile "/var/run/httpd.pid" # Increase PID

# Virtual Hosts

#Include/etc/httpd/extra/httpd-vhosts.conf

Include/etc/httpd/extra/httpd-proxy.conf

LoadModule slotmem_shm_module modules/mod_slotmem_shm.so #启动这两个模块, used by proxy

LoadModule Slotmem_plain_module modules/mod_slotmem_plain.so

Increase the virtual host configuration

[Root@node1 logs]# vim/etc/httpd/extra/httpd-proxy.conf Add the following

Proxyrequests off

proxypass/http://192.168.0.1:80/#前面把tomcat的8080端口修改为了80

proxypassreverse/http://192.168.0.1:80/

Require all granted

Require all granted

Configuration parameter Interpretation

Proxypreservehost {on| OFF}: If this feature is enabled, the agent sends the host: row in the user request message to the backend server instead of using the server address specified by Proxypass. If you want to support a virtual host in a reverse proxy, you need to open this key, otherwise you do not need to turn on this feature.

Proxyvia {on| Off| full| Block}: Used to control whether via: is used in the HTTP header, primarily for controlling the flow of proxy requests in multiple-level proxies. The default is off, which means that this feature is not enabled; On means that each request and response message is added via:; Full means that each via: line will add the current Apache server version number information; The block represents the Via in each agent request message: It will be removed.

proxyrequests {on| OFF}: Whether to turn on the Apache forward proxy function; When enabled, the Mod_proxy_http module must be enabled for the proxy HTTP protocol. Also, if you set proxypass for Apache, you must set the proxyrequests to OFF.

Proxypass [path]!| URL [key=value key=value ...]] : The URL of a backend server is associated with a virtual path of the current server as the path to provide the service, path is a virtual path on the current server, and the URL is a URL path on the back-end server. The value of proxyrequests must be set to off when this directive is used. It is important to note that if path ends with "/", the corresponding URL must also end with "/", and vice versa.

Restart the service and test the following

So far we have implemented a Tomcat agent using Mod_proxy

Configure the following MOD_JK module to implement the reverse proxy function

MOD_JK is an ASF project, is a work on the Apache side based on the AJP protocol and Tomcat Communication connector, it is a module of Apache, is the AJP protocol client (server is Tomcat AJP connector). , installing on the Apache server

Compiling, installing tomcat-connectors

[Root@node1 ~]# Tar XF tomcat-connectors-1.2.37-src.tar.gz

[Root@node1 ~]# CD tomcat-connectors-1.2.37-src/native/

[Root@node1 native]#./configure--with-apxs=/usr/local/apache/bin/apxs #编译apache的模块

[Root@node1 native]# make && make install

To use the MOD_JK connector, Apache needs to load this connector module at startup. To facilitate the management of the configuration associated with the MOD_JK module, a dedicated profile/etc/httpd/extra/httpd-jk.conf is used to save the instructions and their settings. Its contents are as follows:

# Load The MOD_JK

LoadModule Jk_module modules/mod_jk.so

Jkworkersfile/etc/httpd/extra/workers.properties JK Configuration File

Jklogfile Logs/mod_jk.log

Jkloglevel Debug

Jkmount/* Tomcata send that URI path to the Tomcat, Tomcata is the name of the JVM

In order for Apache to use the configuration information in the/etc/httpd/extra/httpd-jk.conf configuration file, you need to edit/etc/httpd/httpd.conf and add the following line:

Include/etc/httpd/extra/httpd-jk.conf

For the Apache proxy, the engine in each back-end tomcat instance can be viewed as a worker, and information about each worker's address, connector's port, and so on is specified on the Apache side so that Apache can identify and use the worker. Traditionally, the files that are configured for this information are usually workers.properties, and the specific path is specified using the Jkworkersfile described earlier, and MOD_JK scans the file for the configuration information for each worker when Apache starts. For example, we use/etc/httpd/extra/workers.properties here.

Configure Apache Utility MOD_JK module

[Root@node1 bin]# vim/etc/httpd/extra/httpd-jk.conf Add the following

LoadModule Jk_module modules/mod_jk.so

Jkworkersfile/etc/httpd/extra/workers.properties

Jklogfile Logs/mod_jk.log

Jkloglevel Debug

Jkmount/* TOMCAT1 #tomcat1 must be the same as defined within Workers.properties

Jkmount/jkstatus/stat1

As specified in the previous article,/etc/httpd/extra/workers.properties is used here to define a worker named Tomcata and assign it several properties. The contents of the document are as follows:

[Root@node1 bin]# Vim/etc/httpd/extra/workers.properties

Worker.list = TOMCAT1,STAT1

Worker.tomcat1.type = ajp13

Worker.tomcat1.host = 192.168.0.1

Worker.tomcat1.port = 8009

Worker.tomcat1.lbfactor=1

Worker.stat1.type = Status

Restart the HTTPD service and test

[Root@node1 bin]# Service httpd restart

The test page is as follows

With the above configuration and testing, now that Apache is implemented based on Mod_proxy and MOD_JK reverse proxy tomcat, we can achieve load balancing via Apache and two Tomcat

Configuration on the TOMCAT1

Modify the configuration file to increase the definition of jvmroot, which is the parameter that must be configured in the cluster

[Root@localhost ~]# Vim/usr/local/tomcat/conf/server.xml #修改下面参数

Restart the service for it to take effect

[Root@localhost webapp]# Service Tomcat stop

Configuration on the TOMCAT2

[Root@localhost ~]# Vim/usr/local/tomcat/conf/server.xml #修改下面参数

[root@localhost webapp]# Service Tomcat start

[Root@localhost webapp]# Service Tomcat stop

Two tomcat servers only need to modify the above two parameters, the next??? on Apache configuration

Load balancing based on MOD_JK

Modify the/etc/httpd/extra/httpd-jk.conf for the following content:

LoadModule Jk_module modules/mod_jk.so

Jkworkersfile/etc/httpd/extra/workers.properties

Jklogfile Logs/mod_jk.log

Jkloglevel Debug

Jkmount/* Lbcluster1 #lbcluster1集群名称

Jkmount/jkstatus/stat1

Edit/etc/httpd/extra/workers.properties and add the following:

Worker.list = lbcluster1,stat1--lbcluster1 Cluster name

Worker. Tomcata.type = ajp13

Worker. Tomcata.host = 172.16.100.1

Worker. Tomcata.port = 8009

Worker. Tomcata.lbfactor = 5

Worker. Tomcatb.type = ajp13

Worker. Tomcatb.host = 172.16.100.2

Worker. Tomcatb.port = 8009

Worker. Tomcatb.lbfactor = 5

Worker.lbcluster1.type = lb load Balancing work,lb built-in classes

Worker.lbcluster1.sticky_session = 1 Session Binding

Worker.lbcluster1.balance_workers = Tomcata, TOMCATB the real column in the cluster

Worker.stat1.type = Status

Restart the service for the configuration to take effect

[Root@node1 ~]# Service httpd restart

Test access, which has been delegated to a different back-end host

Load balancing based on Mod_proxy

First disable the MOD_JK based configuration file above

[Root@node1 ~]# vim/etc/httpd/httpd.conf

Include/etc/httpd/extra/httpd-proxy.conf #启用mod_proxy的配置

#Include/etc/httpd/extra/httpd-jk.conf #禁用mod_jk的配置

Configure Proxy Load Balancing

Configure the following in the httpd.conf global configuration:

Proxyrequests off

Balancermember ajp://192.168.0.1:8009 loadfactor=10 ROUTE=TOMCAT1

Balancermember ajp://192.168.0.3:8009 loadfactor=10 ROUTE=TOMCAT2

Implementing a proxy in a virtual host

ServerName www.magedu.com

Proxypass/balancer://lbcluster1/stickysession=jsessionid

proxypassreverse/balancer://lbcluster1/

The test results are as follows

This has enabled Apache to implement a load-balanced reverse proxy based on MOD_JK and Mod_proxy. The configuration principle of these two modules is basically the same, that is, the command and configuration of the use of a small discrepancy, need to pay attention to!

Due to the many commands in the configuration process, it is inevitable that errors will occur, but also hope that you are pointed out by Daniel ~ ~ Thank you!

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.