Linux下面配置Apache2 + tomcat7.0 + ssl 集群安装与配置

      可以通过apache proxy代理或mod_jk.mo模块实现tomcat集群的配置.

一、apache2.4的安装

1、下载apache2.4源码

http://apache.fayea.com//httpd/httpd-2.4.18.tar.gz

把源码保存在目录/usr/local/src目录下

进入目录/usr/local/src

cd /usr/local/src

解压源码:tax -xf httpd-2.4.18.tar.gz

下载apr,apr-util, pcre源码至/usr/local/src 目录下

wget http://archive.apache.org/dist/apr/apr-1.4.5.tar.gz
wget http://archive.apache.org/dist/apr/apr-util-1.3.12.tar.gz
wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.10/pcre-8.10.zip

解压相关的源码至/usr/local/src目录下。运行以下命令

./configure --prefix /usr/local/apache2 --with-apr=/usr/local/src/apr --with-apr-util=/usr/local/src/apr-util/ --with-pcre=/usr/local/src/pcre

2、安装apache2.4

进入apache2的源码目录:

#make install

3、运行apache2

安装完的apache2存入于目录/usr/local/apache2下面

#cd /usr/local/apache2/bin

#sudo apachectl start

在浏览器中运行 http://localhost

显示It Works表示安装成功。

二、下载 tomcat7

http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.67/bin/apache-tomcat-7.0.67.tar.gz

解压到目录/usr/local/

#tar -xf apache-tomcat-7.0.67.tar.gz

改变目录名为:tomcat7

#sudo mv apache-tomcat-7.0.64 tomcat7

复制多一个tomcat目录

#sudo cp tomcat7 tomcat71

三、配置tomcat

进入tomcat其中的一个目录,修改配置文件:conf/server.xml

#sudo vim conf/server.xml

修改如下内容:增加jvmRoute="jvm1"

<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1" >

增加如下内容实现集群和Session的复制功能:

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> 

	<Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> 

	<Channel className="org.apache.catalina.tribes.group.GroupChannel"> 

		<Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000"/> 

		<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> 

		<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> 

			<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> 

		</Sender> <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> 

		<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> 

	</Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> 

	<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> 

	<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" 

		tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" 

		watchDir="/tmp/war-listen/" watchEnabled="false"/> 

	<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> 

	<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> 

</Cluster>

保存,把此文件拷贝覆盖到另一tomcat的配置文件:tomcat71/conf/server.xml。

打开另一tomcat的配置文件,修改以下相关内容,以免端口冲突。

<Server port="9005" shutdown="SHUTDOWN">

    <Connector port="9080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="9443" />

   <Connector port="9009" protocol="AJP/1.3" redirectPort="9443" />

<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2" >

 四、以代理方式配置集群

1、编辑httpd.conf配置文件

#vim conf/httpd.conf增加以下模块(把相关注释去掉):

LoadModule authn_socache_module modules/mod_authn_socache.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule speling_module modules/mod_speling.so
LoadModule rewrite_module modules/mod_rewrite.so
 

把Servername改成如下:

ServerName 127.0.0.1:80

# Virtual hosts

去掉下面的注释
Include conf/extra/httpd-vhosts.conf

 在文件的未尾添加如下内容:

ProxyRequests Off
#正向代理需要关闭,主要是为了安全
proxyPass / balancer://tomcatcluster/  
#表示所有请求都交给 mycluster 来处理
ProxyPassReverse / balancer://tomcatcluster/  
#防止内部请求重定向会绕过apache2,这句需要加上
<proxy balancer://tomcatcluster>
     BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=jvm1
     BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=jvm2
</proxy>
 2、编辑conf/extra/httpd-vhosts.conf

 添加如下内容:

<VirtualHost *:80>
         ServerAdmin [email protected]
         ServerName localhost
         ServerAlias localhost
         ProxyPass / balancer://tomcatcluster/ stickysession=jsessionid nofailover=On
         ProxyPassReverse / balancer://tomcatcluster/
         ErrorLog "logs/lbtest-error.log"
         CustomLog "logs/lbtest-access.log" common
</VirtualHost>

 以代理方式配置完成。

五、通过mod_jk.mo模块配置集群

1.下载mod_jk.mo模块

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

解压之后把文件mod_jk.so 拷贝到apache2/modules目录下

2.在apache2/conf/目录下新加文件mod-jk.conf

把以下内容添加到文件mod-jk.conf中

#目录下的文件名保持一致.  
LoadModule jk_module /usr/local/apache2/modules/mod_jk.so  
#指定workers.properties文件目录.  
JkWorkersFile /usr/local/apache2/conf/workers.properties  
#指定哪些请求由tomcat处理,controller为workers.properties文件里指定的负载控制器.  
JkMount /* controller  
#JkMount /*.html controller  
#JkMount /*.jsp controller  
#JkMount /*.do controller  
#JkMount /servlet/* controller  
#JkUnMount /index.html controller

 3.在apache2/conf/目录下新加文件workers.properties

把以下内容添加到文件workers.properties中

增加如下内容:

worker.list = controller      #不指定报错Could not find a worker for worker name=controller  
    #========jvm1========  
    worker.jvm1.port=8009  
    worker.jvm1.host=localhost  
    worker.jvm1.type=ajp13  
    worker.jvm1.lbfactor=1  
    #========jvm2========  
    worker.jvm2.port=9009  
    worker.jvm2.host=localhost  
    worker.jvm2.type=ajp13  
    worker.jvm2.lbfactor=1  
       
    #========controller负载平衡控制器========  
    worker.controller.type=lb                          #指定controller类型  
    worker.controller.balanced_workers=jvm1,jvm2#指定负载平衡的tomcat  
    worker.controller.sticky_session=false              #指定是否粘性session  
    worker.controller.sticky_session_force=false  
    worker.connection_pool_size=3000  
    worker.connection_pool_minsize=50  
    worker.connection_pool_timeout=50000  
    # session配置说明:  
    #当sticky_session,sticky_session_force都为true时不复制session,  
    #sticky_session_force=false指集群中某台服务器多次请求没有响应,则转发到其它服务器处理,  
    #sticky_session=false不使用粘性session,同时配置不复制session时,注意转发请求后可能会找不到原来的session.

4.修改apache2/conf/httpd.conf

在文件的未尾加下如下内容:

include /usr/local/apache2/conf/mod-jk.conf

六、修改应用的web.xml

增加如下内容:

<distributable/>

七、apache配置ssl证书

打开apache配置文件

vim conf/httpd.conf

增加下面模块:

LoadModule ssl_module libexec/apache2/mod_ssl.so

加入如下配置:

Include conf/extra/httpd-ssl.conf

修改extra/httpd-ssl.conf配置

<VirtualHost _default_:443>下增加如下内容:

#ssl也具有集群功能:

JkMount /* controller

#证书及密钥

SSLCertificateFile "/usr/local/ssl/certs/server.cer"

SSLCertificateKeyFile "/usr/local/ssl/private/server-key.pem"

有关证书和密钥的生成,请参考:

 最后,有关的配置文件,参考bak.rar

相关推荐