浅谈apache与tomact的整合
一般来讲,配置apache和tomcat的整合有三种方式,mod_proxy,mod_ajp和mod_jk等三种方式,前2种配置步骤类似且相对容易,且只支持apache2.2以后的版本,mod_jk则配置相对复杂,但强在稳定性和性能方面,因为没有系统的写过这方面的文档,故在此简要的记录下使用mod_proxy和mod_jk两种方式来整合apache和tomcat!三种方式的对比,具体请参考:http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/
本文环境介绍:
os版本:centos5.4 64bit
jdk版本:jdk-6u2-linux-i586.bin
tomcat版本:7.0.29
apache版本:2.4.2
apache服务器IP:192.168.123.110/24
tomcat实例一IP:192.168.123.110/24 (端口默认)
tomcat实例二IP:192.168.123.20/24 (端口默认)
一:mod_proxy方式的整合
1:编译安装apache,需要编译proxy相关的模块,这里采用目前最新版的apache2.4.2
[*]# tar -zxvpf httpd-2.4.2.tar.gz
[*]# cd httpd-2.4.2
[*]# ./configure --prefix=/usr/local/apache --enable-proxy --enable-proxy-ajp--enable-rewrite --enable-so--enable-headers --enable-expires--enable-modules-shared=most --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/
[*]# make
[*]# make install
若启动apache出现下面的错误,基本上属于apr的问题,要么apr没安装,要么版本不对!
# /usr/local/apache/bin/apachectl -t
httpd: Syntax error on line 113 of /usr/local/apache/conf/httpd.conf: Cannot load /usr/local/apache/modules/mod_proxy.so into server: /usr/local/apache/modules/mod_proxy.so: undefined symbol: apr_global_mutex_lockfile
# yum remove apr
# /usr/local/apache/bin/apachectl -t
Syntax OK
# /usr/local/apache/bin/apachectl -t -D DUMP_MODULES |grep proxy
proxy_module (shared)
proxy_connect_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_fcgi_module (shared)
proxy_scgi_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_express_module (shared)
2:配置apache的虚拟主机
[*]# /usr/local/apache/bin/apachectl -t -D DUMP_VHOSTS
[*]VirtualHost configuration:
[*]*:80 is a NameVirtualHost
[*] default server 192.168.123.110 (/usr/local/apache/conf/extra/httpd-vhosts.conf:23)
[*] port 80 namevhost 192.168.123.110 (/usr/local/apache/conf/extra/httpd-vhosts.conf:23)
[*] port 80 namevhost 192.168.123.110 (/usr/local/apache/conf/extra/httpd-vhosts.conf:23)
[*] port 80 namevhost www.yang.com (/usr/local/apache/conf/extra/httpd-vhosts.conf:31)
[*] port 80 namevhost www.yang.com (/usr/local/apache/conf/extra/httpd-vhosts.conf:31)
若启动apache报错如下,则需要加载mod_slotmem_shm.so模块,之前在使用apache中很少用到mod_proxy模块,所以遇到的问题多多!
# /usr/local/apache/bin/apachectl -k start
# echo $?
0
# netstat -ntpl |grep :80
# cat /usr/local/apache/logs/error_log
AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[:emerg] AH00020: Configuration Failed, exiting
AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[:emerg] AH00020: Configuration Failed, exiting
# grep 'slotmem' /usr/local/apache/conf/httpd.conf
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
# /usr/local/apache/bin/apachectl -k start
# netstat -ntpl |grep :80
tcp 0 0 :::80 :::* LISTEN 7898/httpd
注意:2.4.2版本的apache中需要在目录下面添加 Require all granted,否则无论怎么访问都是403,悲了个催的!具体报错如下:
AH01630: clientdenied by server configuration: /tmp
[*]# grep -v '^#' /usr/local/apache/conf/extra/httpd-vhosts.conf |grep -v '^$'
[*]
[*] ServerName 192.168.123.110
[*]
[*] Order deny,allow
[*] Deny from all
[*]
[*]
[*]
[*] ServerName www.yang.com
[*] DocumentRoot /tmp
[*]
[*] Options +indexes
[*] Order allow,deny
[*] Allow from all
[*] Require all granted
[*]
[*]
3:安装jdk和tomcat,并启动tomcat
[*]# java -version
[*]java version "1.6.0"
[*]OpenJDKRuntime Environment (build 1.6.0-b09)
[*]OpenJDK 64-Bit Server VM (build 1.6.0-b09, mixed mode)
[*]
[*]# /usr/local/tomcat7/bin/startup.sh
[*]Using CATALINA_BASE: /usr/local/tomcat7
[*]Using CATALINA_HOME: /usr/local/tomcat7
[*]Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
[*]Using JRE_HOME: /usr/local/java
[*]Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
[*]
[*]# netstat -ntpl |grep java
[*]tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 7601/java
[*]tcp 0 0 :::8009 :::* LISTEN 7601/java
[*]tcp 0 0 :::8080 :::* LISTEN 7601/java
4:使用mod_proxy转发jsp的请求,test目录,tomcat.css,tomcat.png请求使用apache处理
[*]
[*] ServerName www.yang.com
[*] DocumentRoot /tmp
[*]
[*] Options +indexes
[*] Order allow,deny
[*] Allow from all
[*] Require all granted
[*]
[*] Proxypass /test !
[*] Proxypass /tomcat.css !
[*] Proxypass /tomcat.png !
[*] Proxypass / http://192.168.123.20:8080
[*]
5:mod_proxy配置集群
[*]
[*] ServerName www.yang.com
[*] DocumentRoot /tmp
[*]
[*] Options +indexes
[*] Order allow,deny
[*] Allow from all
[*] Require all granted
[*]
[*] Proxypass /test !
[*] Proxypass /tomcat.css !
[*] Proxypass /tomcat.png !
[*] Proxypass / balancer://yang
[*]
[*] BalancerMember http://192.168.123.20:8080/
[*] BalancerMember http://192.168.123.110:8080/
[*]
[*]
6:测试
二:mod_jk方式整合
1:下载并编译安装mod_jk模块
[*]# wget http://www.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.37-src.tar.gz
[*]# tar -zxvf tomcat-connectors-1.2.37-src.tar.gz
[*]# cd tomcat-connectors-1.2.37-src/native/
[*]# ./configure --with-apxs=/usr/local/apache/bin/apxs
[*]# make && make install
[*]
[*]# grep 'mod_jk' /usr/local/apache/conf/httpd.conf
[*]Include conf/extra/mod_jk.conf
2:修改mod_jk配置文件如下
[*]# grep -v '^#' /usr/local/apache/conf/extra/mod_jk.conf
[*]LoadModule jk_module modules/mod_jk.so
[*]JkWorkersFile conf/extra/workers.properties
[*]JkLogFile "logs/mod_jk.log"
[*]JkLogLevel info
[*]JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
[*]JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
[*]JkRequestLogFormat "%w %V %T"
[*]JkShmFile logs/jk.shm
[*]HostnameLookups Off
3:配置workers.properties,指定ajp连接tomcat的信息
[*]# cat /usr/local/apache/conf/extra/workers.properties
[*]worker.list=yang,jkstatus
[*]#110
[*]worker.server110.port=8009
[*]worker.server110.host=192.168.123.110
[*]worker.server110.type=ajp13
[*]worker.server110.lbfactor=0
[*]worker.server110.socket_keepalive=1
[*]worker.server110.socket_timeout=0
[*]worker.server110.retries=3
[*]
[*]#20
[*]worker.server20.port=8009
[*]worker.server20.host=192.168.123.20
[*]worker.server20.type=ajp13
[*]worker.server20.lbfactor=9
[*]worker.server20.socket_keepalive=1
[*]worker.server20.socket_timeout=0
[*]worker.server20.retries=3
[*]
[*]worker.yang.type=lb
[*]worker.retries=3
[*]worker.yang.balance_workers=server110, server22
[*]worker.yang.sticky_session=true
[*]worker.yang.sticky_session_force=true
[*]
[*]worker.jkstatus.type=status
[*]worker.jkstatus.read_only=true
4:配置虚拟主机,jkmount_yang.conf文件指定了客户端http请求的走向,test目录下的请求交给apache处理
[*]
[*] ServerName www.yang.com
[*] DocumentRoot /tmp
[*] Includeconf/extra/jkmount_yang.conf
[*]
[*] Options +indexes
[*] Order allow,deny
[*] Allow from all
[*] Require all granted
[*]
[*]
[*]
[*]# cat /usr/local/apache/conf/extra/jkmount_yang.conf
[*]JkMount /* yang
[*]JkMount /status jkstatus
[*]JkunMount /test/* yang
5:重启apache
[*]# /usr/local/apache/bin/apachectl -t
[*]Syntax OK
[*]# /usr/local/apache/bin/apachectl -k restart
6:修改tomcat配置文件如下,重启tomcat实例,文件中主要修改jvmroute参数,同workers.properties中的一致
# cat sr/local/tomcat7/conf/server.xml
7:测试
# tail -f/usr/local/apache/logs/mod_jk.log
yang www.yang.com 0.017531
yang www.yang.com 0.003080
yang www.yang.com 0.001009
yang www.yang.com 0.001876
页:
[1]