tomcat 为一个jsp的容器,apache为一个web server,两者之间通信通过worker进行(由Tomcat使用Server.xml文件中Connector的标签来定义其端口和协议),通过 mod_jk的模块(由web服务器像apache、iis等使用)和Web Server通信。
通信协议有很多,其中jk2已经被抛弃了,现在就用jk就好了。
整个过程其实就是让apache的httpd.conf文件调用mod_jk.conf,mod_jk.conf调用workers.properties,最后配置虚拟主机。
1.所需软件
tomcat5.5.9
apache2.2.0
jakarta-tomcat-connectors-1.2.15-src.tar.gz
2.tomcat和apache安装相对简单.
访问:http://ip:8080 出现tomcat的欢迎界面,tomcat安装成功
访问:http://ip出现apache的欢迎界面,apache安装成功
3.安装jakarta-tomcat-connectors-1.2.15-src.tar.gz
执行: tar zxvf jakarta-tomcat-connectors-1.2.15-src.tar.gz //解压包
cd jakarta-tomcat-connectors-1.2.15-src/jk/native //进入目录
./buildconf.sh
./configure --with-apxs=/usr/local/apache2/bin/apxs
make && make install mod_jk.so模块自动生成到apache2/modules目录下
4.配置apache和tomcat(关键)
A. 配置apache httpd.conf文件
执行:cd /usr/local/apache2/conf
vi httpd.conf 修改: httpd.conf 文件增加下列项##############################
NameVirtualHost 10.10.100.17
Include /usr/local/apache2/conf/vhost/*.conf
对WEB-INF禁止访问
<Directory "path/WEB-INF">
AllowOverride None
Deny from All
</Directory>
然后httpd.conf同级目录下新建目录
vhost
在vhost目录中新建mod_jk.conf,添加内容如下:
# Load mod_jk module
LoadModule jk_module modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile /usr/local/apache2/conf/workers.properties
# Where to put jk logs
JkLogFile /usr/local/apache2/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel error
# 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"
JkShmFile /usr/local/apache2/logs/mod_jk.shm
# Send jsp,servlet for context * to worker named worker1
JkMount /status/* worker1
JkMount /*.jsp worker1
JkMount /servlet/* worker1
JkMount /* worker1
在vhost目录下继续添加test.conf配置文件. 修改如下:
<Directory "/www/test/www">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<VirtualHost 10.10.100.17>
DocumentRoot "/www/test/www" #主程序发布目录
ServerName www.test.com
ErrorLog logs/localhost-error_log
CustomLog logs/localhost-access_log common
</VirtualHost>
在conf同级目录中建立 workers.properties文件添加如下内容:
# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=127.0.0.1 #本机,若上面Tomcat主机不为localhost,作相应修改
worker.worker1.port=8009
worker.worker1.lbfactor=1
worker.worker1.cachesize=128
worker.worker1.cache_timeout=600
worker.worker1.socket_keepalive=1
worker.worker1.reclycle_timeout=300
apache目录权限修改:
chown –R nobody:nobody apache2 //修改apache2目录的用户和组
这里要修改下httpd.conf文件里下列两项如下: 不然无权限访问
User nobody
Group nobody
B.tomcat中配置:
主要配置server.xml
vi server.xml //编辑server.xml文件如下:
//虚拟主机 (部署的程序在/www/test/www)
<Host name="www.test.com" debug="0" appBase="/www/test" unpackWARs="false" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="www" path="" reloadable="true">
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="iccess." suffix=".log" timestamp="true"/>
</Context>
</Host>
注:appBase是程序的基本目录
docBase是jsp或者html的所在目录
5.测试
在/www/test/www目录存放jsp 或者html文件
jsp代码如下
<html>
<title>
test jsp
</title>
<%
String showMessage="Oh My God!";
out.print(showMessage);
%>
</html>
访问 : http://www.test.com:8080/test.jsp 成功说明tomcat启动成功
访问 http://www.test.com/test.jsp 成功说明apache和tocmat整合成功
jakarta-tomcat-connectors-1.2.15-src.tar.gz 下载地址http://archive.apache.org/dist/jakarta/tomcat-connectors/jk/source/jk-1.2.15/
tomcat运行java虚拟机修改 bin/catalina.sh增加
#set JAVA_OPTS modified by Fencer
JAVA_OPTS=" -Xmx512M -Xms256M -Djava.awt.headless=true "
JAVA_HOME=/exports/home/cnistrs/j2sdk1.4.2_10