环境
系统:centos 7.3
Apache:2.4.27
Modjk:1.2.42
Tomcat:8.5
Jdk:1.8.92
Apr:1.6.2
Apr-util:1.6.0
关闭所有防火墙和selinux
systemctlstop firewalld serviceiptables stop setenforce0
一、安装Apache
1、 安装依赖
yum -y install wget gcc gcc-c++ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2libxml2-devel curl-devel libjpeg* libpng* freetype* pcre-devel bzip2-devellibmcrypt-devel libmhash libexpat1 libexpat1-dev
2、 安装apr
tar xzvf apr-1.6.2.tar.gz ;cdapr-1.6.2; ./configure--prefix=/usr/local/apr make && make install
3、 安装apr-util
tar xzvf apr-util-1.6.0.tar.gz ;cdapr-util-1.6.0 ./configure--prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ make && make install
4、 安装apache2.4
tar xzvf httpd-2.4.27.tar.gz ;cdhttpd-2.4.27
编译apache之前要把解压后的apr和apr-util复制一分到apache解压后的源码目录中的srclib子目录一,不然会报错。configure: error: BundledAPR requested but not found at ./srclib/. Download and unpack the correspondingapr and apr-util packages to ./srclib/.
cp -a apr-1.6.2httpd-2.4.27/srclib/apr cp -a apr-util-1.6.0httpd-2.4.27/srclib/apr-util ./configure--prefix=/usr/local/apache --with-included-apr --enable-so--enable-deflate=shared --enable-expires=shared --enable-rewrite=shared--enable-static-support --with-mpm=worker --disable-userdir--enable-proxy-balancer --enable-proxy-http--enable-proxy-connect --enable-proxy --enable-rewrite--enable-proxy-ajp make && make install
5、 安装mod jk
tar xzvftomcat-connectors-1.2.42-src.tar.gz ;cd tomcat-connectors-1.2.42-src/native ./configure--with-apxs=/usr/local/apache/bin/apxs make && make install
二、安装Tomcat
1、 安装jdk
rpm -ivhjdk-8u92-linux-x64.rpm
2、 安装和配置tomcat
tar xzvfapache-tomcat-8.5.15.tar.gz -C /usr/local/
解压两个tomcat,分别命名为tomcat1 tomcat2
因为我们在同一台机上实验,所以要修改第二个tomcat的http和ajp端口,避免端口冲突。这里不再叙述
修改server.xml如下几个地方
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
这里添加一个tomcat1,作为等下mod jk转发的路由,另一台为tomcat2
<Host name="localhost" appBase="" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context path="" docBase="/www/1" debug="0"reloadable="false" crossContext="true" />
这里定义程序网站根目录
<ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs" prefix="localhost_access_log."suffix=".txt" pattern="common"resolveHosts="false"/>
这里定义日志和格式
3、 启动tomcat
sh /usr/local/tomcat1/bin/catalina.sh start sh /usr/local/tomcat2/bin/catalina.sh start
测试两个tomcat是否能正常访问,返回200为正常,也可用浏览测试。根据自己改的端口来测试。
curl -I192.168.2.72:8080 curl -I192.168.2.72:8180
三、配置Apache mod jk转发
1、在httpd.conf中找到LoadModule添加
LoadModule jk_module modules/mod_jk.so
打开和添加如下两行,打开vhost和mod_jk的配置
Include conf/extra/httpd-vhosts.conf Include conf/mod_jk.conf
2、在apache的conf目录下创建mod_jk.conf
#cat mod_jk.conf
JkWorkersFileconf/workers.properties JkLogFile/usr/local/apache/logs/jk.log JkShmFile/usr/local/apache/logs/jk-runtime-status JkLogLevelerror JkLogStampFormat"[%d %H:%M:%S] " JkOptions+ForwardKeySize +ForwardURICompat -ForwardDirectories JkRequestLogFormat"%w %V %U%q %s %T"
3、创建workers.properties文件
# cat workers.properties
worker.list=controller,tomcat1,tomcat2 # tomcat1 worker.tomcat1.port=8009 worker.tomcat1.host=127.0.0.1 worker.tomcat1.type=ajp13 worker.tomcat1.lbfactor=1 worker.tomcat1.connection_pool_size=500 worker.tomcat1.connection_pool_minsize=25 worker.tomcat1.connection_pool_timeout=300 worker.tomcat1.socket_timeout=20 worker.tomcat2.port=8109 worker.tomcat2.host=127.0.0.1 worker.tomcat2.type=ajp13 worker.tomcat2.lbfactor=1 worker.tomcat2.connection_pool_size=500 worker.tomcat2.connection_pool_minsize=25 worker.tomcat2.connection_pool_timeout=300 worker.tomcat2.socket_timeout=20 #========controllertomcat online ======= worker.controller.type=lb worker.controller.balanced_workers=tomcat1,tomcat2 worker.controller.sticky_session=1 worker.controller.connection_pool_size=1000 worker.controller.connection_pool_minsize=50 worker.controller.connection_pool_timeout=300 worker.controller.socket_timeout=20
4、配置httpd-vhosts.conf
<VirtualHost*:80> ServerName www.yy.com DocumentRoot "/www/1" ErrorLog "logs/yy-error_log" CustomLog"logs/yy-access_log" common JkMount /*.html* controller JkMount /*.tt* controller JkMount /*.index* controller <Directory /> AllowOverride none Options -Indexes AllowOverride All Require all granted </Directory> </VirtualHost>
[[email protected] www]# ls 1 2 [[email protected] www]# ls * 1: 1.tt index.html 2: 1.tt index.html
/www/1和/www/2分别是tomcat1 和tomcat2的根目录。里面放置一样的文件名,但内容不同。然后刷新浏览器,看内容是否会不停变化,如果会就主明转发成功了。
JkMount /*.html* controller
这个是定义所有.html的请求都转发到tomcat处理,这里是实验,生产中应该把动太内容发给tomcat处理,如jsp等。