apache服务器:192.168.1.155
yum -y install httpd
[root@RS1 conf]# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built: May 11 2016 19:28:33
基于mod_jk模块:
wget http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.41-src.tar.gz
tar xf tomcat-connectors-1.2.41-src.tar.gz
cd tomcat-connectors-1.2.41-src/native
./configure --with-apxs=/usr/sbin/apxs
make && make install
查看是否已经存在mod_jk.so
ls -l /etc/httpd/modules/mod_jk.so
[root@RS1 conf]# ls -l /etc/httpd/modules/mod_jk.so
-rwxr-xr-x. 1 root root 1215731 5月 17 05:35 /etc/httpd/modules/mod_jk.so
配置mod_jk模块
cd /etc/httpd/conf.d/
vim mod_jk.conf
[root@RS1 conf.d]# cat mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* tomcat1
JkMount /status/ stat1
[root@RS1 conf.d]# cat workers.properties
worker.list=tomcat1,stat1
worker.tomcat1.port=8009
worker.tomcat1.host=192.168.1.155
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.stat1.type = status
检查一下语法就ok
service httpd start
install tomcat
安装jdk7
curl -LO -H "Cookie: oraclelicense=accept-securebackup-cookie" \
"http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-x64.rpm" -k
rpm -ivh jdk-7u75-linux-x64.rpm
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.7.0_75
export PATH=$JAVA_HOME/bin:$PATH
. /etc/profile.d/java.sh
安装tomcat
wget http://ftp.riken.jp/net/apache/tomcat/tomcat-7/v7.0.70/bin/apache-tomcat-7.0.70.tar.gz
tar zxvf apache-tomcat-7.0.59.tar.gz
mv apache-tomcat-7.0.70 /usr/local/tomcat7
vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat7
export PATH=$CATALINA_HOME/bin:$PATH
. /etc/profile.d/tomcat.sh
useradd -M -d /usr/tomcat7 tomcat7
chown -R tomcat7. /usr/local/tomcat7/*
给定tomcat提供脚本:
脚本:
[root@RS2 ~]# cat tomcat7.sh
#!/bin/bash
tomcat_home="/usr/local/tomcat7"
tomcat_user="tomcat7"
LOCKFILE="/var/lock/subsys/tomcat7"
. /etc/init.d/functions
start() {
su - $tomcat_user -c $tomcat_home/bin/startup.sh
[ $? -eq 0 ] && {
action "Starting Tomcat" /bin/true
touch $LOCKFILE
}
}
stop() {
$tomcat_home/bin/shutdown.sh
[ $? -eq 0 ] && {
action "Stopping Tomcat" /bin/true
rm -f $LOCKFILE
}
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
替换掉tomcat原始浏览器页面
cd /usr/local/tomcat7/webapps/ROOT
mv index.jsp index.jsp.bak
vim index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<%
out.println("Hello world!");
%>
</body>
</html>
浏览器访问:http://192.168.1.155:8080,出现如下
结合apache和tomcat,由于刚刚已经配置好了mod_jk模块的信息
service httpd restart
浏览器访问:http://192.168.1.155出现如下
于是基于apache的反向代理成功(基于mod_jk模块)
基于mod_proxy模块(将刚刚两个配置文件取消掉mod_jk.conf.bak等)
[root@RS1 conf.d]# cat mod_proxy.conf
ProxyVia Off
ProxyRequests Off
ProxyPass / ajp://192.168.1.155:8009/
ProxyPassReverse / ajp://192.168.1.155:8009/
<Proxy *>(由于httpd版本是2.2的,2.4的版本这里有更改)
Order allow,deny
Allow from all
</Proxy>
<Location />
Order allow,deny
Allow from all
</Location>
基于apache2.4版本的配置:
ProxyVia Off
ProxyRequests Off
ProxyPass / ajp://192.168.1.155:8009/
#ProxyPass /my-webapp http://www.magedu.com:8080/my-webapp:访问的tomcat上不是默认的目录而是/my-webapp下面的
ProxyPassReverse / ajp://192.168.1.155:8009/
<Location />
Require all granted
</Location>
于是基于mod_proxy模块的反代也成功了
研究了apache2.4版本的mod_jk模块,但一直报错,有时间再次操作一遍
tomcat篇之与apache结合实现反代理(基于模块mod_jk,mod_proxy)
原文地址:http://huangsir007.blog.51cto.com/6159353/1827422