`
h819
  • 浏览: 89786 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Windows Apache Tomcat PHP mysql work together

阅读更多
# title : Windows Apache Tomcat PHP mysql work together
#
# Author H819
# 2005.08.19



**********
*过程分析*
**********
apache 的 httpd.conf 文件调用一个 mod_jk.conf 文件,mod_jk.conf 文件声明了加载的 mod_jk.so 模块,并且定义该模块的一些设置参数,其中就包括和 tomcat 连接的时候的一些参数,而这些参数又在 works.properties 中声明。
php 是独立的模块,加载即可。


******
*环境*
******
window xp sp2
Apache server:   http://httpd.apache.org/    2.0.50
Tomcat server:   http://jakarta.apache.org/site/downloads/downloads_tomcat.html   Tomcat 5.0.28 (Binary)
Jdk1.4.2_08      http://java.sun.com
jk-1.2.14:       http://jakarta.apache.org/site/downloads/downloads_tomcat.html   选择 JK windwos 版本 (JK Binary Releases)
php-5.0.0-Win32.zip  http://www.php.net/
MySQL 4.1 http://dev.mysql.com/downloads/
MySQL Connector/J http://dev.mysql.com/downloads/  for connecting to MySQL from Java

**********
*注意事项*
**********
1. 安装过程,任何目录和文件名都不要使用空格,如不要使用D:\Program Files 而使用D:\ProgramFiles。空格问题可能使软件访问出现问题,也许随着版本的提高,这个问题不会再出现,但是我们需要注意这一点。
2. 把上面所有的安装程序和网站的文档都保存到同一个目录下面如 D:\www,这样也方便日常管理配置和将来移植到别的服务器上去。
3. jk or jk2 ?
jk2 已经停止开发 , jk 是目前推荐的连接器。
4. 每个配置文件,我都用英文做了一些注释,要注意阅读。具体应用的时候,修改相关的配置,直接拷贝过去即可。

*************
*安装 Apache*
*************

1. apache_2.0.52-win32-x86-no_ssl
双击安装到D:\www\Apache2 注意,目录名不要包含空格,否则下面设置php会出错。
安装结束后,apache自动运行,在浏览器里输入http://127.0.0.1,应该显示出了默认的欢迎网页。
(在 管理->服务 中,可以设定为手工运行方式,在开发阶段很有用处,不用的时候,可以停止他,以节省资源,并且可以随时重启等)
(unix 下面的运行命令,待完善)

2. 修改 httpd.conf 中的默认配置
2.1 ServerRoot 如果安装过程顺利的话,这个参数的值会自动设置正确(在unix 中,不是安装的,要手工改写);
2.2 DocumentRoot 默认的应用目录(如 E:/www/Apache2/MyDocRoot);
2.3 AddDefaultCharset 使 apache 支持中文:AddDefaultCharset ISO-8859-1 改为 AddDefaultCharset GBK
......

3. 注意事项
3.1 httpd.conf 中,目录分隔符用的是“/”,不是 windows 的反斜杠"\"
3.2 检测 httpd.conf 语法的工具 apache.exe
3.2.1 检测 httpd .conf 语法 apache.exe -t
3.2.2 检测 httpd.conf 中虚拟机的设置 apache.exe -S
3.2.3 ...
全部命令列表: apache.exe -h

****************
*安装 Java2 SDK*
****************

1. 如果从 sun 官方网站上面下载安装版本(.exe),安装完成之后,相应的会在系统的环境变量中设置相应的变量。

2. jbuilder 带有 jdk (或者其他的免安装的 jdk),tomcat 直接指定该 jdk 即可。(推荐)

附注:如果想要在命令行执行 javac,java,jar 等 java 命令,需要设置环境变量,方法如下:

共有三个环境变量需要设置  JAVA_HOME, path 和 classpath
(假定 jdk1.4 安装在 E:/www/jdk1.4 下面)

桌面上右击"我的电脑"属性,弹出"系统特性"对话框,选择"高级"标签,单击"环境变量"按钮,弹出"环境变量"对话框,在"系统变量"中,单击"新建"按钮,在弹出的"新建系统变量"对话框中:
1) 在"变量名"中写:JAVA_HOME,在"变量值"中写:D:\www\JDK1.4,然后确定;
2) 在变量名中写入:path,变量值中写入:追加 .;%JAVA_HOME%\bin 然后确定;
3) 在变量名中写入: classpath ,变量值中写入:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; 然后确定;
4) 命令 path 检验 path 的设置情况;
5) 验证设置是否正确,在命令行中输入 java ,如果返回提示信息,则表明设置正确。

说明:
1) classpath 变量: 用命令行执行java命令的时候,class 加载的寻径路径,注意,不要漏写圆点 . , 这是指当前路径,同样 path 也不要忘记圆点。
2) 变量引用 %% ,这是 dos 命令引用的格式,在设置其他的环境变量的时候,也可以用此方法来简写变量。
.;%JAVA_HOME%\bin 等同 .;D:\www\JDK1.4\bin
3) windows 中,用户变量只对当前的用户有效,而系统变量对所有的用户都有效。(为了使设置的环境变量生效,用户变量需要注销系统,而系统变量需要重新启动系统 --- 不能确定,有时候即时生效 JH)

*************
*安装 Tomcat*
*************

选择 zip 版(Binary releases): jakarta-tomcat-5.0.28.zip
1.解压至 D:\www\tomcat5

2.设置JAVA_HOME、CATALINA_HOME环境变量(假定 jdk1.4 安装在 E:/www/jdk1.4 下面)
桌面上右击"我的电脑"属性,弹出"系统特性"对话框,选择"高级"标签,单击"环境变量"按钮,弹出"环境变量"对话框,在"系统变量"中,单击"新建"按钮,在弹出的"新建系统变量"对话框中:
1)在"变量名"中写:JAVA_HOME,在"变量值"中写:D:\www\JDK1.4,然后确定;(如果在 jdk 中已经设置完毕,此步骤就可以省略了)
2)在变量名中写入:CATALINA_HOME,在"变量值"中写入:D:\www\tomcat5 然后确定。
3) Tomcat的启动、关闭:双击 D:\ApacheGroup\tomcat5\bin\setup.bat启动Tomcat服务器,双击 D:\ApacheGroup\tomcat5\bin\shutdown.bat关闭Tomcat服务器(或者在控制台 ctr+c) 。
(unix 下面的运行命令,待完善)
4) 测试:启动Tomcat,在IE地址中输入:http://localhost:8080出现 tomcat 欢迎页面,则说明安装成功 。

************************************
*Apache Tomcat 结合,虚拟目录的实现*
************************************

假定应用情况:
定义两个虚拟主机:testa , testb(testa: 一个虚拟主机,两个应用;testb :一个虚拟主机一个应用)
虚拟主机 testa 有两个应用(数目无限制),testa 和 testa/a/
testa 应用的根目录在 E:/tomcat-5.0.18/webapps/servlets-examples
testa/a/ 应用的根目录在 E:/tomcat-5.0.18/webapps/jsp-examples/
testb 应用的根目录在 E:/ApacheGroup/Apache2/DocumentRoot
testa 可以通过 http://testa 访问
testa/a/ 可以通过 http://testa/a/ 访问(对于有多个子应用的系统,非常有用处,域名不用变化)
testb 可以通过 http://testb 访问
注:
应用目录所在位置任意,但是要注意,用绝对路径;
这里只是为了说明目录可以随意设定,才举例应用位置在上面比较复杂的目录,实际开发的时候,要统一到一个目录中:D:\www\webapp\testa
D:\www\webapp\testa.a
D:\www\webapp\testb

http://apache.justdn.org/jakarta/tomcat-connectors/jk/binaries/win32/jk-1.2.14/
apache:
1. 选择 mod_jk-1.2.14-apache-2.0.54.so ,重命名为 mod_jk.so,拷贝到APAHCE_HOME\modules目录下面;
2. 拷贝 workers.properties , mod_jk.conf ,  VirtualHost.conf 三个文件到 APAHCE_HOME\conf 中;
3. 仔细阅读之后,修改 workers.properties , mod_jk.conf ,  VirtualHost.conf 中的参数为实际应用的数值;
4. 修改 httpd.conf ,在文件的最后,引入配置文件
include conf/mod_jk.conf
include conf/VirtualHost.conf
(httpd.conf 支持 include 命令,这样可以做到尽量少的修改原文件,易于管理。在 httpd.conf 中,默认的根路径为 apache_home,设置的相对路径,均从此目录开始。)
5. 检查 httpd.conf 语法和虚拟主机的设置
apache.exe -t     apache.exe -S

tomcat:
6. 修改 Tomcat_home/conf/server.xml
6.1 确保
    
     <Connector port="8009"
               enableLookups="false" redirectPort="8443" debug="0"
               protocol="AJP/1.3" />
没有被注释掉。

6.2 在 <Engine></Engine> 中,<Host></Host> 同级,加入如下内容

<Host name="testa" debug="0" appBase="E:/tomcat-5.0.18/webapps/servlets-examples"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">     
  <Logger className="org.apache.catalina.logger.FileLogger"
           directory="E:/tomcat-5.0.18/webapps/servlets-examples/logs"  prefix="standard_log." suffix=".txt"
      timestamp="true"/>
</Host>

<Host name="testb" debug="0" appBase="E:/ApacheGroup/Apache2/DocumentRoot"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">     
  <Logger className="org.apache.catalina.logger.FileLogger"
           directory="E:/ApacheGroup/Apache2/DocumentRoot/logs"  prefix="standard_log." suffix=".txt"
      timestamp="true"/>
</Host>

注意:
这是两个虚拟主机,appBase 指定的是绝对路径,修改成自己实际应用;
如果 server.xml 设置的不正确,tomcat 启动会发生异常。

7. 在 Tomcat_home/conf/Catalina 中,建立两个目录 testa ,testb,表示两个虚拟主机,目录名称要和虚拟主机的名字一致( tomcat 启动的时候,可以根据 server.xml 中设定的虚拟主机的名字自动建立相应的文件夹,但是有的时候不会,需要检查);
在 testa 目录中,建立两个文件 testa.xml, a.xml(文件的名字随意,为了便于记忆,和应用名相同)
在 testb 目录中,建立 testb.xml
注:
testa.xml , a.xml , testb.xml 文件内容见示例文件,注意不同的目录中的文件内容,要对应相应当主机;
三个文件的内容也可以加到 server.xml 中,相应的虚拟主机下面,单独的文件,便于管理。

注:
步骤 6,7是 tomcat 的虚拟主机的设置方法,可以独立使用,和 apache 的设置没有关系;
在虚拟主机中加一个应用,在相应的文件夹下面声明一个配置文件即可;
如果和 apache 联合应用,两者的虚拟主机的设置要一致。


8. 测试
访问
http://testa
http://testa/a/
http://testb

********************
*apache 和 PHP 结合*
********************
http://www.php.net/ 下载 php-5.0.0-Win32.zip 
解压缩其内容到 D:\www\php5

1. 找到 php5 目录下的 php.ini-dist 重命名为 php.ini 并复制到 C:\WINDOWS 目录下;
2. 找到 php5 目录下的 php5ts.dll,libmysql.dll 并复制到 C:\windows\system 目录下;
//libmysql.dll 用以支持 mysql
3. 找到DirectoryIndex index.html index.html.var 在后面加入 index.htm index.php index.php3 ;
4. LoadModule php5_module D:/www/php5/php5apache2.dll  //加入模块
5. 查找 #AddEncoding x-gzip .gz .tgz ,在下面的行中加入 AddType application/x-httpd-php .php .phtml .php3 .php4
//即增加解析文件类型为php.phtml.php3.php4
6. 测试
建立一个文本文件,info.php, 加入一句 <?php  phpinfo(); ?>
浏览 http://yourhost/info.php ,如果看见 php 信息页面,php 安装成功。

****************************
*Apache Tomcat PHP 综合测试*
****************************
如果上述步骤都完成了,三者结合即完成。

*****************
*PHP mysql 结合 *
*****************
安装到 D:\www\mysql4.1 中(可以象管理 apache 一样来控制其启动)

1. 如果 php 的版本为 php5  ,需要把 php 安装目录中的 libmysql.dll  和 libmysqli.dll 拷贝到 windows/system32 下;
2. 进入C:\WINDOWS 打开 php.ini
2.1 找到extension_dir = "./" 改为extension_dir = "D:/www/php5/ext"
//实际的 php 目录
2.2 找到 ;extension=php_mysql.dll
将';'去掉改为
extension=php_mysql.dll
2.3 找到 ;session.save_path = "/tmp"
将';'去掉 设置你保存session的目录,如
session.save_path = " D:/www/php5/session_temp";

3 php.ini 的一些高级配置
3.1 php.ini 配置
register_globals = On
有些程序需要该项支持,否则无法上传文件

******
*附录*
******


*******
* F&Q *
*******

1. localhost 放在 httpd.conf 中定义的 DocumentRoot 指定的目录中的 jsp 文件怎么不能访问?
apache 默认 locahost 根目录是 httpd.conf 中定义的 DocumentRoot 指定的目录, tomcat 默认得 localhost 根目录是 tomcat_home/webapps/ROOT
apache 转发到 tomcat 的请求,都会到 ROOT 目录中,如
访问 http://localhost/index.html 会到 DocumentRoot 中寻找 index.html
访问 http://localhost/index.jsp apache 会把请求转发到 ROOT 中寻找

2. 如何在本地机器上面测试不同域名的虚拟主机?
在 windows 下,有个 hosts 文件,当浏览器进行域名解析的时候,首先到该文件中查找,如果找到记录,直接返回,找不到再到系统定义的域名服务上面去找。利用 windows 这个特性,在该文件中加入如下内容:

# All below are added by jh,only for test virtualhost
127.0.0.1 testa
127.0.0.1 testb

就可以进行虚拟主机测试了。

hosts 文件在 C:\WINDOWS\system32\drivers\etc 中,没有扩展名


********************
*主要参考文章和书目*
********************

http://boot.blogchina.com/835336.html
http://www.nihaoblog.com/15_11248.html
http://dev.csdn.net/develop/article/45/article/45/45858.shtm

Tomcat 与 Java Web 开发技术详解 孙卫琴 李洪成 电子工业出版社 2004



附加文件
********************************
mod_jk.conf

# The config document is at the url
# http://jakarta.apache.org/tomcat/connectors-doc/config/apache.html
#
# It is suitable for JK 1.2.4,uesed with workers.properties file write by H819.
# Rewrite the parameters as your application
#
# Author H819
# 2005.08.19

#JkMountFile is a new directive available from JK 1.2.9 version. It is used for dynamic updates of mount points at runtime. When the mount file is changed, JK will reload it's content.
# Load mount points
#JkMountFile conf/uriworkermap.properties

# Load mod_jk module
LoadModule    jk_module  modules/mod_jk.so

# JkWorkersFile specify the location where mod_jk will find the workers definitions.
JkWorkersFile conf/workers.properties

# JkLogFile specify the location where mod_jk is going to place its log file.
JkLogFile     logs/mod_jk.log

# Set the jk log level [debug/error/info]
#  info log will contains standard mod_jk activity (default).
#  error log will contains also error reports.
#  debug log will contains all informations on mod_jk activity
JkLogLevel    info

# JkLogStampFormat will configure the date/time format found on mod_jk logfile.
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"

# send all requests ending in .jsp to worker1
JkMount /*.jsp worker1
# send all requests ending servlet/* to worker1
JkMount /servlet/* worker1

#### examples
# Send servlet for context /examples to worker named worker1
#JkMount  /examples/servlet/* worker1
# send all requests jsp requests to files located in /otherworker will go worker2
# JkMount /otherworker/*.jsp worker2
# send all requests ending in .do to worker1
#JkMount /*.do worker1


*******************************
VirtualHost.conf

# VirtualHost config
#
# visit url http://httpd.apache.org/docs/2.0/vhosts/ for details.
#
# Rewrite the parameters as your application.
# DocumentRoot define: If your application files are located in DocumentRoot (httpd.conf defined),your can use relative path , otherwise you should use absolute path.
# "NameVirtualHost *:80" should not be commented,otherwise only one VirtualHost works.
#
# Author H819
# 2005.08.19

NameVirtualHost *:80
<VirtualHost *:80>
# visit http://testa
ServerAdmin webmaster@testa
DocumentRoot E:/tomcat-5.0.18/webapps/servlets-examples
ServerName testa
ErrorLog logs/testa.com-error_log
CustomLog logs/testa.com-access_log common

# visit http://testa/a/
Alias /a/ "E:/tomcat-5.0.18/webapps/jsp-examples/"
    <Directory "E:/tomcat-5.0.18/webapps/jsp-examples">
        Options Indexes FollowSymlinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>
  # visit http://testb
ServerAdmin webmaster@testb
DocumentRoot E:/ApacheGroup/Apache2/DocumentRoot
ServerName testb
ErrorLog logs/testb.com-error_log
CustomLog logs/testb.com-access_log common
</VirtualHost>


******************************
workers.properties

# The workers.properties file config document is at the url
# http://jakarta.apache.org/tomcat/connectors-doc/howto/workers.html
# http://jakarta.apache.org/tomcat/connectors-doc/config/workers.html
#
# It is suitable for JK 1.2.4
# Rewrite the parameters as your application
#
# Author H819
# 2005.08.19

# the list of workers
worker.list=worker1

# Worker connection pool maintain timeout in seconds.This feature has been added in jk 1.2.13
worker.maintain=60

# Set properties for worker1
# Defines a worker named "worker1" that uses the ajpv13 protocol to forward requests to a Tomcat process.
# Ajp13 is now the only out-process protocol supported by Tomcat 4.0.x, 4.1.x and 5.
worker.worker1.type=ajp13

# worker "worker1" will talk to Tomcat listening on machine localhost (it can be www2.x.com) at port 8009
# Notes: In the ajpv13 protocol, the default port is 8009
worker.worker1.host=localhost
worker.worker1.port=8009

#
# for all detail configurable items , please refer to http://jakarta.apache.org/tomcat/connectors-doc/config/workers.html
# This directive should be used when you have a firewall between your webserver and the Tomcat engine, who tend to drop inactive connections.
worker.worker1.socket_keepalive=False

# worker "worker1" use up to 10 sockets, which will stay no more than 10mn in cache
worker.worker1.cachesize=10
worker.worker1.cache_timeout=600

# worker "worker1" ask operating system to send KEEP-ALIVE signal on the connection
worker.worker1.socket_keepalive=1

# worker "worker1" want ajp13 connection to be dropped after 5mn (recycle)
worker.worker1.recycle_timeout=300

# load balancer .please refer to http://jakarta.apache.org/tomcat/connectors-doc/config/workers.html


***********************

testa.xml

<!--
# This file should be in Tomcat_home/conf/Catalina/testa/ path.
#
# visit http://testa
#
# Author H819
# 2005.08.19
-->
<Context path="/" docBase="E:/tomcat-5.0.18/webapps/servlets-examples"
        debug="0" privileged="true">
</Context>

***************************

a.xml

<!--
# This file should be in Tomcat_home/conf/Catalina/testa/ path.
#
# visit http://testa/a/
#
# Author H819
# 2005.08.19
-->
<Context path="/a" docBase="E:/tomcat-5.0.18/webapps/jsp-examples"
        debug="0" privileged="true">
</Context>

***************************

testb.xml

<!--
# This file should be in Tomcat_home/conf/Catalina/testb/ path.
#
# visit http://testb
#
# Author H819
# 2005.08.19
-->
<Context path="/" docBase="E:/ApacheGroup/Apache2/DocumentRoot"
        debug="0" privileged="true">
</Context>

*****************************
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics