Windows XP下PHP+MySQL环境搭建(4)

http://www.itjxue.com  2015-07-17 08:20  来源:未知  点击次数: 

 安装 PHP 解释器

下载 PHP 5

准备好 Apache 之后,就可以开始进行更有趣的工作了。在我看来,PHP 是可用的最好的 Web 编程工具之一。当然,还存在其他选择和大量的应用程序。如果您正在构建大型的服务器端应用程序,那么 Java 可能是最佳选择;如果您是一位真正的黑客,那么具有最大限度灵活性的 Perl 非常合适;JavaScript、AJAX 和 Ruby(尤其是 Ruby on Rails)非常适合高度交互性 Web 站点,例如 Google。但是,如果您只想快速实现一些基本功能,那么我认为 PHP 是最佳选择。它易于设置(您马上就会发现这一点)、能够和 Apache 很好地集成,而且一旦您熟悉了这个环境,就能够实现更复杂的功能。总之,它的确可以称得上是一种易于使用的、可访问的 Web 脚本语言。

当然,无论一种语言如何易于使用,都必须建立开发环境并使其运行。这就是 PHP 的优势。只需几个简短的页面,您就能够使脚本在 Apache 上运行。首先,访问 PHP Web 站点, www.php.net。 请确保不要访问 php.com,这是一个完全不同的网站。

您可以在以后浏览 PHP Web 站点并获得大量的信息(包括入门教程)。现在,您访问这个网站的目的是下载软件,因此请寻找位于页面顶部的下载链接。单击此链接,进入一个类似图 6 的页面。

图 6. 从官方 Web 站点下载 PHP 软件

<a href='http://www.itjxue.com/os/windows/'><u>Windows</u></a> XP下PHP+MySQL环境搭建_IT教学网itjxue.com转载 

寻找 Windows Binaries 标题,并找到 PHP 5.0.4 zip package 的链接;单击此链接,然后选择最近的镜像来下载安装程序(不要 下载 PHP 5.0.4 Installer,因为它不包括 MySQL 扩展)。将此文件保存到本地机器;因为它是临时文件,所以我将它放在桌面上。好了,现在可以安装它了。

Windows 上安装 PHP

获得 zip 文件之后,双击它来打开此 zip 文件。将此 zip 文件中的所有内容解压缩到 C:\PHP。

接下来做什么?哦,您需要简单地看一下进度条,然后安装就完成了。打开 C:\PHP ,您应该看到类似图 7 的内容。

图 7. 安装在 C:\PHP 的 PHP 解释器

<a href='http://www.itjxue.com/os/windows/'><u>Windows</u></a> XP下PHP+MySQL环境搭建_IT教学网itjxue.com转载 

下一步是创建一个可用的 INI 文件,此文件会告诉 PHP 在被其他应用程序(例如 Apache)调用时如何反应。创建 INI 文件的最佳方式是从模板复制一个文件,然后将其重命名为 php.ini。 在 C:\PHP 中查找名为 php.ini-recommended 的文件。复制此文件并将其粘贴到相同的目录中;然后将其重命名为 php.ini。这就是需要对它进行的所有操作。以后您将回过头来重新查看这个文件,但是现在,这就是开始运行所需的全部操作。

此过程不会 设置 PHP 使用 Apache。因此,让我们回过头来看一些其他的文本文件和手动配置。

将 PHP 连接到 Apache

启动文本编辑器,并打开 Apache 的 httpd.conf 文件,就像您在之前部分中做的那样。搜索 ScriptAlias,应该看到类似如下的内容:

#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache2/cgi-bin/"

对于 /cgi-bin/来说,此条目告诉 Apache 如何处理任何包含 cgi-bin 目录的 URL。例如,如果您请求了 http://www.newInstance.com/cgi-bin/mail-me,Apache 不会在 cgi-bin 目录中查找一个名为 mail-me 的文件。相反,ScriptAlias 命令会告诉 Apache 查看另一个目录 —— 在本例中,这个目录是 C:/Program Files/Apache Group/Apache2/cgi-bin/。这是至关重要的,因为您不会希望将脚本存储在可通过 Web 访问的目录中。因此,您需要为 PHP 脚本添加一个条目。如下所示:

#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache2/cgi-bin/"
ScriptAlias /php/ "C:/PHP/"

您可能认为 C:/PHP 不是存储 Web 站点脚本的好地方。这绝对是正确的;然而,您将告诉 Apache 通过 PHP 解释器处理所有以 .php 结尾的文件,而无需考虑这些文件的位置;换句话说,无需担心这个问题。

接下来,您需要告诉 Apache 以 .php 结尾的文件必须作为应用程序处理;尤其是,您希望为 PHP 文件分配一个类型,然后您就能够指示 Apache 以特定方式处理它们。在 ScriptAlias 条目下,添加以下条目:

#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache2/cgi-bin/"
ScriptAlias /php/ "C:/PHP/"
AddType application/x-httpd-php .php

现在您只需告诉 Apache 当它碰到这种新类型(链接到以 .php结尾的文件)时应该进行什么操作即可。基本上,这仅涉及告诉 Apache 运行什么程序,以及将 PHP 脚本交给什么程序;当然,这个神奇的程序就是您刚刚安装的 PHP 解释器。将此指令直接添加到刚才添加的两个指令下方:

#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache2/cgi-bin/"
ScriptAlias /php/ "C:/PHP/"
AddType application/x-httpd-php .php
Action application/x-httpd-php "/php/php-cgi.exe"

请记住,Apache 已经知道将 /php/ 翻译为 C:/PHP/,因此这实际上将 PHP 应用程序类型分配给解释器 C:/PHP/php-cgi.exe。当然,您可以通过浏览文件系统中的目录的方式验证这些文件是否存在。

完成上述操作之后,重启 Apache,并确保您没有犯错误。如果当您尝试输入 http://localhost 进行简单的测试时一切正常,那么您就成功了。

测试 PHP 安装

要进行的关于 PHP 方面的工作已经不多了。首先,确保 PHP 脚本运行正常。再次打开文本编辑器,并输入以下代码:

<html>
 <head><title>PHP Installation Test</title></head>
 <body>
  <?php phpinfo(); ?>
 </body>
</html>

将此文件作为 index.php 保存在您的 Web 站点的根目录中:C:/Documents and Settings/Brett McLaughlin/My Documents/websites。当完成此操作后,应该看到如图 8 所示的内容。

图 8. 编辑一个示例 PHP 脚本

<a href='http://www.itjxue.com/os/windows/'><u>Windows</u></a> XP下PHP+MySQL环境搭建_IT教学网itjxue.com转载 

现在您能够将此文件作为正常 URL 的一部分请求:http://localhost/index.php。打开您最喜欢的浏览器,查看得到的内容。它应该类似图 9。

图 9. phpinfo() 命令输出有用的调试信息

<a href='http://www.itjxue.com/os/windows/'><u>Windows</u></a> XP下PHP+MySQL环境搭建_IT教学网itjxue.com转载 

很明显,phpinfo() 命令为您提供了大量关于您的机器、PHP 解释器以及开发环境的有用信息。而且,此过程也证明了 PHP 安装正常。

允许 PHP 脚本作为 index 页面

您可能希望为 Apache 和 PHP 安装最后添加一点小内容。在某些情况下,您可通过诸如 http://localhost/my-script-name.php 之类的 URL 访问在上一部分看到的脚本。然而在其他情况下,您可能希望您的主页是 PHP 脚本。如果您在整个站点中还使用了其他脚本元素,且不希望用户必须输入一个特定于 PHP 的扩展名,那么此操作就非常有用。换句话说,您希望 .php 成为默认文件的有效扩展名,就像 .html 一样。例如,名为 index.php 的文件会被默认加载,就像 index.html 一样。

要进行此更改 —— 是的,您猜到了 —— 请再次打开 Apache 的 httpd.conf 文件。这次搜索 DirectoryIndex,您将看到类似如下的内容:

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
# The index.html.var file (a type-map) is used to deliver content-
# negotiated documents.  The MultiViews Option can be used for the
# same purpose, but it is much slower.
#
DirectoryIndex index.html index.html.var

将 index.php添加到此文件清单中:

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
# The index.html.var file (a type-map) is used to deliver content-
# negotiated documents.  The MultiViews Option can be used for the
# same purpose, but it is much slower.
#
DirectoryIndex index.html index.html.var index.php

现在重启 Apache,并输入 URL http://localhost/z(不指定 PHP 文件)。您看到的内容应该和您上次输入 My First Page 的 URL 时看到的内容完全相同。吃惊吗?您有一个名为 index.php 的文件,那么这是怎么回事呢?哦,问题是 Apache 以指定的顺序查找这些文件。在此例中,它找到了 index.html ,因此它在找到 index.php 之前就停止了查找。

您可以通过调整 httpd.conf 中的顺序的方式解决此问题,但是这会干扰正常的 Web 服务器行为。和此(过激的)步骤相反,您只需将您的 Web 站点根目录中的 index.html 重命名为 index.old 即可。然后返回并重载浏览器(或重新输入 http://localhost/)。现在 Apache 找不到 index.html,它就寻找 index.html.var (此文件不存在),最后寻找 index.php。它加载了此文件,您应该看到图 9 中的内容。

自我陶醉一下吧。您现在拥有了一个工作正常、功能完善、适当定制的 Apache 和 PHP 开发环境。

 

(责任编辑:IT教学网)

更多