原创 服务器&存储

我们这里只讲apache 和tomcat的整合,apache 和 tomcat都安装成功 并测试正常

apache的安装路径为:/usr/local/httpd 文档主目录为 /usr/local/httpd/htdocs

tomcat的安装路径为:/usr/local/tomcat6 文档主目录为 /usr/local/tomcat/webapps

网站路径为:/data/webapps (以公司网站为例)

有关apache 和 tomcat的安装与配置请查看我其它的文章:

《linux 下 mysql+apache+php+gd 的安装和配置》http://jedy82.blog.51cto.com/425872/310392

《linux 下JDK6 + TOMCAT6 安装与配置》http://jedy82.blog.51cto.com/425872/271984

《 tomcat 6 故障解决 》http://jedy82.blog.51cto.com/425872/487618

 

一、安装 jakarta

jakarta可从以下地址下载,要注意版本

http://archive.apache.org/dist/tomcat/tomcat-connectors/jk2/source/jakarta-tomcat-connectors-jk2-2.0.4-src.tar.gz

 

1,解压缩连接器

[root@web ]# cd /usr/local/src
[root@webb src]# tar -zxvf /software/jakarta-tomcat-connectors-jk2-2.0.4-src.tar.gz

2,进入安装目录


[root@web src]# cd jakarta-tomcat-connectors-jk2-2.0.4-src/jk/native2
[root@web native2]# ./buildconf.sh
[root@web native2]# ./configure –with-apxs2=/usr/local/httpd/bin/apxs #改成你的apache安装目录.


3,make后,生成编译配置文件.就可以看到mod_jk2.so文件已经在你的apache/modules/ 中了


[root@web native2]# make
[root@web native2]# cd ../build/jk2/apache2

###以下两行只要一行就行了###
[root@web apache2]# /usr/local/httpd/bin/apxs -n jk2 -i mod_jk2.so
[root@web apache2]# cp /usr/local/src/jakarta-tomcat-connectors-jk2-2.0.4-src/jk/build/jk2/apache2/mod_jk2.so /usr/local/httpd/modules/
############################

4,编辑apache目录的 /usr/local/httpd/conf/httpd.conf的文件.(修改httpd.conf文件前最好作个备份)

[root@web apache2]# vi /usr/local/httpd/conf/httpd.conf

在LoadModule下添加
添加LoadModule jk2_module modules/mod_jk2.so
找到" DirectoryIndex" ,在index .html后面再添加index.jsp

 

二、修改配置相关文件


1、在apache/conf/中新建文件workers2.properties,修改后内容如下(注意其中目录要改为你的目录噢):

[root@web ~]# # more /usr/local/httpd/conf/workers2.properties

##############################################################

[logger.httpd]
file=/usr/local/httpd/logs/jk2.log

level=INFO
debug=1
[config]
file=/usr/local/httpd/conf/workers2.properties

level=INFO
debug=1

[channel.socket:127.0.0.1:8009]
host=127.0.0.1
port=8009
[shm]
file=/usr/local/httpd/logs/jk2.shm

#size=1048576
# define the worker
[ajp13:127.0.0.1:8009]
channel=channel.socket:127.0.0.1:8009
#[uri:/*]

[uri:/*.jsp]
[uri:/*.css]
[uri:/*.gif]
[uri:/*.js]
[uri:/*.action]

######################################################################

 

2、编辑httpd.conf,去除无关的行,完整内容见附件,内容为下(注意其中目录要改为你的目录噢) :
[root@web root] # more /usr/local/httpd/conf/httpd.conf

######################################################################

ServerRoot "/data/webapps/"

<IfModule !mpm_winnt.c>
< IfModule !mpm_netware.c>

</IfModule>
< /IfModule>

<IfModule !mpm_netware.c>
< IfModule !perchild.c>

</IfModule>
< /IfModule>

<IfModule !mpm_netware.c>
PidFile logs/httpd.pid
< /IfModule>

Timeout 300

KeepAlive On

MaxKeepAliveRequests 100

KeepAliveTimeout 15

<IfModule prefork.c>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
< /IfModule>

<IfModule worker.c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
< /IfModule>

<IfModule perchild.c>
NumServers 5
StartThreads 5
MinSpareThreads 5
MaxSpareThreads 10
MaxThreadsPerChild 20
MaxRequestsPerChild 0
< /IfModule>

<IfModule mpm_winnt.c>
ThreadsPerChild 250
MaxRequestsPerChild 0
< /IfModule>

<IfModule beos.c>
StartThreads 10
MaxClients 50
MaxRequestsPerThread 10000
< /IfModule>

<IfModule mpm_netware.c>
ThreadStackSize 65536
StartThreads 250
MinSpareThreads 25
MaxSpareThreads 250
MaxThreads 1000
MaxRequestsPerChild 0
MaxMemFree 100
< /IfModule>

<IfModule mpmt_os2.c>
StartServers 2
MinSpareThreads 5
MaxSpareThreads 10
MaxRequestsPerChild 0
< /IfModule>

Listen 80

LoadModule speling_module modules/mod_speling.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php5_module modules/libphp5.so
\\PHP支持
LoadModule jk2_module modules/mod_jk2.so

<IfModule !mpm_winnt.c>
< IfModule !mpm_netware.c>

User nobody
Group nobody
< /IfModule>
< /IfModule>

#管理员邮箱
ServerAdmin
root@web.com

ServerName 127.0.0.1

UseCanonicalName Off

DocumentRoot "/usr/local/httpd-2.0.61/htdocs"

<Directory />
Options FollowSymLinks
AllowOverride None
< /Directory>

<Directory "/usr/local/httpd-2.0.61/htdocs">

Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny
Allow from all

</Directory>

UserDir public_html

DirectoryIndex index.html index.jsp index.php index.html.var

AccessFileName .htaccess

<FilesMatch "^\.ht">
Order allow,deny
Deny from all
< /FilesMatch>

TypesConfig conf/mime.types

DefaultType text/plain

<IfModule mod_mime_magic.c>
MIMEMagicFile conf/magic
< /IfModule>

HostnameLookups Off

ErrorLog logs/error_log

LogLevel warn

LogFormat "%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %&gt;s %b" common
LogFormat "%{Referer}i -&gt; %U" referer
LogFormat "%{User-agent}i" agent

CustomLog logs/access_log common

ServerTokens Full

ServerSignature On

Alias /icons/ "/usr/local/httpd-2.0.61/icons/"

<Directory "/data/webapps/icons">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
< /Directory>

AliasMatch ^/manual(?:/(?:de|en|es|fr|ja|ko|ru))?(/.*)?$ "/data/webapps/manual$1"

<Directory "/usr/local/httpd-2.0.61/manual">
Options Indexes
AllowOverride None
Order allow,deny
Allow from all

<Files *.html>
SetHandler type-map
</Files>

SetEnvIf Request_URI ^/manual/(de|en|es|fr|ja|ko|ru)/ prefer-language=$1
RedirectMatch 301 ^/manual(?:/(de|en|es|fr|ja|ko|ru)){2,}(/.*)?$ /manual/$1$2
< /Directory>

ScriptAlias /cgi-bin/ "/usr/local/httpd-2.0.61/cgi-bin/"

< IfModule mod_cgid.c>
< /IfModule>

< Directory "/usr/local/httpd-2.0.61/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
< /Directory>

IndexOptions FancyIndexing VersionSort

AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip

AddIconByType (TXT,/icons/text.gif) text/*
AddIconByType (IMG,/icons/p_w_picpath2.gif) p_w_picpath/*
AddIconByType (SND,/icons/sound2.gif) audio/*
AddIconByType (VID,/icons/movie.gif) video/*

AddIcon /icons/binary.gif .bin .exe
AddIcon /icons/binhex.gif .hqx
AddIcon /icons/tar.gif .tar
AddIcon /icons/world2.gif .wrl .wrl.gz .vrml .vrm .iv
AddIcon /icons/compressed.gif .Z .z .tgz .gz .zip
AddIcon /icons/a.gif .ps .ai .eps
AddIcon /icons/layout.gif .html .shtml .htm .pdf
AddIcon /icons/text.gif .txt
AddIcon /icons/c.gif .c
AddIcon /icons/p.gif .pl .py
AddIcon /icons/f.gif .for
AddIcon /icons/dvi.gif .dvi
AddIcon /icons/uuencoded.gif .uu
AddIcon /icons/script.gif .conf .sh .shar .csh .ksh .tcl
AddIcon /icons/tex.gif .tex
AddIcon /icons/bomb.gif core

AddIcon /icons/back.gif ..
AddIcon /icons/hand.right.gif README
AddIcon /icons/folder.gif ^^DIRECTORY^^
AddIcon /icons/blank.gif ^^BLANKICON^^

DefaultIcon /icons/unknown.gif

ReadmeName README.html
HeaderName HEADER.html

IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t

AddLanguage ca .ca
AddLanguage cs .cz .cs
AddLanguage da .dk
AddLanguage de .de
AddLanguage el .el
AddLanguage en .en
AddLanguage eo .eo
AddLanguage es .es
AddLanguage et .et
AddLanguage fr .fr
AddLanguage he .he
AddLanguage hr .hr
AddLanguage it .it
AddLanguage ja .ja
AddLanguage ko .ko
AddLanguage ltz .ltz
AddLanguage nl .nl
AddLanguage nn .nn
AddLanguage no .no
AddLanguage pl .po
AddLanguage pt .pt
AddLanguage pt-BR .pt-br
AddLanguage ru .ru
AddLanguage sv .sv
AddLanguage zh-CN .zh-cn
AddLanguage zh-TW .zh-tw

LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW

ForceLanguagePriority Prefer Fallback

AddCharset ISO-8859-1 .iso8859-1 .latin1
AddCharset ISO-8859-2 .iso8859-2 .latin2 .cen
AddCharset ISO-8859-3 .iso8859-3 .latin3
AddCharset ISO-8859-4 .iso8859-4 .latin4
AddCharset ISO-8859-5 .iso8859-5 .latin5 .cyr .iso-ru
AddCharset ISO-8859-6 .iso8859-6 .latin6 .arb
AddCharset ISO-8859-7 .iso8859-7 .latin7 .grk
AddCharset ISO-8859-8 .iso8859-8 .latin8 .heb
AddCharset ISO-8859-9 .iso8859-9 .latin9 .trk
AddCharset ISO-2022-JP .iso2022-jp .jis
AddCharset ISO-2022-KR .iso2022-kr .kis
AddCharset ISO-2022-CN .iso2022-cn .cis
AddCharset Big5 .Big5 .big5

AddCharset WINDOWS-1251 .cp-1251 .win-1251
AddCharset CP866 .cp866
AddCharset KOI8-r .koi8-r .koi8-ru
AddCharset KOI8-ru .koi8-uk .ua
AddCharset ISO-10646-UCS-2 .ucs2
AddCharset ISO-10646-UCS-4 .ucs4
AddCharset UTF-8 .utf8

AddCharset GB2312 .gb2312 .gb
AddCharset utf-7 .utf7
AddCharset utf-8 .utf8
AddCharset big5 .big5 .b5
AddCharset EUC-TW .euc-tw
AddCharset EUC-JP .euc-jp
AddCharset EUC-KR .euc-kr
AddCharset shift_jis .sjis

AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php

AddHandler type-map var

BrowserMatch "Microsoft Data Access Internet Publishing Provider" redirect-carefully
BrowserMatch "MS FrontPage" redirect-carefully
BrowserMatch "^WebDrive" redirect-carefully
BrowserMatch "^WebDAVFS/1.[0123]" redirect-carefully
BrowserMatch "^gnome-vfs" redirect-carefully
BrowserMatch "^XML Spy" redirect-carefully
BrowserMatch "^Dreamweaver-WebDAV-SCM1" redirect-carefully

<IfModule mod_ssl.c>
Include conf/ssl.conf
< /IfModule>

~~~~~~~~~~添加以下内容~~~~~~~~~~~~~~~~~

NameVirtualHost *
< VirtualHost * >
ServerName localhost
DocumentRoot /usr/local/httpd/htdocs/
< /VirtualHost>

#<VirtualHost * >
#ServerAlias www.web1.com www.web2.com \\多个域名
#DocumentRoot /data/webapps/
#Alias /dotproject /data/webapps/dotproject \\另外的路径 可根据情况自行添加
#</VirtualHost>

<VirtualHost * >
ServerName www.web.com \\你的域名
DocumentRoot /data/webapps/web/
Alias /dotproject /data/webapps/dotproject \\另外的路径 可根据情况自行添加
< /VirtualHost>

<VirtualHost * >
ServerName www.web1.com \\你的域名
DocumentRoot /data/webapps/web1/
Alias /dotproject /data/webapps/dotproject \\另外的路径 可根据情况自行添加
< /VirtualHost>

<VirtualHost * >
ServerName www.web2.com \\你的域名
DocumentRoot /data/webapps/web2/
Alias /dotproject /data/webapps/dotproject \\另外的路径 可根据情况自行添加
< /VirtualHost>

<Directory "/data/webapps/dotproject/">
Order allow,deny
Allow from all
< /Directory>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

######################################################################

3、编辑server.xml,完整内容见附件,内容为下(注意其中目录要改为你的目录噢) :
[root@web root] # more /usr/local/tomcat/conf/server.xml

 

######################################################################

 

<?xml version='1.0' encoding='utf-8'?>
< !--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
< !-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">

<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>

<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
&lt;Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
--&gt;
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
&lt;Connector executor="tomcatThreadPool"
port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
--&gt;
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->
<!--
&lt;Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
--&gt;

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->

<!-- You should set jvmRoute to support load-balancing via AJP ie :
&lt;Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
--&gt;
<Engine name="Catalina" defaultHost="
www.xxx.com">

<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
&lt;Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
--&gt;

<!-- The request dumper valve dumps useful debugging information about
the request and response data received and sent by Tomcat.
Documentation at: /docs/config/valve.html -->
<!--
&lt;Valve className="org.apache.catalina.valves.RequestDumperValve"/>
--&gt;

<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>

<!-- Define the default virtual host
Note: XML Schema validation will not work with Xerces 2.2.
-->
<Host name="localhost" appBase="/usr/local/tomcat/webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
</Host>

<Host name="www.web.com" appBase="/data/webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/data/webapps/web" reloadable="true" caseSensitive="false" debug="0"/>
</Host>

<Host name="www.web1.com" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Alias>www.web1.cn</Alias> \\别名主机
<Context path="" docBase="/data/webapps/web1" reloadable="true" caseSensitive="false" debug="0"/>
\\主机信息段 结束,多主机可重复此信息
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
&lt;Valve className="org.apache.catalina.authenticator.SingleSignOn" />
--&gt;

<!-- Access log processes all example.
Documentation at: /docs/config/valve.html -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>

</Host>
</Engine>
</Service>
< /Server>

######################################################################
[root@web root] #

[root@web root] # ln -sv /usr/local/tomcat/webapps /data/webapps \\添加网站目录
[root@web root] # chown -R tomcat /data/webapps \\赋予tomcat用户的所有权

 

[root@web root] # vi /etc/hosts \\添加DNS记录

在浏览器里输入http://www.web.com/webhttp://www.web.com:8080/web得到的信息应该是一样的

 

本文出自 “jedy 网络技术&linux学习” 博客,请务必保留此出处http://jedy82.blog.51cto.com/425872/310398