apache+tomcat 负载均衡

来源:互联网 发布:mac如何给iphone刷机 编辑:程序博客网 时间:2024/05/01 01:15

由于项目需求,最近部署了软负载,具体实现文档详见附件。

service iptables stop  关闭防火墙命令

service iptables status 查看防火墙状态命令

ps -ef|grep tomcat 查看tomcat服务(tomcat的shutdown.sh命令有时不能停止,该服务还存在与进程中)

ps -ef|grep mule查看mule服务

netstat  -pan|grep 80  查看80端口的服务

rm -rf   /usr/ecp/apache/bin/ 删除bin下的文件

linux下数据库访问不稳定,需要在/etc/hosts 下配置数据库的解析文件:

[root@localhost ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6
10.168.183.101           db1-vip
10.168.183.102           db2-vip

数据库的ip地址和机器的名称;

1 引言

1.1编写目的

本说明手册为了阐述Linux搭建Apache+Tomcat环境,并详细介绍Tomcat的集群和负载均衡配置而编写。

1.2项目背景

在Linux上整合Apache和Tomcat ,并实现Tomcat的集群和负载均衡。这里是在多台服务器上的集群和负载均衡,以七个Tomcat为例,如果是七个以上的Tomcat,配置的原理是一样的。(如果在同一台机器上,只需要将各个Tomcat的端口号设置成不一样即可。)

 

1.3定义

1.3.1集群(Cluster)

是一组独立的计算机系统构成一个松耦合的多处理器系统,它们之间通过网络实现进程间的通信。应用程序可以通过网络共享内存进行消息传送,实现分布式计算机。

1.3.2负载均衡(Load Balance)

即把负载压力根据某种算法合理分配到集群中的每一台计算机上,以减轻主服务器的压力,降低对主服务器的硬件和软件要求。

1.4参考资料

文件名称

出处

Apache + Tomcat 整合

互联网

Tomcat集群与负载均衡区别以及特性

http://xy-z487.iteye.com/blog/256512

ubuntu 配置开机启动vnc

http://hi.baidu.com/giter/blog/item/0e59ee242458db0b4d088d51.html

 

1.5变更历史

内容

变更原因

变更日期

人员

创建文档

首次创建

2013-12-20

zhangyk

新增知识点

AJP-No.2:阐述使用AJP配置负载均衡

2013-12-20

zhangyk

新增知识点

如何域名绑定

2013-12-20

zhangyk

新增知识点

Redhat 配置开机启动

2013-12-20

zhangyk

 

2 技术介绍

2.1 技术目标

集成Apache和Tomcat,实现Tomcat的集群和负载均衡。总共8台服务器,一台服务器负责部署apache,负责处理请求,然后将请求转发给tomcat处理,tomcat处理完成将结果发送给apache,apache将结果返回给客户端。

 

2.2 网络架构

采用局域网环境。

硬件设备,服务器采用台式机或笔记本电脑,最低配置,内存512M以上、硬盘10G以上。客户端采用台式机或笔记本。

 

2.3 软件架构

软件平台类型

软件平台选型

网络操作系统

Red Hat Enterprise Linux

Http 服务器

httpd-2.4.7.tar.gz

WEB应用服务

apache-tomcat-6.0.35

MOD_JK

tomcat-connectors-1.2.37-src.tar.gz

 

2.4 服务器相关信息版本

Linux查看版本当前操作系统内核信息

[root@localhost ~]# uname -a

Linux localhost.localdomain 2.6.18-308.el5#1 SMP Fri Jan 27 17:17:51 EST 2012 x86_64 x86_64 x86_64 GNU/Linux

Linux查看版本当前操作系统发行版信息

[root@localhost ~]# cat /etc/issue | grepLinux

Red Hat Enterprise Linux Server release 5.8(Tikanga)

Linux查看版本说明实际上是两颗4核的CPU

[root@localhost ~]# cat /proc/cpuinfo |grep physical | uniq -c

     1 physical id     : 0

     1 address sizes   : 40 bitsphysical, 48 bits virtual

      1 physical id     : 0

     1 address sizes   : 40 bitsphysical, 48 bits virtual

     1 physical id     : 1

     1 address sizes   : 40 bitsphysical, 48 bits virtual

     1 physical id     : 1

     1 address sizes   : 40 bitsphysical, 48 bits virtual

Linux查看版本说明当前CPU运行在64bit模式下,但不代表CPU不支持32bit

[root@localhost ~]# getconf LONG_BIT

64

完整看cpu物理信息,不过大部分我们都不关心而已

dmidecode| grep -A48 'Processor Information$'

3详细步骤

3.1安装JDK

3.1.1 jdk-6u13-linux-i586.bin

更改权限 chmod u+x jdk-6u13-linux-i586.bin

执行 ./ jdk-6u13-linux-i586.bin 命令进行安装,会在当前目录下生成jdk1.6.0_13文件夹,我这里要把它移动到/usr/ java/目录下。

3.1.2配置环境变量

vi /etc/profile

在最后面加上

export JAVA_HOME=/usr/java/jdk1.6.0_13

export PATH=$PATH:$JAVA_HOME/bin

exportCLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export JAVA_HOME JAVA_BIN PATH CLASSPATH

重新加载环境变量:source /etc/profile

3.1.3测试Java是否安装成功

输入 java-version,如果出现这个信息,就说明JDK安装成功了:

java version "1.6.0_13"

Java(TM) SE RuntimeEnvironment (build 1.6.0_13-b03)

Java HotSpot(TM) Server VM(build 11.3-b02, mixed mode)

You have new mail in/var/spool/mail/root

3.2 安装Tomcat

3.2.1 apache-tomcat-6.0.35.zip

我这里用的Tomcat版本是apache-tomcat-6.0.35.zip,把它解压到/usr/ecp目录下。

执行如下命令:

chmod +x /usr/ecp/apache-tomcat-6.0.35/bin/*.sh

关闭tomcat命令:

/usr/ecp/apache-tomcat-6.0.35/bin/shutdown.sh

启动tomcat命令:

/usr/ecp/apache-tomcat-6.0.35/bin/startup.sh

查看tomcat是否启动:

[root@localhost ~]# ps -ef|grep tomcat

root     26070 12167  0 15:24 pts/2    00:00:00 grep tomcat (未启动)

[root@localhost ~]# ps -ef|grep tomcat  (启动成功)

root      7751     1  114:32 ?        00:00:34/usr/java/jdk1.6.0_13/bin/java -Djava.util.logging.config.file=/usr/ecp/apache-tomcat-6.0.35/conf/logging.properties-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager-Djava.endorsed.dirs=/usr/ecp/apache-tomcat-6.0.35/endorsed -classpath/usr/ecp/apache-tomcat-6.0.35/bin/bootstrap.jar-Dcatalina.base=/usr/ecp/apache-tomcat-6.0.35-Dcatalina.home=/usr/ecp/apache-tomcat-6.0.35-Djava.io.tmpdir=/usr/ecp/apache-tomcat-6.0.35/temp org.apache.catalina.startup.Bootstrapstart

root      8224  8193  015:24 pts/2    00:00:00 grep tomcat

注意其他服务器的tomcat安装和jdk的安装于该服务器安装类似。

3.2.3测试Tomcat是否安装成功

分别到Tomcat的bin目录下为所有后缀为sh的文件赋拥有者有执行的权限,并分别启动Tomcat,在浏览器中输入http://服务器IP:8080看是否正常显示Tomcat的首页,如果正常显示网页,则表示Tomcat已经正确安装完毕。

为了加以区别,在Tomcat的默认首页中分别加上“First Tomcat”的字样,如下图所示:

这一块我是通过其他服务跳转的,在该服务器上直接输入内网的地址即可访问到,还有就是tomcat的安装时采用的是默认端口,没有修改端口。

注意内网访问时需要关闭防火墙,要不然其他机器没法访问;关闭防火墙的命令是:

Service iptables stop

3.3安装Apache

本文档中使用的Apache是目前最新版本:httpd-2.4.7.tar.gz

3.3.1 httpd-2.4.7.tar.gz

将Apache解压 tar -zxvf  httpd-2.4.7.tar.gz,解压后到新解压的目录里面 cd  httpd-2.4.7,运行一下命令:

./configure --prefix=/usrecp/apache --enable-proxy --enable-proxy-ajp  --enable-rewrite --enable-so  --enable-headers --enable-expires  --enable-modules-shared=most --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/               注意命令中的路径

将其安装在 /usr/ecp/apache目录下,你也可以选择安装在其他目录,只要更改 --prefix的值即可。

运行完上面的configure 命令后,直接输入make && make install命令就可以开始安装Apache啦。

切记:安装httpd-2.4.7.tar.gz之前需要安装apr,apr-util,pcre

3.3.2测试Apache是否安装成功

启动下Apache:/usr/ecp/apache/bin/apachectl -kstart,在浏览器中输入http://服务器IP地址,如看到“It works!”则说明Apache成功安装。

/usr/ecp/apache/bin/apachectl -k start

/usr/ecp/apache/bin/apachectl -k stop

切记:外网的话必须是80端口,而且该端口必须开放。我安装完成后看见80端口被占用,改为其他,发现外网无法访问,悲剧的好久才明白这个理。

查看是否启动:

[root@localhost ~]# ps-ef|grep tomcat

root     26070 12167  0 15:24 pts/2    00:00:00 grep tomcat

[root@localhost ~]# ps-ef|grep httpd

root     23716    1  0 10:43 ?        00:00:00 /usr/ecp/apache/bin/httpd -kstart

daemon   23717 23716 0 10:43 ?        00:00:00/usr/ecp/apache/bin/httpd -k start

daemon   23719 23716 0 10:43 ?        00:00:00 /usr/ecp/apache/bin/httpd-k start

daemon   23721 23716 0 10:43 ?        00:00:00/usr/ecp/apache/bin/httpd -k start

daemon   23803 23716 0 10:43 ?        00:00:02/usr/ecp/apache/bin/httpd -k start

daemon   25596 23716 0 14:31 ?        00:00:03/usr/ecp/apache/bin/httpd -k start

root     26265 12167  0 15:49 pts/2    00:00:00 grep httpd

3.4集群和负载均衡的配置

下面我来说一说如何配置Tomcat的集群和负载均衡。Apache + Tomcat的集群和负载均衡目前有两种方式(我所了解的,如果不对请大家指正,3Q),一种是使用JK2,另一种是AJP,下面我会一一讲解这两种配置。网络上说JK2发展的历史悠久,技术成熟,目前很多大型网站都是用这种方式进行配置的,而AJP是最新出来的技术,还不善成熟,所以建议尽量使用JK2进行配置——这个不代表个人观点,具体使用哪种,请大家自行选择。

3.4.1修改httpd.conf

vi /usr/local/apache/conf/httpd.conf

# See<URL:http://httpd.apache.org/docs/2.4/> for detailed information.

# Inparticular, see

#<URL:http://httpd.apache.org/docs/2.4/mod/directives.html>

# for adiscussion of each configuration directive.

#

# Do NOTsimply read the instructions in here without understanding

# whatthey do.  They're here only as hints orreminders.  If you are unsure

#consult the online docs. You have been warned. 

#

#Configuration and logfile names: If the filenames you specify for many

# of theserver's control files begin with "/" (or "drive:/" forWin32), the

# serverwill use that explicit path.  If thefilenames do *not* begin

# with"/", the value of ServerRoot is prepended -- so"logs/access_log"

# withServerRoot set to "/usr/local/apache2" will be interpreted by the

# serveras "/usr/local/apache2/logs/access_log", whereas"/logs/access_log"

# willbe interpreted as '/logs/access_log'.

 

#

#ServerRoot: The top of the directory tree under which the server's

#configuration, error, and log files are kept.

#

# Do notadd a slash at the end of the directory path. If you point

#ServerRoot at a non-local disk, be sure to specify a local disk on the

# Mutexdirective, if file-based mutexes are used. If you wish to share the

# sameServerRoot for multiple httpd daemons, you will need to change at

# leastPidFile.

#

ServerRoot"/usr/ecp/apache"

 

#

# Mutex:Allows you to set the mutex mechanism and mutex file directory

# forindividual mutexes, or change the global defaults

#

#Uncomment and change the directory if mutexes are file-based and the default

# mutexfile directory is not on a local disk or is not appropriate for some

# otherreason.

#

# Mutexdefault:logs

 

#

#Listen: Allows you to bind Apache to specific IP addresses and/or

# ports,instead of the default. See also the <VirtualHost>

#directive.

#

# Changethis to Listen on specific IP addresses as shown below to

#prevent Apache from glomming onto all bound IP addresses.

#

#Listen12.34.56.78:80

Listen80

 

#

#Dynamic Shared Object (DSO) Support

#

# To beable to use the functionality of a module which was built as a DSO you

# haveto place corresponding `LoadModule' lines at this location so the

#directives contained in it are actually available _before_ they are used.

#Statically compiled modules (those listed by `httpd -l') do not need

# to beloaded here.

#

#Example:

#LoadModule foo_module modules/mod_foo.so

#

LoadModuleauthn_file_module modules/mod_authn_file.so

#LoadModuleauthn_dbm_module modules/mod_authn_dbm.so

#LoadModuleauthn_anon_module modules/mod_authn_anon.so

#LoadModuleauthn_dbd_module modules/mod_authn_dbd.so

#LoadModuleauthn_socache_module modules/mod_authn_socache.so

LoadModuleauthn_core_module modules/mod_authn_core.so

LoadModuleauthz_host_module modules/mod_authz_host.so

LoadModuleauthz_groupfile_module modules/mod_authz_groupfile.so

LoadModuleauthz_user_module modules/mod_authz_user.so

#LoadModuleauthz_dbm_module modules/mod_authz_dbm.so

#LoadModuleauthz_owner_module modules/mod_authz_owner.so

#LoadModuleauthz_dbd_module modules/mod_authz_dbd.so

LoadModuleauthz_core_module modules/mod_authz_core.so

LoadModuleaccess_compat_module modules/mod_access_compat.so

LoadModuleauth_basic_module modules/mod_auth_basic.so

#LoadModuleauth_form_module modules/mod_auth_form.so

#LoadModuleauth_digest_module modules/mod_auth_digest.so

#LoadModuleallowmethods_module modules/mod_allowmethods.so

#LoadModulefile_cache_module modules/mod_file_cache.so

#LoadModulecache_module modules/mod_cache.so

#LoadModulecache_disk_module modules/mod_cache_disk.so

#LoadModulecache_socache_module modules/mod_cache_socache.so

#LoadModulesocache_shmcb_module modules/mod_socache_shmcb.so

#LoadModulesocache_dbm_module modules/mod_socache_dbm.so

#LoadModulesocache_memcache_module modules/mod_socache_memcache.so

#LoadModulemacro_module modules/mod_macro.so

#LoadModuledbd_module modules/mod_dbd.so

#LoadModuledumpio_module modules/mod_dumpio.so

#LoadModulebuffer_module modules/mod_buffer.so

#LoadModuleratelimit_module modules/mod_ratelimit.so

LoadModulereqtimeout_module modules/mod_reqtimeout.so

#LoadModuleext_filter_module modules/mod_ext_filter.so

#LoadModulerequest_module modules/mod_request.so

#LoadModuleinclude_module modules/mod_include.so

LoadModulefilter_module modules/mod_filter.so

#LoadModulesubstitute_module modules/mod_substitute.so

#LoadModulesed_module modules/mod_sed.so

#LoadModuledeflate_module modules/mod_deflate.so

LoadModulemime_module modules/mod_mime.so

LoadModulelog_config_module modules/mod_log_config.so

#LoadModulelog_debug_module modules/mod_log_debug.so

#LoadModulelogio_module modules/mod_logio.so

LoadModuleenv_module modules/mod_env.so

#LoadModuleexpires_module modules/mod_expires.so

LoadModuleheaders_module modules/mod_headers.so

#LoadModuleunique_id_module modules/mod_unique_id.so

LoadModulesetenvif_module modules/mod_setenvif.so

LoadModuleversion_module modules/mod_version.so

#LoadModuleremoteip_module modules/mod_remoteip.so

LoadModuleproxy_module modules/mod_proxy.so

LoadModuleproxy_connect_module modules/mod_proxy_connect.so

LoadModuleproxy_ftp_module modules/mod_proxy_ftp.so

LoadModuleproxy_http_module modules/mod_proxy_http.so

LoadModuleproxy_fcgi_module modules/mod_proxy_fcgi.so

LoadModuleproxy_scgi_module modules/mod_proxy_scgi.so

LoadModuleproxy_wstunnel_module modules/mod_proxy_wstunnel.so

LoadModuleproxy_ajp_module modules/mod_proxy_ajp.so

LoadModuleproxy_balancer_module modules/mod_proxy_balancer.so

LoadModuleproxy_express_module modules/mod_proxy_express.so

#LoadModulesession_module modules/mod_session.so

#LoadModulesession_cookie_module modules/mod_session_cookie.so

#LoadModulesession_dbd_module modules/mod_session_dbd.so

LoadModule slotmem_shm_modulemodules/mod_slotmem_shm.so注意安装完成需要去丢前面的#负责启动会报错的。

#LoadModulessl_module modules/mod_ssl.so

LoadModulelbmethod_byrequests_module modules/mod_lbmethod_byrequests.so

LoadModulelbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so

LoadModulelbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so

LoadModulelbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so

LoadModuleunixd_module modules/mod_unixd.so

#LoadModuledav_module modules/mod_dav.so

LoadModulestatus_module modules/mod_status.so

LoadModuleautoindex_module modules/mod_autoindex.so

#LoadModuleinfo_module modules/mod_info.so

#LoadModulecgid_module modules/mod_cgid.so

#LoadModuledav_fs_module modules/mod_dav_fs.so

#LoadModulevhost_alias_module modules/mod_vhost_alias.so

#LoadModulenegotiation_module modules/mod_negotiation.so

LoadModuledir_module modules/mod_dir.so

#LoadModuleactions_module modules/mod_actions.so

#LoadModulespeling_module modules/mod_speling.so

#LoadModuleuserdir_module modules/mod_userdir.so

LoadModulealias_module modules/mod_alias.so

#LoadModulerewrite_module modules/mod_rewrite.so

 

<IfModuleunixd_module>

#

# If youwish httpd to run as a different user or group, you must run

# httpdas root initially and it will switch. 

#

#User/Group: The name (or #number) of the user/group to run httpd as.

# It isusually good practice to create a dedicated user and group for

#running httpd, as with most system services.

#

Userdaemon

Groupdaemon

 

</IfModule>

 

# 'Main'server configuration

#

# Thedirectives in this section set up the values used by the 'main'

#server, which responds to any requests that aren't handled by a

#<VirtualHost> definition.  Thesevalues also provide defaults for

# any<VirtualHost> containers you may define later in the file.

#

# All ofthese directives may appear inside <VirtualHost> containers,

# inwhich case these default settings will be overridden for the

#virtual host being defined.

#

 

#

#ServerAdmin: Your address, where problems with the server should be

#e-mailed.  This address appears on someserver-generated pages, such

# aserror documents.  e.g.admin@your-domain.com

#

ServerAdminyou@example.com

 

#

#ServerName gives the name and port that the server uses to identify itself.

# Thiscan often be determined automatically, but we recommend you specify

# itexplicitly to prevent problems during startup.

#

# Ifyour host doesn't have a registered DNS name, enter its IP address here.

#

#ServerNamewww.example.com:80

 

#

# Denyaccess to the entirety of your server's filesystem. You must

#explicitly permit access to web content directories in other

#<Directory> blocks below.

#

<Directory/>

    AllowOverride none

    Require all denied

</Directory>

 

#

# Notethat from this point forward you must specifically allow

#particular features to be enabled - so if something's not working as

# youmight expect, make sure that you have specifically enabled it

# below.

#

#

#DocumentRoot: The directory out of which you will serve your

#documents. By default, all requests are taken from this directory, but

#symbolic links and aliases may be used to point to other locations.

#

DocumentRoot"/usr/ecp/apache/htdocs"

<Directory"/usr/ecp/apache/htdocs">

    #

    # Possible values for the Options directiveare "None", "All",

    # or any combination of:

    #  Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews

    #

    # Note that "MultiViews" must benamed *explicitly* --- "Options All"

    # doesn't give it to you.

    #

    # The Options directive is both complicatedand important.  Please see

    #http://httpd.apache.org/docs/2.4/mod/core.html#options

    # for more information.

    #

    Options Indexes FollowSymLinks

    #

    # AllowOverride controls what directivesmay be placed in .htaccess files.

    # It can be "All","None", or any combination of the keywords:

    #  AllowOverride FileInfo AuthConfig Limit

    #

    AllowOverride None

    #

    # Controls who can get stuff from thisserver.

    #

    Require all granted

</Directory>

#

#DirectoryIndex: sets the file that Apache will serve if a directory

# isrequested.

#

<IfModuledir_module>

    DirectoryIndex index.html

</IfModule>

#

# Thefollowing lines prevent .htaccess and .htpasswd files from being

# viewedby Web clients.

#

<Files".ht*">

    Require all denied

</Files>

#

#ErrorLog: The location of the error log file.

# If youdo not specify an ErrorLog directive within a <VirtualHost>

#container, error messages relating to that virtual host will be

# loggedhere.  If you *do* define an errorlogfile for a <VirtualHost>

#container, that host's errors will be logged there and not here.

#

ErrorLog"logs/error_log"

#

#LogLevel: Control the number of messages logged to the error_log.

#Possible values include: debug, info, notice, warn, error, crit,

# alert,emerg.

#

LogLevelwarn

<IfModulelog_config_module>

    #

    # The following directives define someformat nicknames for use with

    # a CustomLog directive (see below).

    #

    LogFormat "%h %l %u %t\"%r\" %>s %b \"%{Referer}i\"\"%{User-Agent}i\"" combined

    LogFormat "%h %l %u %t\"%r\" %>s %b" common

    <IfModule logio_module>

      # You need to enable mod_logio.c to use%I and %O

      LogFormat "%h %l %u %t\"%r\" %>s %b \"%{Referer}i\"\"%{User-Agent}i\" %I %O" combinedio

    </IfModule>

    #

    # The location and format of the accesslogfile (Common Logfile Format).

    # If you do not define any access logfileswithin a <VirtualHost>

    # container, they will be logged here.  Contrariwise, if you *do*

    # define per-<VirtualHost> accesslogfiles, transactions will be

    # logged therein and *not* in this file.

    #

    CustomLog "logs/access_log"common

    #

    # If you prefer a logfile with access,agent, and referer information

    # (Combined Logfile Format) you can use thefollowing directive.

    #

    #CustomLog "logs/access_log"combined

</IfModule>

<IfModulealias_module>

    #

    # Redirect: Allows you to tell clientsabout documents that used to

    # exist in your server's namespace, but donot anymore. The client

    # will make a new request for the documentat its new location.

    # Example:

    # Redirect permanent /foohttp://www.example.com/bar

    #

    # Alias: Maps web paths into filesystempaths and is used to

    # access content that does not live underthe DocumentRoot.

    # Example:

    # Alias /webpath /full/filesystem/path

    #

    # If you include a trailing / on /webpaththen the server will

    # require it to be present in the URL.  You will also likely

    # need to provide a <Directory>section to allow access to

    # the filesystem path.

    #

    # ScriptAlias: This controls whichdirectories contain server scripts.

    # ScriptAliases are essentially the same asAliases, except that

    # documents in the target directory aretreated as applications and

    # run by the server when requested ratherthan as documents sent to the

    # client. The same rules about trailing "/" apply to ScriptAlias

    # directives as to Alias.

    #

    ScriptAlias /cgi-bin/"/usr/ecp/apache/cgi-bin/"

</IfModule>

<IfModulecgid_module>

    #

    # ScriptSock: On threaded servers,designate the path to the UNIX

    # socket used to communicate with the CGIdaemon of mod_cgid.

    #

    #Scriptsock cgisock

</IfModule>

#

#"/usr/ecp/apache/cgi-bin" should be changed to whatever yourScriptAliased

# CGIdirectory exists, if you have that configured.

#

<Directory"/usr/ecp/apache/cgi-bin">

    AllowOverride None

    Options None

    Require all granted

</Directory>

<IfModulemime_module>

    #

    # TypesConfig points to the file containingthe list of mappings from

    # filename extension to MIME-type.

    #

    TypesConfig conf/mime.types

    #

    # AddType allows you to add to or overridethe MIME configuration

    # file specified in TypesConfig forspecific file types.

    #

    #AddType application/x-gzip .tgz

    #

    # AddEncoding allows you to have certainbrowsers uncompress

    # information on the fly. Note: Not allbrowsers support this.

    #

    #AddEncoding x-compress .Z

    #AddEncoding x-gzip .gz .tgz

    #

    # If the AddEncoding directives above arecommented-out, then you

    # probably should define those extensionsto indicate media types:

    #

    AddType application/x-compress .Z

    AddType application/x-gzip .gz .tgz

    #

    # AddHandler allows you to map certain fileextensions to "handlers":

    # actions unrelated to filetype. These canbe either built into the server

    # or added with the Action directive (seebelow)

    #

    # To use CGI scripts outside ofScriptAliased directories:

    # (You will also need to add"ExecCGI" to the "Options" directive.)

    #

    #AddHandler cgi-script .cgi

    # For type maps (negotiated resources):

    #AddHandler type-map var

 

    #

    # Filters allow you to process contentbefore it is sent to the client.

    #

    # To parse .shtml files for server-sideincludes (SSI):

    # (You will also need to add"Includes" to the "Options" directive.)

    #

    #AddType text/html .shtml

    #AddOutputFilter INCLUDES .shtml

</IfModule>

#

# Themod_mime_magic module allows the server to use various hints from the

#contents of the file itself to determine its type.  The MIMEMagicFile

#directive tells the module where the hint definitions are located.

#

#MIMEMagicFileconf/magic

 

#

#Customizable error responses come in three flavors:

# 1)plain text 2) local redirects 3) external redirects

#

# Someexamples:

#ErrorDocument500 "The server made a boo boo."

#ErrorDocument404 /missing.html

#ErrorDocument404 "/cgi-bin/missing_handler.pl"

#ErrorDocument402 http://www.example.com/subscription_info.html

#

#

#MaxRanges: Maximum number of Ranges in a request before

#returning the entire resource, or one of the special

# values'default', 'none' or 'unlimited'.

#Default setting is to accept 200 Ranges.

#MaxRangesunlimited

#

#EnableMMAP and EnableSendfile: On systems that support it,

#memory-mapping or the sendfile syscall may be used to deliver

#files.  This usually improves serverperformance, but must

# beturned off when serving from networked-mounted

#filesystems or if support for these functions is otherwise

# brokenon your system.

#Defaults: EnableMMAP On, EnableSendfile Off

#

#EnableMMAPoff

#EnableSendfileon

#Supplemental configuration

#

# Theconfiguration files in the conf/extra/ directory can be

#included to add extra features or to modify the default configuration of

# theserver, or you may simply copy their contents here and change as

#necessary.

#Server-pool management (MPM specific)

#Includeconf/extra/httpd-mpm.conf

#Multi-language error messages

#Includeconf/extra/httpd-multilang-errordoc.conf

# Fancydirectory listings

#Includeconf/extra/httpd-autoindex.conf

 

#Language settings

#Includeconf/extra/httpd-languages.conf

 

# Userhome directories

#Includeconf/extra/httpd-userdir.conf

 

#Real-time info on requests and configuration

#Includeconf/extra/httpd-info.conf

 

#Virtual hosts

#Includeconf/extra/httpd-vhosts.conf

 

# Localaccess to the Apache HTTP Server Manual

#Includeconf/extra/httpd-manual.conf

 

#Distributed authoring and versioning (WebDAV)

#Includeconf/extra/httpd-dav.conf

 

#Various default settings

#Includeconf/extra/httpd-default.conf

 

#Configure mod_proxy_html to understand HTML4/XHTML1

<IfModuleproxy_html_module>

Includeconf/extra/proxy-html.conf

</IfModule>

 

# Secure(SSL/TLS) connections

#Includeconf/extra/httpd-ssl.conf

#

# Note:The following must must be present to support

#       starting without SSL on platforms withno /dev/random equivalent

#       but a statically compiled-in mod_ssl.

#

<IfModulessl_module>

SSLRandomSeedstartup builtin

SSLRandomSeedconnect builtin

</IfModule>

#

#uncomment out the below to deal with user agents that deliberately

#violate open standards by misusing DNT (DNT *must* be a specific

#end-user choice)

#

#<IfModulesetenvif_module>

#BrowserMatch"MSIE 10.0;" bad_DNT

#</IfModule>

#<IfModuleheaders_module>

#RequestHeaderunset DNT env=bad_DNT

#</IfModule>

Include "conf/mod_jk.conf"

3.4.2安装mod_jk.so

解压tomcat-connectors-1.2.37-src.tar.gz,到解压目录下的tomcat-connectors-1.2.37-src\native中,运行如下命令:./configure --with-apxs=/usr/ecp/apache/bin/apxs

上面命令运行完后再运行:make && makeinstall

运行完上面命令后,mod_jk.so将自动安装到Apache的modules目录中,如果Apache的modules中没找到mod_jk.so,请到tomcat-connectors-1.2.37-src /native/apache-2.0/目录下查找,并复制到Apache的modules目录中即可。

 

3.4.3建立mod_jk.conf文件

主要定义mod_jk模块的位置以及mod_jk模块的连接日志设置,还有定义worker.properties文件的位置。

vi /usr/ecp/apache/conf/mod_jk.conf

粘贴如下代码:

LoadModule jk_module modules/mod_jk.so

JkWorkersFile conf/workers.properties

#JkMountFile conf/uriworkermap.properties

JkLogFile logs/mod_jk.log

JkLogLevel info

JkMount /* controller

JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"

JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

JkRequestLogFormat "%w %V %T"

JkShmFile logs/jk.shm

HostnameLookups Off

3.4.4建立workers.properties文件

定义worker的参数,主要是连接tomcat主机的地址和端口信息。如果Tomcat与apache不在同一台机器上,或者需要做多台机器上tomcat的负载均衡只需要更改workers.properties文件中的相应定义即可。

vi /usr/ecp/apache/conf/workers.properties

粘贴如下代码:

worker.list=controller,tomcat2,tomcat3,tomcat4,tomcat5,tomcat6,tomcat7,tomcat8

 

worker.tomcat2.port=8009

worker.tomcat2.host=192.168.183.110

worker.tomcat2.type=ajp13

worker.tomcat2.lbfactor=1

 

worker.tomcat3.port=8009

worker.tomcat3.host=192.168.183.104

worker.tomcat3.type=ajp13

worker.tomcat3.lbfactor=1

 

worker.tomcat4.port=8009

worker.tomcat4.host=192.168.183.105

worker.tomcat4.type=ajp13

worker.tomcat4.lbfactor=1

 

 

worker.tomcat5.port=8009

worker.tomcat5.host=192.168.183.106

worker.tomcat5.type=ajp13

worker.tomcat5.lbfactor=1

 

worker.tomcat6.port=8009

worker.tomcat6.host=192.168.183.107

worker.tomcat6.type=ajp13

worker.tomcat6.lbfactor=1

 

worker.tomcat7.port=8009

worker.tomcat7.host=192.168.183.108

worker.tomcat7.type=ajp13

worker.tomcat7.lbfactor=1

 

worker.tomcat8.port=8009

worker.tomcat8.host=192.168.183.109

worker.tomcat8.type=ajp13

worker.tomcat8.lbfactor=1

 

worker.controller.type=lb

worker.retries=3

worker.controller.balanced_workers=tomcat2,tomcat3,tomcat4,tomcat5,tomcat6,tomcat7,tomcat8

worker.controller.sticky_session=false

worker.controller.sticky_session=1

worker.list=jk-watch

worker.jk-watch.type=status

worker.jk-watch.read_only=True

worker.jk-watch.mount=/user/status/jk

worker.list=jk-manage

worker.jk-manage.type=status

worker.jk-manage.mount=/admin/status/jk

说明:此文件配置了7个tomcat服务器进行负载均衡处理,sticky_session表示是否将对SESSIONID的请求路由回到相同的Tomcat worker。如果属性值不为0,它将被设置为JK_TRUE,session将是粘性的,即SESSION ID的请求路由回到相同的Tomcatworker;当Tomcat正使用能够跨越多个Tomcat实例持久化session数据的Session Manager时,它将被设置为JK_FALSE。我们服务器上用不到session复制,用的是cookie的形式,所以这块注释掉不用配置。

3.5综合测试Apache + Tomcat

到目前为止,Apache和Tomcat的配置就算完成了,我们依次启动Apache和Tomcat群,在浏览器中输入http://服务器IP,如果能正确显示Tomcat首页这就集成成功了。期间,如果我们反复刷新浏览器,看到页面交替显示“First Tomcat”和“Second Tomcat”——可参考图示,那么Tomcat的负载均衡就算配置成功!

3.6 session同步与会话保持

     负载均衡中需要实现会话的保持,即集群中session复制的话,会消耗很大的内存,我们项目采用的处理方法是:当一个用户登录或者打开该网页(就会将请求转发至后台一个服务器),以后所有的操作都会定位到该服务器,而不是发送到其他服务器。假如该服务器挂了,刷新页面就会定位到其他服务器。

具体的配置如下所示:

[root@localhost ~]# cat/usr/ecp/apache/conf/workers.properties

 

worker.list=controller,tomcat2,tomcat3,tomcat4,tomcat5,tomcat6,tomcat7,tomcat8

#worker.list=controller,tomcat3,tomcat4

 

worker.tomcat2.socket_keepalive=1

worker.tomcat2.socket_timeout=180

worker.tomcat2.connect_timeout=0

worker.tomcat2.reply_timeout=0

worker.tomcat2.recovery_options=1

worker.tomcat2.retries=1

worker.tomcat2.port=8009

worker.tomcat2.host=192.168.183.110

worker.tomcat2.type=ajp13

worker.tomcat2.lbfactor=1

 

worker.tomcat3.socket_keepalive=1

worker.tomcat3.socket_timeout=180

worker.tomcat3.connect_timeout=0

worker.tomcat3.reply_timeout=0

worker.tomcat3.recovery_options=1

worker.tomcat3.retries=1

worker.tomcat3.port=8009

worker.tomcat3.host=192.168.183.104

worker.tomcat3.type=ajp13

worker.tomcat3.lbfactor=1

 

worker.tomcat4.socket_keepalive=1

worker.tomcat4.socket_timeout=180

worker.tomcat4.connect_timeout=0

worker.tomcat4.reply_timeout=0

worker.tomcat4.recovery_options=1

worker.tomcat4.retries=1

worker.tomcat4.port=8009

worker.tomcat4.host=192.168.183.105

worker.tomcat4.type=ajp13

worker.tomcat4.lbfactor=1

 

worker.tomcat5.socket_keepalive=1

worker.tomcat5.socket_timeout=180

worker.tomcat5.connect_timeout=0

worker.tomcat5.reply_timeout=0

worker.tomcat5.recovery_options=1

worker.tomcat5.retries=1

worker.tomcat5.port=8009

worker.tomcat5.host=192.168.183.106

worker.tomcat5.type=ajp13

worker.tomcat5.lbfactor=1

 

worker.tomcat6.socket_keepalive=1

worker.tomcat6.socket_timeout=180

worker.tomcat6.connect_timeout=0

worker.tomcat6.reply_timeout=0

worker.tomcat6.recovery_options=1

worker.tomcat6.retries=1

worker.tomcat6.port=8009

worker.tomcat6.host=192.168.183.107

worker.tomcat6.type=ajp13

worker.tomcat6.lbfactor=1

 

worker.tomcat7.socket_keepalive=1

worker.tomcat7.socket_timeout=180

worker.tomcat7.connect_timeout=0

worker.tomcat7.reply_timeout=0

worker.tomcat7.recovery_options=1

worker.tomcat7.retries=1

worker.tomcat7.port=8009

worker.tomcat7.host=192.168.183.108

worker.tomcat7.type=ajp13

worker.tomcat7.lbfactor=1

 

worker.tomcat8.socket_keepalive=1

worker.tomcat8.socket_timeout=180

worker.tomcat8.connect_timeout=0

worker.tomcat8.reply_timeout=0

worker.tomcat8.recovery_options=1

worker.tomcat8.retries=1

worker.tomcat8.port=8009

worker.tomcat8.host=192.168.183.109

worker.tomcat8.type=ajp13

worker.tomcat8.lbfactor=1

 

 

worker.controller.type=lb

worker.retries=3

 

 

worker.controller.balanced_workers=tomcat2,tomcat3,tomcat4,tomcat5,tomcat6,tomcat7,tomcat8

#worker.controller.balanced_workers=tomcat3,tomcat4

 

#worker.controller.sticky_session=false

 

worker.controller.sticky_session=1

 

worker.list=jk-watch

worker.jk-watch.type=status

worker.jk-watch.read_only=True

worker.jk-watch.mount=/user/status/jk

worker.list=jk-manage

worker.jk-manage.type=status

worker.jk-manage.mount=/admin/status/jk

注意此处的workers.properties和前面配置的不一样:

修改tomcat的server.xml文件:

将括号中的删丢(<!--<Enginename="Catalina" defaultHost="localhost">-->),增加下面一句:

<Engine jvmRoute="tomcat4"name="Catalina" defaultHost="localhost"debug="0">

(注意:此处的tomcat4.。。。。需要和workers.properties中配置的一样,负责无法会话保持。需要修改集群中每个tomcatserver.xml

参考文件:http://blog.sina.com.cn/s/blog_4b4a8b430100exhn.html

 

4 安装中出现的问题

4.1 安装apache出错

   [root@localhosthttpd-2.4.7]# ./configure --prefix=/usr/ecp/apache --enable-so--enable-mods-shared=all --enable-deflate --enable-rewrite --enable-proxy--enable-proxy-connect --enable-proxy-ftp --enable-proxy-http--enable-proxy-scgi --enable-proxy-ajp --enable-proxy-balance

checking for chosen layout... Apache

checking for working mkdir -p... yes

checking for grep that handles long linesand -e... /bin/grep

checking for egrep... /bin/grep -E

checking build system type...x86_64-unknown-linux-gnu

checking host system type...x86_64-unknown-linux-gnu

checking target system type...x86_64-unknown-linux-gnu

configure:

configure: Configuring Apache PortableRuntime library...

configure:

checking for APR... no

configure:error: APR not found.  Please read thedocumentation.

没有安装apr相关的包;

Apache遇到的问题:APR not found

#./configure --prefix……检查编辑环境时出现:

checking for APR... no
configure: error: APR not found .  Please read thedocumentation.

可以用./configure–help | grep apr 查看帮助。
--with-included-apr     Use bundled copies of APR/APR-Util
--with-apr=PATH         prefix forinstalled APR or the full path to apr-config
--with-apr-util=PATH    prefix for installed APU or the fullpath to
安装APR(ApachePortable Runtime )
下载:http://apr.apache.org/download.cgi

#cd /tmp/52lamp/ //源码存放位置
#tar -zxvf apr-1.4.2.tar.gz //unzip -o apr-1.4.2.zip
#cd apr-1.4.2
#./configure
#make
#make install

再次检查编译环境出现

checking for APR-util... no
configure: error: APR-util not found .  Please read thedocumentation.

#./configure –help | grepapr-util
--with-apr-util=PATH    prefix for installed APU or the fullpath to

下载:http://download.chinaunix.net/download/0001000/472.shtml
#tar -zxvf apr-util-1.3.9.tar.gz
#cd apr-util-1.3.9
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
#make
#make install

./configure仍提示APR-util not found,增加--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util后出现
configure: error: pcre-config for libpcre not found. PCRE is required andavailable from 
http://pcre.org/

#./configure –help | grep pcre
--with-pcre=PATH        Use external PCRElibrary

下载:http://sourceforge.net/projects/pcre
#unzip -o pcre-8.10.zip
#cd pcre-8.10
#./configure --prefix=/usr/local/pcre
#make
#make install

继续安装Apache/httpd./configure时加上参数 --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/--with-pcre=/usr/local/pcre,这个问题就解决了

注意:Apache在安装时不会检查参数是否正确,错误的参数会直接被丢弃,不会报告给用户。但可以使用echo $?命令检查是否有错误,当输出结果为0时表示没有错误。

#echo $?
0

#make
#make install

复制Apache启动文件
#cp /usr/local/httpd/bin/apachectl /sbin/

启动Apache
#apachectl start

设置Apache开机自启动
#vi /etc/rc.d/rc.local
增加一行/sbin/apachectl start

或者将httpd服务添加到ntsysv服务管理工具
#apachectl stop //
关闭Apache以免不必要的麻烦
#cp /usr/local/httpd/bin/apachectl /etc/rc.d/init.d/httpd
#vi /etc/rc.d/init.d/httpd
修改为
#!/bin/sh
#
#chkconfig: 345 85 15 //#
不能省略,注意空格
#description: httpd for 52lamp 20101016 21:54 //
任意字符串
#
......

第二行中345的含义:
#       0 - operation completed successfully
#       1 -
#       2 - usage error
#       3 - httpd could not be started
#       4 - httpd could not be stopped
#       5 - httpd could not be started during arestart

修改有关权限
#cd /etc/rc.d/init.d/
#chmod a+x httpd
#chkconfig --add httpd

#ntsysv
httpd
已经在列表中,按F1可以看到刚才编写的服务描述httpd for 52lamp 20101016 21:54

#apachectl start
#ps -e |grep httpd
23247 ?        00:00:00 httpd
23248 ?        00:00:00 httpd
23249 ?        00:00:00 httpd
23251 ?        00:00:00 httpd
23252 ?        00:00:00 httpd

在浏览器中输入127.0.0.1,看起来一切正常;但是局域网内其他电脑不能访问!

#service iptables stop

如果不想关闭防火墙,放开80端口即可。

#vi /etc/sysconfig/iptables
增加一行-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
#service iptables restart //
重启防火墙

 

4.2 启动apache报错

[root@localhost ~]#/usr/ecp/apache/bin/apachectl -k start

(98)Address already in use: AH00072:make_sock: could not bind to address [::]:80

(98)Address already in use: AH00072:make_sock: could not bind to address 0.0.0.0:80

no listening sockets available, shuttingdown

AH00015: Unable to open logs

端口80被占用;

[root@localhost logs]# cat error_log

[Tue Dec 17 21:12:10.868177 2013][proxy_balancer:emerg] [pid 14458:tid 46977481064432] AH01177: Failed to lookupprovider 'shm' for 'slotmem': is mod_slotmem_shm loaded??

[Tue Dec 17 21:12:10.868303 2013] [:emerg][pid 14458:tid 46977481064432] AH00020: Configuration Failed, exiting

需要将httpd.conf中的前面说的那个mod_slotmem_shm放开;


0 0
原创粉丝点击