Utilize shell scripts,linux commands, open source tools,java, to Maximize the Power of Linux.
Focused on working with linux and shell, search engine technology including Chinese segmenter
Any questions please contact me at gmail: david.ullua

2/02/2007

Apache 2.2.4, tomcat5.0.28 整合,以及中文乱码问题解决

Author: David Euler, 2007, david.ullua _at_ gmail (请用gmail.com替换gmail)

这篇文章主要参考从google搜索到的整合方法和问题解决方法,以及apache.org官方网站的文档。

并经过实际的验证 (os: slackware 11, 软件版本参考安装)。

目录

I.安装Apache, Tomcat, tomcat-connector (mod_jk) , 并整合apache与tomcat

II.tomcat, apache整合后,去掉tomcat的8080端口服务,用户只能通过80端口访问服务器

III.Tomcat/Apache乱码问题解决.

IV.虚拟主机配置

---------------------------------------------------------------------------------------------------------------

I.安装Apache, Tomcat, tomcat-connector (mod_jk) , 并整合apache与tomcat

共分5个步骤,详述如下

1、准备,下载需要的文件。这里假定你已经正确安装配置好了JDK。

到Apache官方网站下载所需要的文件:

httpd-2.2.4.tar.gz , apache的httpd server包 (需要编译安装,安装时)

apache-tomcat-5.5.20.tar.gz, apache的tomcat包, 不需要编译安装(解压即可)

tomcat-connectors-1.2.20-src.tar.gz (mod_jk的安装包)

2、安装Apache。

# tar xzvf httpd-2.2.4.tar.gz

或 tar jxvf httpd-2.2.4.tar.bz2

# cd httpd-2.2.4

参考INSTLL安装文档,如下即可安装apache(自带有cgi,perl, apxs的支持):

# ./configure --prefix=/usr/local/apache2 (不需要加--enable-so参数,

# make

# make install

3、安装Tomcat (解压即可)

# cp apache-tomcat-5.5.20.tar.gz /usr/local/

# cd /usr/local

# tar xzvf apache-tomcat-5.5.20.tar.gz

# ln -s apache-tomcat-5.5.20 tomcat

4、编译生成mod_jk

# tar xzvf tomcat-connectors-1.2.20-src.tar.gz

# cd tomcat-connectors-1.2.20-src/jk/native

# ./configure --with-apxs=/usr/local/apache2/bin/apxs

# make

# cp ./apache-2.0/mod_jk.so /usr/local/apache2/modules/

5、配置

在/usr/local/apache2/conf/下面建立两个配置文件mod_jk.conf和workers.properties。

# vi mod_jk.conf

添加以下内容:

# 指出mod_jk模块工作所需要的工作文件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 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

# vi workers.properties

添加以下内容:

# Defining a worker named worker1 and of type ajp13,

# 注意8009是tomcat的ajp13端口,注意不是8080

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

6.再配置httpd.conf,作以下修改:

将Listen 80 修改为 Listen 127.0.0.1:80

将ServerName 修改为 ServerName localhost:80

在DirectoryIndex中添加 index.jsp, 假设网页放在/var/wwwroot下,要修改DocumentRoot

DocumentRoot "/var/wwwroot"

<Directory "/var/wwwroot">

Options Includes FollowSymLinks

AllowOverride None

Order deny,allow

Allow from all

XBitHack on

</Directory>

<Directory "/var/wwwroot/WEB-INF">

Order deny,allow

Deny from all

</Directory>

增加关于加载mod_jk的语句:

LoadModule jk_module modules/mod_jk.so

Include /usr/local/apache2/conf/mod_jk.conf

在/var/wwwroot下建立一个index.jsp,启动Apache和Tomcat,用浏览器访问http://localhost/,应可看到正确的页面。

---------------------------------------------------------------------------------------------------------------

II.tomcat, apache整合后,去掉tomcat的8080端口服务,用户只能通过80端口访问服务器 (以统一日志分析等的处理):

找到server.xml, 去掉port="8080"

<Connector port="8080" maxHttpHeaderSize="8192"

maxThreads="5000" minSpareThreads="25" maxSpareThreads="75"

enableLookups="false" redirectPort="8443" acceptCount="100"

connectionTimeout="20000" disableUploadTimeout="true"

URIEncoding="UTF-8" useBodyEncodingForURI="true" />

---------------------------------------------------------------------------------------------------------------

III.Tomcat/Apache乱码问题解决

首先,确保 httpd.conf中的下面一行的注释没有打开(未配置AddDefaultCharset ):

#AddDefaultCharset ISO-8859-1

然后依情况按步骤1), 2)操作

如果你的网页中定义的charset为UTF-8, 如下

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

那么步骤1-2用UTF-8替代GBK

1)如果你将apache和tomcat整合了,那么修改tomcat/conf中的server.xml

<Connector port="8009" enableLookups="false" redirectPort="8443" debug="0"

protocol="AJP/1.3" URIEncoding="GBK"/>

添加URIEncoding="GBK",请确保你的jsp的开头:

<%@page contentType="text/html;charset=GBK"%>

里的charset和URIEncoding一致。 (均为GBK或UTF-8)

2)如果没有将tomcat和apache整合,那么修改如下的标签:

<Connector

port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

enableLookups="false" redirectPort="8443" acceptCount="100"

debug="0" connectionTimeout="20000"

disableUploadTimeout="true" URIEncoding="GBK"/>

参考:

http://www.gbunix.com/htmldata/2006_08/15/23/article_1383_1.html

http://dev.cbw.com/web/apache/2005524308_3480676.shtml

---------------------------------------------------------------------------------------------------------------

IV.虚拟主机配置:

删除掉下面这一行的#(取消注释)

#Include conf/extra/httpd-vhosts.conf

vi conf/extra/httpd-vhosts.conf, 根据文件中的例子添加虚拟主机。

参考:

http://httpd.apache.org/docs/2.2/vhosts/name-based.html

http://httpd.apache.org/docs/2.2/vhosts/examples.html

No comments:

About Me

I am a senior developer and a team leader with 3 years development experience in Suzhou, China, focus on mobile web search, linux, Java and machine learning in NLP (natural language processing). My goal is to improve people's life with computer technology.