apache與tomcat整合的方式有多種方式,這裏介紹mod_jk方式:html
系統環境:java
[root@ORACLE logs]# uname -a Linux ORACLE 2.6.18-308.el5 #1 SMP Fri Jan 27 17:17:51 EST 2012 x86_64 x86_64 x86_64 GNU/Linux [root@ORACLE logs]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 5.8 (Tikanga)
軟件環境:python
apr-1.5.1.tar.gzlinux
apr-util-1.5.4web
libevent-2.0.22-stable.tar.gzapache
pcre-8.36.zipbootstrap
httpd-2.2.29.tar.gzvim
tomcat-connectors-1.2.40-src.tar.gztomcat
jdk-7u45-linux-x64.gz服務器
apache-tomcat-6.0.43.tar.gz
一、安裝apache
# 編譯和安裝 apr 1.5 cd srclib/apr ./configure --prefix=/usr/local/apr-httpd/ make make install # 編譯和安裝 apr-util 1.5 cd ../apr-util ./configure --prefix=/usr/local/apr-util-httpd/ --with-apr=/usr/local/apr-httpd/ make make install # 配置 httpd cd ../../ ./configure --with-apr=/usr/local/apr-httpd/ --with-apr-util=/usr/local/apr-util-httpd/ --prefix=/usr/local/apache2.2
make && make install
#若是須要自動加載一些模塊,則進行以下編譯: ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-rewrite=shared --enable-proxy --enable-proxy=shared --enable-speling --enable-speling=shared --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
二、安裝tomcat,前面我已介紹如何安裝tomcat,這裏不作介紹,請看前面個人博客
三、編譯生成mod_jk
[root@ORACLE data]# tar xzvf tomcat-connectors-1.2.40-src.tar.gz [root@ORACLE data]# cd tomcat-connectors-1.2.40-src [root@ORACLE tomcat-connectors-1.2.40-src]# ls conf docs HOWTO-RELEASE.txt jkstatus LICENSE native NOTICE README.txt support tools xdocs [root@ORACLE tomcat-connectors-1.2.40-src]# cd native/ [root@ORACLE native]# ls aclocal.m4 buildconf.sh config.log configure iis Makefile.am README.txt TODO.txt apache-1.3 BUILDING.txt config.nice configure.ac libtool Makefile.in scripts apache-2.0 common config.status docs Makefile netscape STATUS.txt [root@ORACLE native]#./configure --with apxs=/usr/local/apache2.2/bin/apxs [root@ORACLE native]#make
四、在apache2.2的conf文件下中建立兩個文件:
mod_jk.conf workers.properties
[root@ORACLE native]# cd /usr/local/apache2.2/conf/ [root@ORACLE conf]# ls extra httpd.conf magic mime.types mod_jk.conf original workers.properties
編輯mod_jk.conf配置文件:
[root@ORACLE conf]# cat mod_jk.conf JkWorkersFile /usr/local/apache2.2/conf/workers.properties # Where to put jk logs JkLogFile /usr/local/apache2.2/logs/mod_jk.log # Set the jk log level [debug/error/info] JkLogLevel info # Select the log format JkLogStampFormat "[%a %b %d %H:%M:%S %Y]" # JkOptions indicate to send SSL KEY SIZE, JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories # JkRequestLogFormat set the request format JkRequestLogFormat "%w %V %T" # 將全部servlet 和jsp請求經過ajp13的協議送給Tomcat,讓Tomcat來處理 JkMount /servlet/* worker1 JkMount /*.jsp worker1
編輯workers.properties:
注意:worker1爲tomcat的名字,等會配置tomcat時和這個名字一致
[root@ORACLE conf]# cat workers.properties # Defining a worker named worker1 and of type ajp13 worker.list=worker1 # Set properties for worker1 worker.worker1.type=ajp13 worker.worker1.host=localhost worker.worker1.port=8009 worker.worker1.lbfactor=50 worker.worker1.cachesize=10 worker.worker1.cache_timeout=600 worker.worker1.socket_keepalive=1 worker.worker1.socket_timeout=300
五、再配置httpd.conf,做如下修改:
將Listen 80 修改成 Listen 127.0.0.1:80
將ServerName 修改成 ServerName LocalHost:80
在DirectoryIndex中添加 index.jsp
個人網頁放在/var/www/html下,因此要修改DocumentRoot
Listen 80 ServerName localhost:80 DocumentRoot "/var/www/html" <Directory "/var/www/html"> AllowOverride None Order allow,deny Allow from all XBitHack on </Directory> <IfModule dir_module> DirectoryIndex index.html index.jsp </IfModule> #添加加載mod_jk模塊的語句 LoadModule jk_module modules/mod_jk.so Include /usr/local/apache2.2/conf/mod_jk.conf
六、修改tomcat server.xml配置文件:
#在host段中加入context段,氣質docBase爲web目錄 <Context path="" docBase="/var/www/html" debug="0" reloadable="true" crossContext="true"/>
添加engine:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1">
七、重啓apache和tomcat服務器
#開啓tomcat服務 [root@ORACLE conf]# ../bin/catalina.sh start Using CATALINA_BASE: /usr/local/tomcat6.0 Using CATALINA_HOME: /usr/local/tomcat6.0 Using CATALINA_TMPDIR: /usr/local/tomcat6.0/temp Using JRE_HOME: /usr/local/jdk1.7 Using CLASSPATH: /usr/local/tomcat6.0/bin/bootstrap.jar [root@ORACLE conf]# ps aux|grep java root 6159 156 4.3 519068 45072 pts/1 Sl 10:36 0:01 /usr/local/jdk1.7/bin/java -Djava.util.logging.config.file=/usr/local/tomcat6.0/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat6.0/endorsed -classpath /usr/local/tomcat6.0/bin/bootstrap.jar -Dcatalina.base=/usr/local/tomcat6.0 -Dcatalina.home=/usr/local/tomcat6.0 -Djava.io.tmpdir=/usr/local/tomcat6.0/temp org.apache.catalina.startup.Bootstrap start root 6170 0.0 0.0 61176 724 pts/1 R+ 10:36 0:00 grep java
#開啓apache服務: apachectl -k start
八、驗證:
[root@ORACLE conf]# curl -I http://127.0.0.1/index.jsp HTTP/1.1 200 OK Date: Fri, 03 Apr 2015 03:29:39 GMT Server: Apache/2.2.29 (Unix) mod_jk/1.2.40 Set-Cookie: JSESSIONID=1B7E68B32F64A807AC695F13F2851E58.worker1; Path=/ Content-Length: 65 Content-Type: text/html
如上圖,咱們能夠看到apache的版本號暴露了,因此,咱們須要將apache的版本號隱藏起來。
更改配置文件httpd.conf,添加以下兩行配置:
ServerSignature Off
ServerTokens Prod
ServerSignature出如今Apache所產生的像404頁面、目錄列表等頁面的底部
ServerTokens目錄被用來判斷 Apache會在Server HTTP響應包的頭部填充什麼信息
重啓apache服務
隱藏tomcat版本號信息:
到apache-tomcat安裝目錄下的lib子文件夾,找到catalina.jar這包,並進行解 unzip catalina.jar
解壓以後進度到org/catalina/apache/util/目錄下,編輯ServerInfo.properties
vim /path/tomcat/lib/org/apache/catalina/util/ServerInfo.properties
原有配置: server.info=Apache Tomcat/6.0.43 server.number=6.0.43.0 server.built=Nov 14 2014 10:04:27 UTC 修改後: server.info=Apache Tomcat/X server.number=X server.built=X
cp catalina.jar catalina.jar.dat rm -f catalina.jar zip -r catalina.jar META-INF
tomcat報錯時的顯示頁面顯示:
type Status report
message /index.jsp
description The requested resource is not available.
打開tomcat訪問日誌:
#修改server.xml文件,增長以下一句 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
查看tomcat日誌:
127.0.0.1 - - [07/Apr/2015:13:57:25 +0800] "GET /bbs/time.jsp HTTP/1.1" 200 43 127.0.0.1 - - [07/Apr/2015:13:58:06 +0800] "GET /bbs/time.jsp HTTP/1.1" 200 43 127.0.0.1 - - [07/Apr/2015:13:59:30 +0800] "GET /bbs HTTP/1.1" 302 - 127.0.0.1 - - [07/Apr/2015:13:59:30 +0800] "GET /bbs/ HTTP/1.1" 200 130 127.0.0.1 - - [07/Apr/2015:13:59:41 +0800] "GET /bbs/ HTTP/1.1" 200 130 127.0.0.1 - - [07/Apr/2015:14:01:40 +0800] "GET /bbs/index.jsp HTTP/1.1" 200 174 127.0.0.1 - - [07/Apr/2015:14:01:40 +0800] "GET /bbs/index.jsp HTTP/1.1" 200 174