环境
系统: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 firewalldserviceiptables stopsetenforce0
一、安装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/aprmake && 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/aprcp -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-ajpmake && 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/apxsmake && 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如下几个地方
这里添加一个tomcat1,作为等下mod jk转发的路由,另一台为tomcat2
这里定义程序网站根目录
这里定义日志和格式
3、 启动tomcat
sh /usr/local/tomcat1/bin/catalina.sh startsh /usr/local/tomcat2/bin/catalina.sh start
测试两个tomcat是否能正常访问,返回200为正常,也可用浏览测试。根据自己改的端口来测试。
curl -I192.168.2.72:8080curl -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.confInclude conf/mod_jk.conf
2、在apache的conf目录下创建mod_jk.conf
#cat mod_jk.conf
JkWorkersFileconf/workers.propertiesJkLogFile/usr/local/apache/logs/jk.logJkShmFile/usr/local/apache/logs/jk-runtime-statusJkLogLevelerrorJkLogStampFormat"[%d %H:%M:%S] "JkOptions+ForwardKeySize +ForwardURICompat -ForwardDirectoriesJkRequestLogFormat"%w %V %U%q %s %T"
3、创建workers.properties文件
# cat workers.properties
worker.list=controller,tomcat1,tomcat2 # tomcat1worker.tomcat1.port=8009worker.tomcat1.host=127.0.0.1worker.tomcat1.type=ajp13worker.tomcat1.lbfactor=1worker.tomcat1.connection_pool_size=500worker.tomcat1.connection_pool_minsize=25worker.tomcat1.connection_pool_timeout=300worker.tomcat1.socket_timeout=20 worker.tomcat2.port=8109worker.tomcat2.host=127.0.0.1worker.tomcat2.type=ajp13worker.tomcat2.lbfactor=1worker.tomcat2.connection_pool_size=500worker.tomcat2.connection_pool_minsize=25worker.tomcat2.connection_pool_timeout=300worker.tomcat2.socket_timeout=20 #========controllertomcat online =======worker.controller.type=lbworker.controller.balanced_workers=tomcat1,tomcat2worker.controller.sticky_session=1worker.controller.connection_pool_size=1000worker.controller.connection_pool_minsize=50worker.controller.connection_pool_timeout=300worker.controller.socket_timeout=20
4、配置httpd-vhosts.conf
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 AllowOverride none Options -Indexes AllowOverride All Require all granted
[root@node2 www]# ls1 2[root@node2 www]# ls *1:1.tt index.html2:1.tt index.html
/www/1和/www/2分别是tomcat1 和tomcat2的根目录。里面放置一样的文件名,但内容不同。然后刷新浏览器,看内容是否会不停变化,如果会就证明转发成功了。
JkMount /*.html* controller
这个是定义所有.html的请求都转发到tomcat处理,这里是实验,生产中应该把动太内容发给tomcat处理,如jsp等。