Apache + Tomcat + jk配置负载均衡以及虚拟主机
实现多域名、多用户同时使用
转自:http://zouzaibianyuan.iteye.com/blog/1633146
一、Apache2.2.4配置多个虚拟主机
1、在httpd.conf文件中,找到下面这一行,把前面的#号去掉,启用http-vhosts.conf 配置文件:
Include conf/extra/httpd-vhosts.conf
2、找到 conf/extra/httpd-vhosts.conf 配置文件,做如下配置:
<VirtualHost *:80>
ServerAdminwebmaster@dummy-host.e.g.somenet.com
DocumentRoot"D:\apps\quickLMS"
ServerName quick.edusaas.com
JkMount /* controller
ErrorLog logs/quick-error_log
CustomLoglogs/quick-access_log common
DirectoryIndex index.jsp
<Directory"D:\apps\quickLMS">
AllowOverrideNone
OptionsFollowSymLinks
Orderallow,deny
Allowfrom all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdminwebmaster@dummy-host.e.g.somenet.com
DocumentRoot"D:\apps\sanyLMS"
ServerName sany.edusaas.com
JkMount /* controller
ErrorLog logs/sany-error_log
CustomLoglogs/sany-access_log common
DirectoryIndex index.jsp
<Directory"D:\apps\sanyLMS">
AllowOverrideNone
OptionsFollowSymLinks
Orderallow,deny
Allowfrom all
</Directory>
</VirtualHost>
#一个虚拟主机,对应一个 <VirtualHost*:80>......</VirtualHost> 节点。
二、Tomcat配置多个虚拟主机
1、在Tomcat的server.xml配置文件中,增加Host节点
<Hostname="quick.edusaas.com" appBase="D:\apps\quickLMS" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Contextpath="" docBase="." />
</Host>
<Hostname="sany.edusaas.com" appBase="D:\apps\sanyLMS" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Contextpath="" docBase="." />
</Host>
三、Apache 整合 Tomcat
1、下载 mod_jk.so ,放到 modules 目录下,
下载地址http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/
下载时,注意 mod_jk和 apache 的版本,选择对应的版本。
1、在Apache的 conf 目录下 新建 workers.properties 配置文件,做如下配置:
worker.list=controller,tomcat1,tomcat2,status
#==========tomcat1============
worker.tomcat1.port=8119
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#==========tomcat2============
worker.tomcat2.port=8219
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
#==========controller 负载均衡控制器=========
worker.controller.type=lb
worker.controller.balance_workers=tomcat1,tomcat2
worker.controller.sticky_session=1
worker.status.type=status
#tomcat 可以配置多个,这里的配置有点类似Apache+ Tomcat 整合集群配置。
2、在Apache的 conf 目录下 新建 uriworkermap.properties 配置文件,做如下配置:
/*=controller
/jkstatus=status
!/*.gif=controller
!/*.jpg=controller
!/*.png=controller
!/*.css=controller
!/*.js=controller
!/*.htm=controller
!/*.html=controller
#这里设置的是过滤规则,哪些资源文件交给controller处理,剩下的由Apache处理。
3、配置Apache的 conf 目录下 httpd.conf 配置文件,在末尾增加如下配置:
LoadModule jk_modulemodules\mod_jk-apache-2.2.4.so
<IfModule jk_module>
JkWorkersFile conf\workers.properties
JkMountFile conf\uriworkermap.properties
JkLogFile logs\mod_jk.log
JkLogLevel warn
</IfModule>
四、配置完成,启动Tomcat 和 Apache,就可以通过以下网址访问:
quick.edusaas.com
sany.edusaas.com
zohl.edusaas.com
而且在访问 jsp、 *.do、servlet等地址时,也不用加tomcat的端口号了。
测试时,如果没有实际两个域名,可以将上面的测试域名配置到系统的 C:\Windows\System32\drivers\etc\hosts 文件中:
127.0.0.1 quick.edusaas.com
127.0.0.1 sany.edusaas.com
127.0.0.1 zohl.edusaas.com
这样在地址栏可以输入上面的域名访问测试站点。
下面是我的配置
安装完Apache和tomcat后直接操作:
1.配置Apache
在httpd.conf文件最下方添加以下代码:
Include conf/extra/httpd-vhosts.conf
LoadModule jk_module modules\mod_jk.so
JkWorkersFile conf\workers.properties
JkMountFile conf\uriworkermap.properties
JkLogFile logs\mod_jk.log
JkLogLevel warn
在httpd-vhosts.conf文件最下方添加以下代码:
<VirtualHost *:80>
ServerAdminwebmaster@dummy-host.e.g.somenet.com
# 项目目录
DocumentRoot"E:\Workspaces\MyEclipse 10\hjy\WebRoot"
# 主机域名
ServerNamequick.edusaas.com
# 分发控制器
JkMount /* controller1
ErrorLog logs/quick-error_log
CustomLog logs/quick-access_log common
DirectoryIndexindex.jsp
# 项目目录
<Directory "E:\Workspaces\MyEclipse 10\hjy\WebRoot">
AllowOverride None
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdminwebmaster@dummy-host.e.g.somenet.com
# 项目目录
DocumentRoot "E:\Workspaces\MyEclipse 10\WeixinJava\WebRoot"
# 主机域名
ServerNamesany.edusaas.com
# 分发控制器
JkMount /* controller2
ErrorLog logs/sany-error_log
CustomLog logs/sany-access_log common
DirectoryIndexindex.jsp
# 项目目录
<Directory "E:\Workspaces\MyEclipse10\WeixinJava\WebRoot">
AllowOverride None
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
新建workers.properties文件,添加以下内容
worker.list=controller1,controller2,tomcat1,tomcat2,status
#==========tomcat1============
worker.tomcat1.port=9109
worker.tomcat1.host=localhost1
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#==========tomcat2============
worker.tomcat2.port=9209
worker.tomcat2.host=localhost2
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
#==========controller 负载均衡控制器=========
worker.controller1.type=lb
worker.controller1.balance_workers=tomcat1
worker.controller1.sticky_session=1
worker.controller2.type=lb
worker.controller2.balance_workers=tomcat2
worker.controller2.sticky_session=1
worker.status.type=status
#tomcat 可以配置多个,这里的配置有点类似Apache +Tomcat 整合集群配置。
新建uriworkermap.properties文件,添加以下内容
/*=controller1
/jkstatus=status
!/*.gif=controller1
!/*.jpg=controller1
!/*.png=controller1
!/*.css=controller1
!/*.js=controller1
!/*.htm=controller1
!/*.html=controller1
#这里设置的是过滤规则,哪些资源文件交给controller处理,剩下的由Apache处理。
2.在hosts文件中添加以下内容
127.0.0.1 localhost1
127.0.0.1 localhost2
# 测试域名,若有真实可用的域名,可省略下面两行
127.0.0.1 quick.edusaas.com
127.0.0.1 sany.edusaas.com
3.配置tomcat1
打开server.xml文件
分别找到以下内容
a.<Server port="8005"shutdown="SHUTDOWN">
b.<Connector port="8080"protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"/>
c.<Connector port="8009"protocol="AJP/1.3" redirectPort="8443" />
d.<Engine name="Catalina" defaultHost="localhost">
分别替换为
a.<Server port="9105" shutdown="SHUTDOWN">
b.<Connector port="9180" protocol="HTTP/1.1"
connection Timeout="20000"
redirectPort="8443"/>
c.<Connector port="9109" protocol="AJP/1.3" redirectPort="8443" />
d.<Engine name="Catalina" defaultHost="localhost1" jvmRoute="tomcat1">
并在Engine标签下添加以下内容
<Host name="quick.edusaas.com" appBase="E:\Workspaces\MyEclipse 10" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Contextpath="/hjy" docBase="hjy\WebRoot" />
</Host>
4.配置tomcat2
打开server.xml文件
分别找到以下内容
a.<Server port="8005"shutdown="SHUTDOWN">
b.<Connector port="8080"protocol="HTTP/1.1"
connection Timeout="20000"
redirectPort="8443" />
c.<Connector port="8009"protocol="AJP/1.3" redirectPort="8443" />
d.<Engine name="Catalina"defaultHost="localhost">
分别替换为
a.<Server port="9205"shutdown="SHUTDOWN">
b.<Connector port="9280"protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"/>
c.<Connector port="9209"protocol="AJP/1.3" redirectPort="8443" />
d.<Engine name="Catalina"defaultHost="localhost2" jvmRoute="tomcat2">
并在Engine标签下添加以下内容
<Host name="sany.edusaas.com" appBase="E:\Workspaces\MyEclipse10" unpackWARs="true" autoDeploy="true"xmlValidation="false" xmlNamespaceAware="false">
<Contextpath="/ wj" docBase="wj\WebRoot"/>
</Host>
这样就完成了我需要的配置,两个tomcat分别对应两个域名,两个项目,访问时同时经过Apache的80端口,分发给两个tomcat处理。
http://quick.edusaas.com/hjy/ 访问的是hjy项目,由tomcat1处理
http://sany.edusaas.com/wj/ 访问的是wj项目,由tomcat2处理
注:tomcat可以配置多个,配置同上,a.b.c.需配置不同端口。若d.和Engine标签配置相同,则可构成负载均衡。具体tomcat数量根据服务器性能自行决定。