一、实验环境
服务器 | IP地址 | 软件版本 |
tomcat | 172.16.8.1 | apache-tomcat-7.0.42 |
apache |
172.16.8.2 | httpd-2.4.9 |
二、httpd的编译安装
httpd2.4.9会依赖更高版本的apr和apr-util,使用低版本的话可能会某些功能不能实现!
先下载软件包
1、准备编译环境,解决依赖关系
安装开发包组: “Development Tools“和”Server Platform Development“ 和‘pcre-devel’
#yum -y groupinstall ”Development Tools“ ”Server Platform Development“ #yum -y install pcre-devel openssl-devel
2、 编译安装apr-1.5.0
[root@localhost ~]# tar xf apr-1.5.0.tar.bz2[root@localhost ~]# cd apr-1.5.0[root@localhost apr-1.5.0]# ./configure --prefix=/usr/local/apr[root@localhost apr-1.5.0]# make && make install
3、编译安装apr-util-1.5.3
[root@localhost ~]# tar xf apr-util-1.5.3.tar.bz2[root@localhost ~]# cd apr-util-1.5.3[root@localhost apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/[root@localhost apr-util-1.5.3]# make && make install
4、编译安装httpd
[root@localhost ~]# tar xf httpd-2.4.9.tar.bz2 [root@localhost ~]# cd httpd-2.4.9[root@localhost httpd-2.4.9]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --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-heartbeat --enable-slotmem-shm --enable-slotmem-plain --enable-watchdog[root@localhost httpd-2.4.9]# make &&make install
5、提供服务脚本
[root@localhost ~]# vim /etc/rc.d/init.d/httpd#!/bin/bash## httpd Startup script for the Apache HTTP Server## chkconfig: - 85 15# description: Apache is a World Wide Web server. It is used to serve \# HTML files and CGI.# processname: httpd# config: /etc/httpd/conf/httpd.conf# config: /etc/sysconfig/httpd# pidfile: /var/run/httpd.pid# Source function library.. /etc/rc.d/init.d/functionsif [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpdfi# Start httpd in the C locale by default.HTTPD_LANG=${HTTPD_LANG-"C"}# This will prevent initlog from swallowing up a pass-phrase prompt if# mod_ssl needs a pass-phrase from the user.INITLOG_ARGS=""# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server# with the thread-based "worker" MPM; BE WARNED that some modules may not# work correctly with a thread-based MPM; notably PHP will refuse to start.# Path to the apachectl script, server binary, and short-form for messages.apachectl=/usr/local/apache/bin/apachectlhttpd=${HTTPD-/usr/local/apache/bin/httpd}prog=httpdpidfile=${PIDFILE-/var/run/httpd.pid}lockfile=${LOCKFILE-/var/lock/subsys/httpd}RETVAL=0start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL}stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d 10 $httpd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}}reload() { echo -n $"Reloading $prog: " if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then RETVAL=$? echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else killproc -p ${pidfile} $httpd -HUP RETVAL=$? fi echo}# See how we were called.case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart) if [ -f ${pidfile} ] ; then stop start fi ;; reload) reload ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1esacexit $RETVAL [root@localhost ~]# chmod +x /etc/rc.d/init.d/httpd[root@localhost ~]# chkconfig --add httpd[root@localhost ~]# chkconfig --level 35 httpd on
6、修改下httpd配置文件
[root@node1 httpd-2.4.9]# vim /etc/httpd/httpd.conf //添加如下PidFile "/var/run/httpd.pid"
7、启动测试
#service httpd start
如果启动不了,就将下图的注释掉
三、tomcat的安装
下载jdk与tomcat包
1、安装jdk
[root@mogilefs1 ~]# rpm -ivh jdk-7u9-linux-x64.rpm
2、修改环境变量
[root@mogilefs1 ~]# vim /etc/profile.d/java.sh//添加如下内容:export JAVA_HOME=/usr/java/jdk1.7.0_40export PATH=$PATH:$JAVA_HOME/bin[root@mogilefs1 ~]# source /etc/profile.d/java.sh
3、安装tomcat
[root@mogilefs1 ~]# tar xf apache-tomcat-7.0.42.tar.gz -C /usr/local/[root@mogilefs1 ~]# cd /usr/local/[root@mogilefs1 local]# ln -sv apache-tomcat-7.0.42 tomcat`tomcat' -> `apache-tomcat-7.0.42'[root@mogilefs1 local]# vim /etc/profile.d/tomcat.sh//添加如下:export CATALINA_HOME=/usr/local/tomcatexport PATH=$PATH:$CATALINA_HOME/bin[root@mogilefs1 local]# source /etc/profile.d/tomcat.sh
4、启动tomcat
[root@mogilefs1 local]# catalina.sh start
添加启动脚本
[root@localhost ~]# vim /etc/rc.d/init.d/tomcat# Tomcat init script for Linux.## chkconfig: 2345 96 14# description: The Apache Tomcat servlet/JSP container.# JAVA_OPTS='-Xms64m -Xmx128m'JAVA_HOME=/usr/java/latestCATALINA_HOME=/usr/local/tomcatexport JAVA_HOME CATALINA_HOMEcase $1 instart) exec $CATALINA_HOME/bin/catalina.sh start ;;stop) exec $CATALINA_HOME/bin/catalina.sh stop;;restart) $CATALINA_HOME/bin/catalina.sh stop sleep 2 exec $CATALINA_HOME/bin/catalina.sh start ;;*) echo "Usage: `basename $0` {start|stop|restart}" exit 1 ;;esac[root@localhost ~]# chmod +x /etc/rc.d/init.d/tomcat[root@localhost ~]# chkconfig --add tomcat
5、为tomcat配置个虚拟主机
[root@localhost local]# vim /usr/local/tomcat/conf/server.xml<Engine name="Catalina" defaultHost="www.gulong.com" //修改engine参数
创建站点目录与文件
[root@localhost local]# mkdir -pv /web/webapp[root@localhost local]# cd /web/webapp[root@localhost webapp]# vim index.jsp//添加如下:<%@ page language="java" %><html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA </h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("abc","abc"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body></html>
重启tomcat测试
四、Apache使用mod_proxy反向代理Tomcat
1、要使用mod_proxy与Tomcat实例连接,需要apache已经装载mod_proxy、mod_proxy_http、mod_proxy_ajp和proxy_balancer_module(实现Tomcat集群时用到)等模块:
#/usr/local/apache/bin/httpd -D DUMP_MODULES | grep proxy
从上图看已经加装了proxy模块!
2、修改httpd配置文件
[root@localhost ~]# vim /etc/httpd/httpd.conf //添加这条语句Include /etc/httpd/extra/httpd-proxy.conf#DocumentRoot "/usr/local/apache/htdocs" //并将主站点注释掉,启用虚拟站点
3、创建httpd-proxy.conf虚拟站点配置文件
[root@localhost ~]# vim /etc/httpd/extra/httpd-proxy.conf//添加如下:<VirtualHost *:80> ProxyVia On ProxyRequests Off ProxyPass / http://172.16.8.1:8080/ ProxyPassReverse / http://172.16.8.1:8080/ <Proxy *> Require all granted </Proxy> <Location / > Require all granted </Location></VirtualHost>
注:指令详解
ProxyPreserveHost {On|Off}:如果启用此功能,代理会将用户请求报文中的Host:行发送给后端的服务器,而不再使用ProxyPass指定的服务器地址。如果想在反向代理中支持虚拟主机,则需要开启此项,否则就无需打开此功能。
ProxyVia {On|Off|Full|Block}:用于控制在http首部是否使用Via:,主要用于在多级代理中控制代理请求的流向。默认为Off,即不启用此功能;On表示每个请求和响应报文均添加Via:;Full表示每个Via:行都会添加当前apache服务器的版本号信息;Block表示每个代理请求报文中的Via:都会被移除。
ProxyRequests {On|Off}:是否开启apache正向代理的功能;启用此项时为了代理http协议必须启用mod_proxy_http模块。同时,如果为apache设置了ProxyPass,则必须将ProxyRequests设置为Off。
ProxyPass [path] :将后端服务器某URL与当前服务器的某虚拟路径关联起来作为提供服务的路径,path为当前服务器上的某虚拟路径,url为后端服务器上某URL路径。使用此指令时必须将ProxyRequests的值设置为Off。需要注意的是,如果path以“/”结尾,则对应的url也必须以“/”结尾,反之亦然
ProxyPassReverse:用于让apache调整HTTP重定向响应报文中的Location、Content-Location及URI标签所对应的URL,在反向代理环境中必须使用此指令避免重定向报文绕过proxy服务器。
4、测试
# service httpd restart
从上图看出,apache使用mod_proxy反向代理tomcat已经完成!
五、Apache使用mod_jk反向代理Tomcat
mod_jk是ASF的一个项目,是一个工作于apache端基于AJP协议与Tomcat通信的连接器,它是apache的一个模块,是AJP协议的客户端(服务端是Tomcat的AJP连接器)。
1.安装tomcat-connectors
[root@localhost ~]# tar xf tomcat-connectors-1.2.40-src.tar.gz [root@localhost ~]# cd tomcat-connectors-1.2.40-src[root@localhost tomcat-connectors-1.2.40-src]# cd native/[root@localhost native]# ./configure --with-apxs=/usr/local/apache/bin/apxs[root@localhost native]# make && make install
下面我们来查看一下安装的模块,
[root@localhost native]# cd /usr/local/apache/modules/[root@localhost modules]# ls | grep mod_jk.somod_jk.so
mod_jk模块已经安装!
2、修改httpd配置文件
[root@localhost ~]# vim /etc/httpd/httpd.conf //修改如下#Include /etc/httpd/extra/httpd-proxy.conf 将这条语句注释掉Include /etc/httpd/extra/httpd-jk.conf //添加这条语句
.3、增加httpd-jk.conf配置文件
[root@localhost ~]# vim /etc/httpd/extra/httpd-jk.conf//添加如下LoadModule jk_module modules/mod_jk.soJkWorkersFile /etc/httpd/extra/workers.propertiesJkLogFile logs/mod_jk.logJkLogLevel debugJkMount /* TomcatAJkMount /status/ stat1
注解:
LoadModule指令 用于在apache中装载mod_jk模块
JkWorkersFile 则用于指定保存了worker相关工作属性定义的配置文件,
JkLogFile 则用于指定mod_jk模块的日志文件,
JkLogLevel 则可用于指定日志的级别(info, error, debug)
JkMount (格式: JkMount <URL to match> <Tomcat worker name>)指定则用于控制URL与Tomcat workers的对应关系。
4、.增加workers.properties配置文件
[root@localhost ~]# vim /etc/httpd/extra/workers.properties //添加如下:worker.list=TomcatA,stat1worker.TomcatA.port=8009worker.TomcatA.host=172.16.8.1worker.TomcatA.type=ajp13worker.TomcatA.lbfactor=1worker.stat1.type = status
5、重启httpd,测试
[root@localhost ~]# service httpd restart
从上图看来,apache基于mod_jk模块的反向代理tomcat已经成功!