一 、Apache与Tomcat的比较
apache支持静态页面,tomcat支持动态的,比如servlet等。 一般使用apache+tomcat的话,apache只是作为一个转发,对jsp的处理是由tomcat来处理的。 apache可以支持php\cgi\perl,但是要使用java的话,你需要tomcat在apache后台支撑,将java请求由apache转发给tomcat处理。 apache是web服务器,Tomcat是应用(java)服务器,它只是一个servlet(jsp也翻译成servlet)容器,可以认为是apache的扩展,但是可以独立于apache运行。
这两个有以下几点可以比较的:
1、两者都是apache组织开发的
2、两者都有HTTP服务的功能
3、两者都是免费的
不同点:
Apache是专门用于提供HTTP服务的,以及相关配置的(例如虚拟主机、URL转发等等)
Tomcat是Apache组织在符合J2EE的JSP、Servlet标准下开发的一个JSP服务器 整合的好处是:
如果客户端请求的是静态页面,则只需要Apache服务器响应请求
如果客户端请求动态页面,则是Tomcat服务器响应请求
因为jsp是服务器端解释代码的,这样整合就可以减少Tomcat的服务开销 二、 Linux下Apache与Tomcat整合的简单方法
准备软件: 下载httpd-2.4.16: http://httpd.apache.org/download.cgi
下载tomcat-connectors-1.2.41:http://tomcat.apache.org/download-connectors.cgi
1、JDK配置 [iyunv@web1 ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [iyunv@web1 ~]# uname -a Linux web1 2.6.32-504.el6.i686 #1 SMP Wed Oct 15 03:02:07 UTC 2014 i686 i686 i386 GNU/Linux [iyunv@web1 ~]# cd tools [iyunv@web1 tools]# tar xf jdk-1.7-linux-32bit.tar.gz -C /usr/local/ [iyunv@web1 tools]# mv /usr/local/jdk1.7.0_67 /usr/local/jdk [iyunv@web1 tools]# vi /etc/profile JAVA_HOME=/usr/local/jdk PATH=$PATH:$JAVA_HOME/bin CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export JAVA_HOME PATH CLASSPATH [iyunv@web1 tools]# source /etc/profile [iyunv@web1 tools]# java -version #显示版本说明成功 java version "1.7.0_67" Java(TM) SE Runtime Environment (build 1.7.0_67-b01) Java HotSpot(TM) Client VM (build 24.65-b04, mixed mode) 2、Tomcat配置 [iyunv@web1 tools]# tar xf apache-tomcat-7.0.64.tar.gz -C /usr/local/ [iyunv@web1 tools]# mv /usr/local/apache-tomcat-7.0.64 /usr/local/tomcat #默认tomcat是root身份运行的,这样不安全,我们设置来用普通用户 [iyunv@web1 tools]# groupadd tomcat [iyunv@web1 tools]# useradd -g tomcat tomcat [iyunv@web1 tools]# passwd tomcat [iyunv@web1 tools]# chown tomcat.tomcat -R /usr/local/tomcat [iyunv@web1 tools]# su - tomcat /usr/local/tomcat/bin/startup.sh [iyunv@web1 tools]# echo "su - tomcat /usr/local/tomcat/bin/startup.sh" >> /etc/rc.local #开机启动 [iyunv@web1 tools]# netstat -tunlp|grep java tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 14940/java tcp 0 0 :::8009 :::* LISTEN 14940/java tcp 0 0 :::8080 :::* LISTEN 14940/java 3、apache配置
[iyunv@web1 tools]# yum -y install zlib zlib-devel
[iyunv@web1 tools]# tar zxvf apr-1.5.1.tar.gz
[iyunv@web1 tools]# cd apr-1.5.1 [iyunv@web1 apr-1.5.1]# ./configure --prefix=/usr/local/apr [iyunv@web1 apr-1.5.1]# make && make install && cd - [iyunv@web1 tools]# tar zxvf apr-util-1.5.4.tar.gz [iyunv@web1 apr-util-1.5.4]# cd apr-util-1.5.4 [iyunv@web1 apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [iyunv@web1 apr-util-1.5.4]# make && make install && cd - [iyunv@web1 tools]# tar xf httpd-2.4.16.tar.gz
[iyunv@web1 tools]# cd httpd-2.4.16
[iyunv@web1 httpd-2.4.16]# ./configure --prefix=/application/apache2.4.16 --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=event --enable-rewrite --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
[iyunv@web1 httpd-2.4.16]# make && make install && cd -
[iyunv@web1 tools]# ln -s /application/apache2.4.16/ /application/apache
[iyunv@web1 tools]# /application/apache/bin/apachectl -t
[iyunv@web1 tools]# /application/apache/bin/apachectl start
[iyunv@web1 tools]# echo "/application/apache/bin/apachectl start" >>/etc/rc.local
4、安装mod_jk
[iyunv@web1 tools]# tar xf tomcat-connectors-1.2.41-src.tar.gz
[iyunv@web1 tools]# cd tomcat-connectors-1.2.41-src/native
[iyunv@web1 native]# ./configure --with-apxs=/application/apache2.4.16/bin/apxs && make && make install
到apache的modules下面看看有没有mod_jk.so模块,就到apache-2.0目录下cp一个
5、配置apache支持jk模块
[iyunv@web1 ~]# vi /application/apache/conf/httpd.conf
LoadModule jk_module modules/mod_jk.so
…… Include conf/extra/mod_jk.conf [iyunv@web1 ~]# vi /application/apache/conf/extra/mod_jk.conf
JkWorkersFile /application/apache2.4.16/conf/extra/workers.properties
JkLogFile /application/apache2.4.16/logs/mod_jk.log
JkShmFile logs/jk-runtime-status
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
#Sample JkMounts. Replace these with the paths you would
#like to mount from your JSP server.
#syntax: JkMount ${URL_DIR}/*.jsp worker_name
JkMount /*.jsp worker1 #判断url中包含.jsp后缀的文件时,就交由tomcat来解析,原理类似nginx里面的location
JkMount /*.png worker1 JkMount /*.css worker1 JkMount /*.page worker1
JkMount /*.cic worker1
JkMount /*.do worker1
JkMount /*.action worker1
[iyunv@web1 ~]# vi /application/apache/conf/extra/workers.properties
worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=127.0.0.1
worker.worker1.port=8009
worker.worker1.cachesize=10
worker.worker1.cache_timeout=600
worker.worker1.socket_keepalive=1
worker.worker1.socket_timeout=300
一般apache的网站根目录和tomcat的网站根目录在同一目录下,此处为了达到明显测试结果,按默认来设置了,保存退出,重启服务,测试如下:
|