实验环境:

IP:10.0.0.10

系统:CentOS 6.4

jdk版本:java version "1.8.0_65"

tomcat版本:Apache Tomcat/8.0.24

apache版本:Apache/2.2.27 (Unix)


思路:

  1. 1、安装Apache 

  2. 2、安装tomcat 

  3. 3、安装mod_jk 

  4. 4、加载mod_jk 

  5. 5、关闭tomcat的侦听端口 

  6. 6、应用分离(静态与动态页面分离) 


安装apache

[root@oracle src]# tar xf httpd-2.2.27.tar.gz 

[root@oracle src]# cd httpd-2.2.27

[root@oracle httpd-2.2.27]# ./configure 

--prefix=/usr/local/apache2       ---指定安装路径

--enable-proxy-balancer           ---apache代理balancer模块 

--enable-proxy-http               ---apache代理http模块

--enable-proxy-connect            ---apache 代理连接模块      

--enable-proxy                    ---apache代理模块

--enable-rewrite                   ---基本的URL操纵规则,即启用URL重写规则

--enable-proxy-ajp                 ---apache代理ajp模块


[root@oracle httpd-2.2.27]# mkdir -p /data/{www,wwwroot}   建两个程序家目录


[root@oracle httpd-2.2.27]# make && make install   编译安装

apache配置文件主文件:

[root@oracle ROOT]# cat httpd.conf     

30:ServerRoot "/usr/local/apache2"   ---指定apache程序安装的家目录

41:Listen 80                       ---指定监听端口

56:<IfModule !mpm_netware_module>

57:<IfModule !mpm_winnt_module>

66:User daemon                      ---指定apache运行的用户

67:Group daemon                     ---指定apache运行的组

69:</IfModule>

70:</IfModule>

89:ServerAdmin you@example.com       ---指定网站管理员邮箱

98:ServerName www.example.com:80     ---指定FQDN名

105:DocumentRoot "/data"         ---指定程序家目录

115:<Directory /> 

116:    Options FollowSymLinks

117:    AllowOverride None

118:    Order deny,allow

119:    Deny from all

120:</Directory>

132:<Directory "/data">        ---指定程序家目录

145:    Options Indexes FollowSymLinks

152:    AllowOverride None

157:    Order allow,deny

158:    Allow from all

160:</Directory>

166:<IfModule dir_module>

167:    DirectoryIndex index.html          指定首页文件

168:</IfModule>

174:<FilesMatch "^\.ht">

175:    Order allow,deny

176:    Deny from all

177:    Satisfy All

178:</FilesMatch>

187:ErrorLog "logs/error_log"     ---指定错误日志文件路径

194:LogLevel warn                 ---指定日志级别

196:<IfModule log_config_module>

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

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

204:    <IfModule logio_module>

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

207:    </IfModule>

216:    CustomLog "logs/access_log" common

223:</IfModule>

225:<IfModule alias_module>

252:    ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"

254:</IfModule>

256:<IfModule cgid_module>

262:</IfModule>

268:<Directory "/usr/local/apache2/cgi-bin">

269:    AllowOverride None

270:    Options None

271:    Order allow,deny

272:    Allow from all

273:</Directory>

284:DefaultType text/plain

286:<IfModule mime_module>

291:    TypesConfig conf/mime.types         --指定文件类型

308:    AddType application/x-compress .Z     ---指定压缩类型

309:    AddType application/x-gzip .gz .tgz    ---指定打包类型

332:</IfModule>

396:Include conf/extra/httpd-vhosts.conf       ---此处必须打开,否则无法使用虚拟主机

414:<IfModule ssl_module>

415:SSLRandomSeed startup builtin

416:SSLRandomSeed connect builtin

417:</IfModule>

418:LoadModule jk_module modules/mod_jk.so 

419:JkWorkersFile /usr/local/apache2/conf/workers.properties  --指定负载均衡列表文件

420:JkMountFile   /usr/local/apache2/conf/uriworkermap.properties   --指定文件类型处理

421:JkMountFile   /usr/local/apache2/conf/uriworkermap1.properties  --指定文件类型处理

422:JkLogFile /usr/local/apache2/logs/mod_jk.log                   --指定mod_jk日志文件

423:JkLogLevel info                                                --指定日志级别

424:JkLogStampformat "[%a %b %d %H:%M:%S %Y]"                     ---指定日志格式


[root@oracle extra]# cat httpd-vhosts.conf |grep -vnE "^#|^$"

19:NameVirtualHost *:80

27:<VirtualHost *:80>

28:    ServerAdmin webmaster@dummy-host.example.com

29:    DocumentRoot "/data/www"

30:    ServerName www.test1.com

31:    ServerAlias test1.com

32:    DirectoryIndex index.html

33:    ErrorLog "logs/dummy-test1-error_log"

34:    CustomLog "logs/dummy-test1-access_log" common

35:    

36:   JkMountFile  conf/uriworkermap.properties   指定对于动静态文件处理的程序

43:   

45:</VirtualHost>

47:<VirtualHost *:80>

48:    ServerAdmin webmaster@dummy-host2.example.com     管理员邮箱

49:    DocumentRoot "/data/wwwroot"                      程序加目录

50:    ServerName www.test2.com                          站点名

51:    ServerAlias test2.com                             站点别名

52:     DirectoryIndex index.html                        首页文件

53:    ErrorLog "logs/dummy-test2-error_log"             错误日志

54:    CustomLog "logs/dummy-test2-access_log" common    访问日志

56:JkMountFile  conf/uriworkermap1.properties    指定对于动静态文件处理的程序

57:</VirtualHost>


在虚拟主机目录里面分别建两个index.html文件。并启动apache程序,用域名访问两个网站(注:此处需要在本地做hosts解析)如果可以访问,则apache正常运行了(需关闭防火墙和安全策略)

[root@oracle data]# /usr/local/apache2/bin/apachectl -t     测试配置文件是否Ok             

Syntax OK

[root@oracle data]# /usr/local/apache2/bin/apachectl start   启动apache

[root@oracle data]# cd /data/

[root@oracle data]# cd www

[root@oracle www]# ls

[root@oracle www]# echo "this is test1 web" > index.html

[root@oracle www]# cd ../wwwroot/

[root@oracle wwwroot]# echo "this is test2 web" > index.html 

[root@oracle wwwroot]# ps -ef|grep httpd

root      54795      1  0 11:12 ?        00:00:00 /usr/local/apache2/bin/httpd -k start

daemon    54796  54795  0 11:12 ?        00:00:00 /usr/local/apache2/bin/httpd -k start

daemon    54797  54795  0 11:12 ?        00:00:00 /usr/local/apache2/bin/httpd -k start

daemon    54798  54795  0 11:12 ?        00:00:00 /usr/local/apache2/bin/httpd -k start

daemon    54799  54795  0 11:12 ?        00:00:00 /usr/local/apache2/bin/httpd -k start

daemon    54800  54795  0 11:12 ?        00:00:00 /usr/local/apache2/bin/httpd -k start

root      54803   2708  0 11:15 pts/1    00:00:00 grep httpd

apache安装完毕。


安装jk_mod:

# tar -xzf tomcat-connectors-1.2.32-src.tar.gz 

# cd tomcat-connectors-1.2.32-src/ 

# cd native/ 

# ./configure --with-apxs=/usr/local/apache2/bin/apxs 

# make && make install  

ll /usr/local/apache2/modules/mod_jk.so  

-rwxr-xr-x 1 root root 1076923 Feb 20 19:14 /usr/local/apache2/modules/mod_jk.so 


安装jdk,因为tomcat的运行必须要jdk的支持。

[root@oracle src]# rpm -qa|grep java    ---查看是否安装有jdk且版本是否支持

java-1.5.0-gcj-1.5.0.0-29.1.el6.x86_64

gcc-java-4.4.7-3.el6.x86_64

java_cup-0.10k-5.el6.x86_64

[root@oracle src]# rpm -e --nodeps java-1.5.0-gcj-1.5.0.0-29.1.el6.x86_64 gcc-java-4.4.7-3.el6.x86_64 java_cup-0.10k-5.el6.x86_64   ---如版本过低则卸载


[root@oracle src]# ls

apache-tomcat-8.0.24.zip  httpd-2.2.27.tar.gz  jdk-8u65-linux-x64.rpm  tomcat-connectors-1.2.32-src.tar.gz

[root@oracle src]# rpm -ivh jdk-8u65-linux-x64.rpm 

Preparing...                ########################################### [100%]

   1:jdk1.8.0_65            ########################################### [100%]

Unpacking JAR files...

        tools.jar...

        plugin.jar...

        javaws.jar...

        deploy.jar...

        rt.jar...

        jsse.jar...

        charsets.jar...

        localedata.jar...

        jfxrt.jar...


[root@oracle java]# vim /etc/profile    ---末尾添加

unset i

export JAVA_HOME=/usr/java/jdk1.8.0_65

export PATH=$JAVA_HOME/bin:$PATH:

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

unset -f pathmunge


[root@oracle java]# source /etc/profile

[root@oracle java]# echo $JAVA_HOME

/usr/java/jdk1.8.0_65


解压tomcat程序:

[root@oracle src]# unzip apache-tomcat-8.0.24.zip  



[root@oracle src]# mv apache-tomcat-8.0.24 tomcat1

[root@oracle src]# \cp -r tomcat1 tomcat2

[root@oracle src]# cd /usr/local/src/tomcat1/bin

[root@oracle bin]# chmod +x *.sh


[root@oracle src]# cd /usr/local/src/tomcat2/bin

[root@oracle bin]# chmod +x *.sh


修改tomcat1和tomcat2的service.xml文件

tomcat1的配置文件:

[root@oracle bin]# cat ../conf/server.xml

<?xml version='1.0' encoding='utf-8'?>

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

  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />

  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />

  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />


    <Resource name="UserDatabase" auth="Container"

              type="org.apache.catalina.UserDatabase"

              description="User database that can be updated and saved"

              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

              pathname="conf/tomcat-users.xml" />

  <Service name="Catalina">


    <Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />


    <!-- Define an AJP 1.3 Connector on port 8009 -->

    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

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

      <Realm className="org.apache.catalina.realm.LockOutRealm">

        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"

               resourceName="UserDatabase"/>

      </Realm>


      <Host name="www.test1.com"  appBase="webapps" --->hostname与httpd-vhost里面相对应

            unpackWARs="true" autoDeploy="true">


             <Context path="" docBase="/data/www"/>  --->docBase与虚拟主机里面的DR对应

      </Host>

    </Engine>

  </Service>

</Server>


tomcat2的配置文件:

[root@oracle conf]# cat server.xml|grep -v "^#"

<?xml version='1.0' encoding='utf-8'?>

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

  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />

  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />

  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

    <Resource name="UserDatabase" auth="Container"

              type="org.apache.catalina.UserDatabase"

              description="User database that can be updated and saved"

              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

              pathname="conf/tomcat-users.xml" />

  <Service name="Catalina">

    <Connector port="8081" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

    <!-- Define an AJP 1.3 Connector on port 8009 -->

    <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />

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

      <Realm className="org.apache.catalina.realm.LockOutRealm">

        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"

               resourceName="UserDatabase"/>

      </Realm>

      <Host name="www.test2.com"  appBase="webapps"   --->hostname与httpd-vhost里面相对应

            unpackWARs="true" autoDeploy="true">

     <Context path="" docBase="/data/wwwroot"/>       --->docBase与虚拟主机里面的DR对应

      </Host>

    </Engine>

  </Service>

</Server>



[root@oracle conf]# cat uriworkermap1.properties 

/*=tomcat2          需要给tomcat2处理的文件类型

!/*.jpg=tomcat2     给apache处理的文件类型

!/*.gif=tomcat2  

!/*.png=tomcat2  

!/*.bmp=tomcat2  

!/*.html=tomcat2  

!/*.htm=tomcat2  

!/*.swf=tomcat2  

!/*.css= tomcat2  

!/*.js= tomcat2  


[root@oracle conf]# cat uriworkermap.properties 

/*=tomcat1          需要给tomcat1处理的文件类型

!/*.jpg=tomcat1     给apache处理的文件类型

!/*.gif=tomcat1  

!/*.png=tomcat1  

!/*.bmp=tomcat1  

!/*.html=tomcat1  

!/*.htm=tomcat1  

!/*.swf=tomcat1  

!/*.css= tomcat1  

!/*.js= tomcat1  


[root@oracle conf]# cat workers.properties 

worker.list=tomcat1,tomcat2     指定负载均衡器名称

worker.tomcat1.type=ajp13       协议

worker.tomcat1.host=localhost   主机名

worker.tomcat1.port=8009        端口,必须和tomcat1里面的对应

worker.tomcat1.lbfactor=1       权重


worker.tomcat2.type=ajp13        协议

worker.tomcat2.host=localhost    主机名

worker.tomcat2.port=8019         端口,必须和tomcat2里面的对应

worker.tomcat2.lbfactor=1        权重


上传网站网站即可,重启apache与tomcat1和tomcat2

www.test1.com效果:

apache代理多个tomcat实例_动态www.test2.com效果:

apache代理多个tomcat实例_java_02