整合apache和tomcat

2015-05-14 11:47

运行环境:

CentOS release 6.6
# uname -sr
Linux 2.6.32-504.el6.x86_64
 
#java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
 
# ./apachectl -v
Server version: Apache/2.2.29 (Unix)
Server built:   Apr 29 2015 14:30:50


1. 安装java环境--jdk

# cd /opt/
# wget -chttp://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-linux-x64.tar.gz
# tar zxf jdk-8u45-linux-x64.gz -C/usr/local/
#ln -snv  /usr/local/jdk1.8.0_45/usr/local/java


2.环境变量配置

# vim /etc/profile.d/java.sh
    exportJAVA_HOME=/usr/local/java/
    exportCLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    exportPATH=$PATH:$JAVA_HOME/bin
         
导出环境变量
# source /etc/profile.d/java.sh

3.验证java是否安装成功

# java -version
java version "1.8.0_45"

4.安装tomcat

# cd /opt/
# tar zxf apache-tomcat-8.0.21.tar.gz -C/usr/local/
# ln -snv /usr/local/apache-tomcat-8.0.21/usr/local/tomcat
# chown -R web:web tomcat
# vim /etc/profile.d/tomcat.sh
         exportCATALINA_HOME=/usr/local/tomcat
         exportPATH=$PATH:$CATALINA_HOME/bin
        
#source /etc/profile.d/tomcat.sh
修改server.xml
<?xml version='1.0'encoding='utf-8'?>
 
<Server port="8005"shutdown="SHUTDOWN">
 <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
 <!--APR library loader. Documentation at /docs/apr.html -->
 <ListenerclassName="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on"/>
 <!-- Prevent memory leaks due to use of particular java/javaxAPIs-->
 <ListenerclassName="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
 <ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
 <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
 
 <GlobalNamingResources>
 
   <Resource name="UserDatabase" auth="Container"
             type="org.apache.catalina.UserDatabase"
              description="User databasethat can be updated and saved"
             factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
             pathname="conf/tomcat-users.xml" />
 </GlobalNamingResources>
 
 <Service name="Catalina">
 
   <Connector port="8080" protocol="HTTP/1.1"
              connectionTimeout="20000"
               maxThreads="8192"
               compression="on"
              compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/css"
              redirectPort="8443"/>
 
   <!-- Define an AJP 1.3 Connector on port 8009 -->
   <Connector port="8009"
                       maxHttpHeaderSize="8192"
                       maxThreads="8192" 
                       minSpareThreads="256"
                       maxSpareThreads="1024"
                       acceptCount="2048"  
                       connectionTimeout="20000"
                       enableLookups="false"
                       compression="on" 
                       compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/css"
                       protocol="AJP/1.3"
                       redirectPort="8443"/>
                       
    <Engine name="Catalina" defaultHost="localhost">
     <RealmclassName="org.apache.catalina.realm.LockOutRealm">
       <RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"
              resourceName="UserDatabase"/>
     </Realm>
 
     <Host name="localhost" appBase="/data/tomcat/webapp"
           unpackWARs="true" autoDeploy="true">
 
       <ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs"
              prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t&quot;%r&quot; %s %b" />
 
     </Host>
   </Engine>
 </Service>
</Server>

5.配置apache通过mod_jk模块与Tomcat连接

安装mod_jk
# cd /opt/
# wget -chttp://mirrors.cnnic.cn/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.40-src.tar.gz
# tar zxftomcat-connectors-1.2.37-src.tar.gz
# cd tomcat-connectors-1.2.37-src/native/
# ./configure --with-apxs=/usr/local/apache2/bin/apxs
# make && make install
配置基于mod_jk的负载均衡,修改httpd.conf加载mod_jk模块,启用mod_jk
# vim /usr/local/apache/conf/httpd.conf
         LoadModule       jk_module        modules/mod_jk.so
         <IfModulemod_jk.c>
                  JkWorkersFile  /usr/local/apache/conf/workers.properties
                  JkLogFile  /usr/local/apache/logs/mod_jk.log
                  JkLogLevel  warn
                  JkOptions +ForwardKeySize+ForwardURICompat -ForwardDirectories
                  JkShmFile  /usr/local/apache/logs/mod_jk.shm
         </IfModule>
 
Include     /usr/local/apache/conf/extra/vhost/test.conf
创建tomcat负载配置属性文件
# vim /usr/local/apache/conf/workers.properties
         worker.list=ajp13w,wlb,jkstatus 
         worker.ajp13w.type=ajp13 
         worker.ajp13w.host=localhost 
         worker.ajp13w.port=8009 
         worker.wlb.type=lb 
         worker.wlb.balance_workers=ajp13w 
         worker.jkstatus.type=status
配置apache的虚拟主机目录将制定的请求转发至Tomcat处理
# vim /usr/local/apache/conf/extra/vhost/test.conf
         <VirtualHost*:80>
                            ServerName192.168.40.121
                            ServerAlias192.168.40.121
                            ErrorLog"/data/logs/apache/test-error_log"
                            CustomLog"/data/logs/apache/test-access_log" common
 
                          JkMount/*.jsp ajp13w
                            JkMount /*.jspx ajp13w
                            JkMount /servlet/* ajp13w
                          JkMount/examples/* ajp13w
 
                            # Following are JkMounts foryour application
                             JkMount /preference/** ajp13w
                           JkMount /api** ajp13w
                           JkMount /uajax/get ajp13w
                           JkMount /system_const/** ajp13w
                             JkMount /main.html** ajp13w
                             JkMount /main.html#/* ajp13w
         </VirtualHost>


6.在/data/www/test目录下提供测试页面

# vim vim/data/tomcat/webapp/ROOT/index.jsp
<%@ pagelanguage="java" %>
<%@ pageimport="java.util.*" %>
<html>
  <head>
    <title>JSP test page.</title>
  </head>
  <body>
    <div>
    <% out.println(" Hello,world"); %>
   </div>
  </body>
</html>


7.启动服务:

# /etc/init.d/httpd restart
#/usr/local/tomcat/bin/startup.sh

# ss -ntl
State       Recv-Q Send-Q                                      Local Address:Port                                        Peer Address:Port 
LISTEN      0      100                                                    :::8081                                                  :::*     
LISTEN      0      100                                                    :::8019                                                  :::*     
LISTEN      0      128                                                    :::22                                                    :::*     
LISTEN      0      128                                                     *:22                                                     *:*     
LISTEN      0      100                                                   ::1:25                                                    :::*     
LISTEN      0      100                                             127.0.0.1:25                                                     *:*     
LISTEN      0      50                                                      *:445                                                    *:*     
LISTEN      0      50                                                     :::445                                                   :::*     
LISTEN      0      128                                                     *:10050                                                  *:*     
LISTEN      0      1                                        ::ffff:127.0.0.1:8005                                                  :::*     
LISTEN      0      128                                                    :::8009                                                  :::*     
LISTEN      0      50                                                      *:139                                                    *:*     
LISTEN      0      50                                                     :::139                                                   :::*     
LISTEN      0      1                                        ::ffff:127.0.0.1:8015                                                  :::*     
LISTEN      0      100                                                    :::8080                                                  :::*     
LISTEN      0      128                                                    :::80                                                    :::*


8.验证:出现如下结果,则配置成功

wKiom1VUFFiSdDsWAAGD84uJtp0630.jpg