 |
本节包括在 Microsoft Windows 系统中针对 Apache 2.0.x
安装 PHP 的指导与说明。在其它页面也有 Apache 1.3.x 用户指导与说明。
| 警告 | 不推荐将线程化 MPM
用于实际运作的 Apache 2 环境中去。用 prefork MPM 替代,或者用
Apache 1。其原因见 FAQ 中的使用线程化 MPM 的 Apache2。 |
强烈建议阅读
Apache 文档来加深对
Apache 2.0.x 服务器的基本理解。此外在继续下去之前考虑先阅读一下
Apache 2.0.x 的 Windows 下使用说明。
PHP 和 Apache 2.0.x 兼容性注意事项:
已知下列版本的 PHP 可以工作于最近版本的 Apache 2.0.x 之下:
以上版本的 PHP 与 Apache 2.0.40 或更高版本兼容。
Apache 2.0 SAPI 支持自 PHP 4.2.0
起。PHP 4.2.3 可工作于 2.0.39,不要用任何其它版本的 Apache
与 PHP 4.2.3 配合。不过推荐的设置是用 PHP 4.3.0
或更高版本与最新版的 Apache2 配合使用。
所有提及的 PHP 版本仍然能工作于 Apache 1.3.x 之下。
| 警告 |
Apache 2.0.x 被设计运行于 Windows NT 4.0,Windows 2000 或
Windows XP 之下。目前为止对 Windows 9x 的支持尚未完成,此时尚不指望
Apache 2.0.x 在此类平台下工作。
|
下载最新版本的
Apache 2.0.x 以及适合的 PHP 版本。先完成手工安装步骤后再回来继续将
PHP 集成入 Apache。
Windows 下有两种方法使 PHP 工作于 Apache 2.0.x 之中。一种是
使用 CGI 可执行程序,另一种是适用 Apache 模块的 DLL。不管哪种都需要编辑
httpd.conf 来配置 Apache 支持 PHP 并重新启动服务器。
注意: 记住在
Windows 下给 Apache 的配置文件中加入路径值的时候,所有的反斜线例如
c:\directory\file.ext 必须转换成正斜线,如
c:/directory/file.ext。
需要将以下三行加入到 Apache 的 httpd.conf 配置文件中以设定 CGI:
例 6-5. PHP 在 Apache 2.0 中的 CGI 方式 ScriptAlias /php/ "c:/php/"
AddType application/x-httpd-php .php
# 对 PHP 4 用这行
Action application/x-httpd-php "/php/php.exe"
# 对 PHP 5 用这行
Action application/x-httpd-php "/php/php-cgi.exe" |
|
| 警告 | 如果使用 CGI
方式安装,则服务器对于某些可能的攻击是开放的。请阅读
CGI
安全一章以学习如何防御这些攻击。 |
需要将以下两行加入到 Apache 的 httpd.conf 配置文件中以设定
Apache 2.0 的 PHP 模块:
例 6-6. PHP 在 Apache 2.0 中的模块方式 # 对 PHP 4 用这两行:
LoadModule php4_module "c:/php/php4apache2.dll"
# 别忘了从 sapi 目录中把 php4apache2.dll 拷贝出来!
AddType application/x-httpd-php .php
# 对 PHP 5 用这两行:
LoadModule php5_module "c:/php/php5apache2.dll"
AddType application/x-httpd-php .php
# 配置 php.ini 的路径
PHPIniDir "C:/php" |
|
注意:
记得用自己 PHP 实际所在的路径替换掉上例中的
c:/php/。要留意在 LoadModule 指令中用的是
php4apache2.dll 或
php5apache2.dll,而不是
php4apache.dll 或
php5apache.dll,后者是设计用于
Apache 1.3.x 的。
注意:
如果要使用内容协商机制,请阅读有关
FAQ。
| 警告 |
不要在安装中混合使用来自不同 PHP 版本的
DLL。使用下载回来的 PHP 版本中所提供的 DLL 和扩展库是唯一选择。
|
philippeboucher at newlogiccanada dot com dot com
31-Jul-2007 09:05
To do a silent install of PHP wich correctly modify the httpd.conf file, you HAVE (I find no other way) to put the directory of the Apache inside the INSTALLDIR's MSI option. the APACHEDIR doesn't seem to work. That's the only way I've make it work. Maybe it's because my Apache is not in the default directory, I don't know, but there is a solution for people who cannot make work the silent install (for those who are told by the php setup that it cannot find the httpd.conf file).
Hope it helps someone.
anonymousness
15-Jun-2007 02:30
Verrrrrrrry important note:
If you enable multi-byte strings for your installation of PHP, the extension loaded MUST be the first extension in the list.
Apache, NOR PHP will generate any error messages or warning information in the error log or in system logs about this.
This information needs to be included in the Readme or in the distributed INI files.
The recommended PHP ini's should place extension=php_mbstring.dll at the top of the list of extensions.
I had completely forgotten about this when recently upgrading php on a test server; and almost set out to re-install apache.
joelwells at streamlineIT dot org
12-May-2007 11:15
I had trouble with the PHPIniDir directive, then I realized that the order of the PHP entries is key.
PHPIniDir is a directive found in the load module for php (e.g., php5apache.dll). Therefore, the PHPIniDir directive must come after the LoadModule directive. Otherwise, Apache has no idea what PHPIniDir is, and your Apache service will not start.
zerocredibility@hot_mail
29-Apr-2007 05:01
PHP5 and apache2_2on a Win32 PC.
Kept getting blank screen (but could see it it with view source) with test script:
<?
phpinfo();
?>
The problem is that the above test script uses short open tags "<?" rather than long ones "<?php" The short ones are not enabled with php.ini-recommended version but are in the php.ini-dist version...
Either change php.ini:
short_open_tag = Off #to On to use the above script
or change the script to:
<?php
phpinfo();
?>
Easy when you know how.
This is after I checked and rechecked the httpd.conf file according to the FAQs...
martin dot hansell at gmail dot com
20-Mar-2007 08:17
As a complete novice with all_things_software, I found the instructions above incomplete, in particular with respect to WHERE to add in the additional lines to the PHP configuration file... they assume a certain amount of knowledge, which left me a bit in the dark. [See "at the foot" below for WHERE...]
In fact configuring the httpd.conf file is a piece of cake with the right instructions. This site below (courtesy of my brother-in-law - credit where it's due) gave me the answer, and I have pasted the key information below (permission not granted but credit again due via URL to respective site)
I quote "Next step is to open up your httpd.conf file in a text editor, wordpad or notepad, and add a few lines. The file, from the default installation is found in the X:\Program Files\Apache Group\Apache2\conf folder.
Do a search for "LoadModule" and you will see a list of modules that Apache tries to initiate when starting up. At the foot of this section, add the following 4 derivatives - each on a separate line: LoadModule; PHPIniDir; AddType; second AddType.
LoadModule php5_module "X:/Program Files/Apache Group/php/php-5.2.0-Win32/php5apache2.dll"
PHPIniDir "X:/Program Files/Apache Group/php/php-5.2.0-Win32"
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
That's it. Left-click the Apache monitor icon that should be in your task-bar and press restart. You can create a php page to test if the installation has worked. Save the following as info.php in your X:\Program Files\Apache Group\Apache2\htdocs folder assuming you haven't modified your document root." [For "the following" you will have to refer to the site...]
http://www.skiffie.com/code/server/php-as-apache-module
It really was as simple as that... Hope this helps.
Martin
charlie at oblivion dot cz
15-Feb-2007 12:19
..try to set the extension_dir in php.ini to the absolute path.. (when you are pretty sure that you have set the PHPIniDir the right way)
Jim Keller
11-Jan-2007 01:35
it's important to note that on Apache 2.2.3 (probably other 2.x versions as well) on Windows, the PHPIniDir directive must use forward slashes to delimit directories, not the backslashes commonly used for Windows path strings. If PHP seems to be ignoring the directive, this may be why.
gaizka at ilunabar dot net
07-Nov-2006 11:22
Apache 2.2.3 and PHP 5.2
# For PHP 5 do something like this:
LoadModule php5_module c:/php/php5apache2_2.dll
AddType application/x-httpd-php .php
# configure the path to php.ini
PHPIniDir C:/php
Remember to substitute your actual path to PHP for the c:/php/ in the above examples.
pcdinh at phpvietnam dot net
07-Oct-2006 10:50
PHP 6 is under active development but for those whose want to try out the new features or just want to keep updated with the development progress of PHP 6.0, you can follow my instructions below to install it on Windows XP and Apache 2.2.3
# For PHP 6 do something like this:
LoadModule php5_module "c:/server/php6/php6apache2_2.dll"
AddType application/x-httpd-php .php
PHPIniDir "c:/server/php6"
You should change the path as in your system. The directive LoadModule php5_module seems a bit strange to you. It should be LoadModule php6_module instead. However, I have just taken a look at php.internals and came across a message that said it should be changed in the near future after the PHP 5.2 is official released. At the time I write this, PHP 5.2 RC6 is planned to be released in the next few days.
k2wardog at yahoo dot com
06-Sep-2006 02:55
I was also having the 403 forbidden error. Removing the trailing backslash from the script alias as well as adding the following lines solved the issue:
<Directory "C:/php">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
Thanks for the help boss.
sgipyza1 at yahoo dot com
19-Aug-2006 06:58
Today I try to install PHP5.1.5 with Apache2.2.3 as a module but Apache always cannot start with Windows XP.
After I read the user contributed notes, I found the website Apache Lounge (http://www.apachelounge.com) and got the fixes: php5apache2.dll-php5.1.x.zip (http://www.apachelounge.com/download /mods/php5apache2.dll-php5.1.x.zip).
In the Readme First.txt, Steffen clearly documented the install steps. And it works! Thank you steffen.
subajawa at yahoo dot com
04-Aug-2006 08:56
To install PHP as Apache 2 CGI script, add these lines in addition to the 3 lines mentioned in the document.
<Directory "C:/php">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
04-Aug-2006 08:31
There is a mistake in documentation regarding installation of PHP as Apache 2 CGI script. The ScriptAlias directive should not have trailing slash on 'php' as it then means that 'php' should be part of the URL, just like 'cgi-bin' usually is, and in case of PHP this leads to 403 Forbidden.
Instead, this is the correct directive (no trailing slash on 'php'):
ScriptAlias /php "C:/path/to/PHP/dir"
m 1 b x d at htomail dot com
10-May-2006 01:22
Indeed the references to "apachelounge" are a lifesaver!
With regards to installation :
Apache 2.2.2 and php v5.1.2 / 5.1.3 / 5.1.4
http://www.apachelounge.com/
Excellent site for these installation issues.
MX
philip at php dot net
06-May-2006 06:57
If you use Apache 2.2.x, the included DLLs (php4apache2.dll and php5apache2.dll) will not work for you as they are specific to the Apache 2.0.x API. There is an open bug report to address this issue but if you can't wait then go to the following URL and download an appropriate DLL for Apache 2.2.x:
* http://apachelounge.com/
The PHP Group does not endorse this site but it appears useful, so use it :-)
lyh@edu
05-May-2006 03:13
I had a hard time to install PHP5.1 with Apache2.2 as a module, because whenever I tried to start apache, I always got the error that "Cannot load C:/php/php5apache2.dll into server: The specified module could not be found."
I have checked that php5apache2.dll does in the right directory of "c:/php/", and I have also tried to copy some files (php5ts.dll, php.ini) into relevant directories, such as WINDOWS/, Apache2/, but none of them worked.
I then found a post http://www.apachelounge.com/forum/
viewtopic.php?t=139&view=next&sid=b8df0fe80ac524939e2553ad7ee49123
and tried as suggested by downloading a zip file created by Steffen, and followed the instructions. The apache2.2 now works fine.
BTW, I am using a XP home and folllowed the instructions on the top part of this page.
chris -dot- chaudruc -at- gmail -dot- co
15-Dec-2005 03:02
This took a while for me to figure out but hopefully it will save some time for others.
Running Apache 2, PHP 5.1.1 on Windows XP and could not get mysql library to load. Extension path was correct in php.ini and the module resided in the correct spot.
Discovered that libmysql.dll in the root php directory needs to be moved to C:\WINDOWS or be included in Windows paths in order for this module to load.
From Zend:
"Some modules depend of other libraries, like MySQL. In this case you need to copy libmysql.dll from your MySQL/bin installation directory to a directory in your PATH, like C:\WINDOWS"
Copied over the file and mysql functionality was enabled.
12-Dec-2005 11:12
There are often strong suggestions that Apache/MySQL/PHP should be set up in the Windows root folder, and dire warnings against using folders with spaces in the name. But as a relatively non-technical user, I hate cluttering my Windows XP root directory with folders that should be under Program Files, and of keeping any documents other than in "My Documents" (even though I agree that MS's folder tree is ugly).
Frankly I've never had any difficulty with Apache and MySQL under Program Files, PHP5 deep in the Apache tree, and all documents under My Docs.. Here are the related Apache 2 config lines I use in case anyone is interested (sorry if there are broken lines):
ServerRoot "C:/Program Files/Apache Group/Apache2"
DocumentRoot "C:/Documents and settings/UserNm/My Documents/Websites"
# PHP 5 module
LoadModule php5_module "c:/Program Files/Apache Group/Apache2/php5/php5apache2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/Program Files/Apache Group/Apache2/php5/"
msonsuz at example dot com
14-Oct-2005 01:28
When you try apache using the bin directory use the command: apache -n "service_name" -t
Use for service_name the name u used. You can find the service_name also in the system tray
withheld at withheld dot com
06-Aug-2005 01:40
BTW I use Win9x to develop but it's not a production server. And yes, adding a trailing slash to the PHPIniDir directive helps.
Isaac dot Brown at ArvinMeritor dot com
17-May-2005 12:59
Some XP machines are having troubles with the PHPIniDir derective not finding the php.ini (or so they think). Directories that do contain a php.ini file are returned as empty and it defaults to the next method of finding php.ini (often C:/windows or C:/winnt).
This is likely caused by read permissions not being set correctly on NTFS file systems, however, it has occurred when no cause could be identified. If setting correct file permissions doesn't work, the easiest way around this problem is moving php.ini to the Apache directory or adding the HKEY_LOCAL_MACHINE\SOFTWARE\PHP\IniFilePath string to your registry and setting it to the correct directory.
wrowe at apache dot org
11-Apr-2005 12:26
Although the warning above reads "We do not recommend using a threaded MPM in production with Apache2" - ignore that nonsense for Win32.
All Apache/Win32 builds were *always* threaded. Always choose Apache 2 over Apache 1.3 on Windows - because major portions of Apache 2 were written from the ground up to accept Windows.
Apache 1.3 was never designed for threads or non-posix systems. For Windows they were squirled in with some rather ugly hacks, which will never enjoy much improvement.
|  |