apache+tomcat整合(一)
apache与tomcat整合的方式有多种方式,这里介绍mod_jk方式:
系统环境:
[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)
软件环境:
apr-1.5.1.tar.gz
apr-util-1.5.4
libevent-2.0.22-stable.tar.gz
pcre-8.36.zip
httpd-2.2.29.tar.gz
tomcat-connectors-1.2.40-src.tar.gz
jdk-7u45-linux-x64.gz
apache-tomcat-6.0.43.tar.gz
1、安装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
2、安装tomcat,前面我已介绍如何安装tomcat,这里不做介绍,请看前面我的博客
3、编译生成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
4、在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
5、再配置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
6、修改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">
7、重启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
8、验证:
[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
- 修改配置文件之后,重新压缩catalina.jar文件:
-
cp catalina.jar catalina.jar.dat rm -f catalina.jar zip -r catalina.jar META-INF
-
tomcat报错时的显示页面显示:
-
HTTP Status 404 - /index.jsp
type Status report
message /index.jsp
description The requested resource is not available.
-
Apache Tomcat/X
打开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