通常來說,配置apache和tomcat的整合有三種方式,mod_proxy,mod_ajp和mod_jk等三種方式,前2種配置步驟相似且相對容易,且只支持apache2.2之後的版本,mod_jk則配置相對複雜,但強在穩定性和性能方面,由於沒有系統的寫過這方面的文檔,故在此簡要的記錄下使用mod_proxy和mod_jk兩種方式來整合apache和tomcat!css
三種方式的對比,具體請參考:http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/html
本文環境介紹:
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 (端口默認)java
一:mod_proxy方式的整合
1:編譯安裝apache,須要編譯proxy相關的模塊,這裏採用目前最新版的apache2.4.2linux
- [root@db1 ~]# tar -zxvpf httpd-2.4.2.tar.gz
- [root@db1 ~]# cd httpd-2.4.2
- [root@db1 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/
- [root@db1 httpd-2.4.2]# make
- [root@db1 httpd-2.4.2]# make install
若啓動apache出現下面的錯誤,基本上屬於apr的問題,要麼apr沒安裝,要麼版本不對! web
[root@db1 ~]# /usr/local/apache/bin/apachectl -t [root@db1 ~]# yum remove apr [root@db1 ~]# /usr/local/apache/bin/apachectl -t -D DUMP_MODULES |grep proxy |
2:配置apache的虛擬主機centos
- [root@db1 ~]# /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模塊,因此遇到的問題多多! tomcat
[root@db1 ~]# /usr/local/apache/bin/apachectl -k start [root@db1 ~]# grep 'slotmem' /usr/local/apache/conf/httpd.conf [root@db1 ~]# /usr/local/apache/bin/apachectl -k start |
注意:2.4.2版本的apache中須要在目錄下面添加 Require all granted,不然不管怎麼訪問都是403,悲了個催的!具體報錯以下:
[Fri Aug 10 19:07:13.263893 2012] [authz_core:error] [pid 8918:tid 1126259008] [client 192.168.123.102:53420] AH01630: client denied by server configuration: /tmp
- [root@db1 ~]# grep -v '^#' /usr/local/apache/conf/extra/httpd-vhosts.conf |grep -v '^$'
- <VirtualHost *:80>
- ServerName 192.168.123.110
- <Location />
- Order deny,allow
- Deny from all
- </Location>
- </VirtualHost>
- <VirtualHost *:80>
- ServerName www.yang.com
- DocumentRoot /tmp
- <Directory /tmp>
- Options +indexes
- Order allow,deny
- Allow from all
- Require all granted
- </Directory>
- </VirtualHost>
3:安裝jdk和tomcat,並啓動tomcat
- [root@db2 ~]# java -version
- java version "1.6.0"
- OpenJDK Runtime Environment (build 1.6.0-b09)
- OpenJDK 64-Bit Server VM (build 1.6.0-b09, mixed mode)
- [root@db2 ~]# /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
- [root@db2 ~]# 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處理
- <VirtualHost *:80>
- ServerName www.yang.com
- DocumentRoot /tmp
- <Directory /tmp>
- Options +indexes
- Order allow,deny
- Allow from all
- Require all granted
- </Directory>
- Proxypass /test !
- Proxypass /tomcat.css !
- Proxypass /tomcat.png !
- Proxypass / http://192.168.123.20:8080
- </VirtualHost>
5:mod_proxy配置集羣
- <VirtualHost *:80>
- ServerName www.yang.com
- DocumentRoot /tmp
- <Directory /tmp>
- Options +indexes
- Order allow,deny
- Allow from all
- Require all granted
- </Directory>
- Proxypass /test !
- Proxypass /tomcat.css !
- Proxypass /tomcat.png !
- Proxypass / balancer://yang
- <Proxy balancer://yang/>
- BalancerMember http://192.168.123.20:8080/
- BalancerMember http://192.168.123.110:8080/
- </Proxy>
- </VirtualHost>
6:測試
二:mod_jk方式整合
1:下載並編譯安裝mod_jk模塊
- [root@db1 ~]# wget http://www.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.37-src.tar.gz
- [root@db1 ~]# tar -zxvf tomcat-connectors-1.2.37-src.tar.gz
- [root@db1 ~]# cd tomcat-connectors-1.2.37-src/native/
- [root@db1 native]# ./configure --with-apxs=/usr/local/apache/bin/apxs
- [root@db1 native]# make && make install
- [root@db1 ~]# grep 'mod_jk' /usr/local/apache/conf/httpd.conf
- Include conf/extra/mod_jk.conf
2:修改mod_jk配置文件以下
- [root@db1 ~]# 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的信息
- [root@db1 ~]# 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處理
- <VirtualHost *:80>
- ServerName www.yang.com
- DocumentRoot /tmp
- Include conf/extra/jkmount_yang.conf
- <Directory /tmp>
- Options +indexes
- Order allow,deny
- Allow from all
- Require all granted
- </Directory>
- </VirtualHost>
- [root@db1 ~]# cat /usr/local/apache/conf/extra/jkmount_yang.conf
- JkMount /* yang
- JkMount /status jkstatus
- JkunMount /test/* yang
5:重啓apache
- [root@db1 ~]# /usr/local/apache/bin/apachectl -t
- Syntax OK
- [root@db1 ~]# /usr/local/apache/bin/apachectl -k restart
6:修改tomcat配置文件以下,重啓tomcat實例,文件中主要修改jvmroute參數,同workers.properties中的一致
[root@db1 ~]# cat sr/local/tomcat7/conf/server.xml <Server port="8005" shutdown="SHUTDOWN"> <Service name="Catalina"> <!-- 配置集羣session 同步相關 --> <Host name="localhost" appBase="webapps" </Host> |
7:測試
[root@db1 ~]# tail -f /usr/local/apache/logs/mod_jk.log
[Thu Aug 16 16:45:23 2012]yang www.yang.com 0.017531
[Thu Aug 16 16:45:23 2012]yang www.yang.com 0.003080
[Thu Aug 16 16:45:23 2012]yang www.yang.com 0.001009
[Thu Aug 16 16:45:23 2012]yang www.yang.com 0.001876