在各种企业级网站应用系统中,JSP也是使用较多的一种网站开发语言,对于这样的网站服务器,必须能够支持Java环境、JSP解析,比如安装Tomcat、Jboss等JSP容器。另外一方面,Apache HTTP Server对静态HTML页面的处理能力要更加优秀一些,因此若能将Apache与Tomcat等JSP容器整合在一起使用,将可以获得更好的网站服务体验。
这种情况下,Apache仍然作为浏览器访问的前端服务,当客户机请求访问*.jsp网页时,再自动调用后端的tomcat服务进行解析,并将结果页面返回给客户机。其间的沟通过程需要安装连接器mod_jk来完成。
后端安装Tomcat:
安装jdk开发环境 [[email protected] ~]# cd /setup/ [[email protected] setup]# chmod +x jdk-6u20-linux-i586.bin [[email protected] setup]# ./jdk-6u20-linux-i586.bin 解压tomcat [[email protected] setup]# vim /var/www/html/index.jsp tar zxf apache-tomcat-7.0.23.tar.gz -C /usr/local/tomcat [[email protected] ~]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/sbin/tomcat-up [[email protected] ~]# ln -s /usr/local/tomcat/bin/shudown.sh /usr/local/sbin/tomcat-down 添加环境变量vim /etc/profile: JAVA_HOME=/setup/jdk1.6.0_20 CATALINA_HOME=/usr/local/tomcat CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/lib/servlet-api.jar PATH=$JAVA_HOME/bin:$PATH export JAVA_HOME CLASSPATH PATH CATALINA_HOME 测试jdk: [[email protected] setup]# java -version java version "1.6.0_20" Java(TM) SE Runtime Environment (build 1.6.0_20-b02) Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing) 修改tomcat的server.xml配置文件: [[email protected] setup]# vim /usr/local/tomcat/conf/server.xml <Engine name="Catalina" defaultHost="www.w4ya.com" jvmRoute="jvm1"> #把默认主机改为"www.w4ya.com" <Host name="www.w4ya.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/var/www/html/" debug="0" /> //新建虚拟主机 </Host> 启动tomcat服务,并确认tomcat服务的监听状态。 [[email protected] ~]# tomcat-up Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.6.0_20 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [[email protected] ~]# netstat -anpt | grep java tcp 0 0 :::8009 :::* LISTEN 2591/java tcp 0 0 :::8080 :::* LISTEN 2591/java
在前端服务器安装apache
[[email protected] http]# tar xf apr-1.5.1.tar.gz [[email protected] http]# cd apr-1.5.1 [[email protected] apr-1.5.1]# ./configure --prefix=/usr/local/apr [[email protected] apr-1.5.1]# make && make install [[email protected] http]# tar xf apr-util-1.5.4.tar.gz [[email protected] http]# cd apr-util-1.5.4 [[email protected] apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [[email protected] apr-util-1.5.4]# make && make install with-apr=/usr/local/apr [[email protected] setup]# tar jxf pcre-8.35.tar.bz2 [[email protected] setup]# cd pcre-8.35 [[email protected] pcre-8.35]# ./configure [[email protected] pcre-8.35]# make && make install [[email protected] httpd-2.4.10]# ./configure --prefix=/usr/local/apache --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-http --enable-proxy-ajp --enable-proxy-balancer --enable-lbmethod-heartbeat --enable-hearbeat --enable-slotmem-shm --enable-slotmem-plain --enable-watchdog [[email protected] httpd-2.4.10]# make && make install [[email protected] conf]# vim /usr/local/apache/conf/httpd.conf LoadModule slotmem_shm_module modules/mod_slotmem_shm.so //打开这个模块
第一种方法apache直接代理tomcat:
[[email protected] conf]# vim /usr/local/apache/conf/httpd.conf //引入配置文件 include /usr/local/apache/conf/http-proxy.conf [[email protected] conf]# vim http-proxy.conf //在配置文件添加以下内容 <VirtualHost *:80> ProxyVia On ProxyRequests Off ProxyPreserveHost On ProxyPass / http://192.168.1.115:8080 ProxyPassReverse / http://192.168.1.115:8080 <Proxy *> Require all granted </Proxy> <Location / > Require all granted </Location> </VirtualHost>
第二种方法使用mod_jk代理:
[[email protected] setup]# weget http://mirrors.cnnic.cn/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.40-src.tar.gz [[email protected] setup]# tar xf tomcat-connectors-1.2.40-src.tar.gz [[email protected]lhost setup]# cd tomcat-connectors-1.2.40-src/native/ [[email protected] native]# ./configure --with-apxs=/usr/local/apache/bin/apxs [[email protected] native]#make && make install [[email protected] modules]# ls /usr/local/apache/modules/ |grep "mod_jk.so" mod_jk.so [[email protected] conf]# vim /usr/local/apache/conf/httpd.conf //添加以下内容: LoadModule jk_module modules/mod_jk.so JkWorkersFile /usr/local/apache/conf/workers.properties JkLogFile logs/mod_jk.log JkLogLevel debug JkShmFile logs/jk.shm JkMount /* Tomcat JkMount /status/ stat [[email protected] conf]# vim workers.properties //容器配置文件 worker.list=Tomcat,stat1 worker.Tomcat.port=8009 worker.Tomcat.host=192.168.1.116 worker.Tomcat.type=ajp13 worker.Tomcat.lbfactor=1 worker.stat.type = status 重启apache
apache代理到tomcatB:
本文转自:维斯亚综合论坛:http://www.w4ya.com/forum.php
时间: 2024-10-29 19:05:35