PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Microsoft Windows 下的 Sun,iPlanet 和 Netscape 服务器> <Microsoft Windows 下的 Apache 1.3.x
Last updated: Sun, 25 Nov 2007

view this page in

Microsoft Windows 下的 Apache 2.0.x

本节包括在 Microsoft Windows 系统中针对 Apache 2.0.x 安装 PHP 的指导与说明。在其它页面也有 Apache 1.3.x 用户指导与说明

Note: 应该先阅读手工安装步骤

Warning

不推荐将线程化 MPM 用于实际运作的 Apache 2 环境中去。用 prefork MPM 替代,或者用 Apache 1。其原因见 FAQ 中的使用线程化 MPM 的 Apache2

强烈建议阅读 » Apache 文档来加深对 Apache 2.0.x 服务器的基本理解。此外在继续下去之前考虑先阅读一下 Apache 2.0.x 的 » Windows 下使用说明

Note: 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 之下。

Warning

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 并重新启动服务器。

Note: 记住在 Windows 下给 Apache 的配置文件中加入路径值的时候,所有的反斜线例如 c:\directory\file.ext 必须转换成正斜线,如 c:/directory/file.ext

以 CGI 方式安装

需要将以下三行加入到 Apache 的 httpd.conf 配置文件中以设定 CGI:

Example#1 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"

Warning

如果使用 CGI 方式安装,则服务器对于某些可能的攻击是开放的。请阅读 CGI 安全一章以学习如何防御这些攻击。

以 Apache 模块方式安装

需要将以下两行加入到 Apache 的 httpd.conf 配置文件中以设定 Apache 2.0 的 PHP 模块:

Example#2 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"

Note: 记得用自己 PHP 实际所在的路径替换掉上例中的 c:/php/。要留意在 LoadModule 指令中用的是 php4apache2.dllphp5apache2.dll,而不是 php4apache.dllphp5apache.dll,后者是设计用于 Apache 1.3.x 的。

Note: 如果要使用内容协商机制,请阅读有关 FAQ

Warning

不要在安装中混合使用来自不同 PHP 版本的 DLL。使用下载回来的 PHP 版本中所提供的 DLL 和扩展库是唯一选择。



add a note add a note User Contributed Notes
Microsoft Windows 下的 Apache 2.0.x
nicolas dot grasset at gmail dot com
07-Jul-2009 05:08
Here is how I created a silent install for Apache2.2 and PHP5.2.10 on Windows XP (running on a MacBook Pro):

Download Apache2 and PHP5 installer files in a directory and update the msi file names in the following commands.

To have PHP installer find Apache2, do not forget APACHEDIR!

msiexec /i apache_2.2.11-win32-x86-no_ssl.msi /passive ALLUSERS=1 SERVERADMIN=admin@localhost SERVERNAME=localhost SERVERDOMAIN=localhost SERVERPORT=80 INSTALLDIR=c:\apache
msiexec /i php-5.2.10-win32-installer.msi /qn APACHEDIR=c:\apache INSTALLDIR=c:\php ADDLOCAL=ext_php_mssql,apache22
net stop "Apache2.2"
net start "Apache2.2"
m008 at springtimesoftware dot com
12-Jun-2009 10:14
Since Apache and PHP are often used together, and since it is difficult to configure the various versions, and since neither the official Apache or PHP websites or manuals contain clear, complete, and correct configuration instructions, and since hundreds of hours of wasted time has been spent, and since several of the user notes here contain conflicting advice, I hereby respectfully ask both the Apache and PHP development groups to study this issue and develop and release a simple automated tool (either to do the configuring OR to display instructions) that works for all valid version combinations.

David Spector
Springtime Software
CP
16-Apr-2009 06:27
This is old news to some. But people with Apache 2.0.55 / PHP 5.1.2 might like a reminder before they pull their hair out! (Some of us have ISPs using older version and need to test on a like version before we upload.)

If using PHP 5.1.2, to run Apache as a module, do *not* use Stephan's "php5apache2.dll" from http://www.ApacheLounge.com. That is for pre-5.1.2!

**There is a "php5apache2.dll" included with Windows PHP 5.1.2 zip file by default.**

If you forget this and overwrite the included DLL with Stephan's (as I did) you'll spend hours attempting to escape "DLL hell", with no success.
bsquidwrd at gmail dot com
04-Apr-2009 03:15
henke at henke37 dot cjb dot net
your way worked thanks a lot!
Anonymous
26-Feb-2009 02:32
i followed henke37's way to for the httpd.conf

I added all this at the very end of httpd.conf

# For PHP 5
#load the php main library to avoid dll hell
Loadfile "C:\php-5.2.8-Win32\php5ts.dll"

#load the sapi so that apache can use php
LoadModule php5_module "C:\php-5.2.8-Win32\php5apache2_2.dll"

#set the php.ini location so that you don't have to waste time guessing where it is
PHPIniDir "C:\php-5.2.8-Win32"

#Hook the php file extensions
AddHandler application/x-httpd-php .php
AddHandler application/x-httpd-php-source .phps

Also i didn't use short open tags as they are disabled in
"php.ini-recommended" if you don't change anything
So use this to test
<?php
 phpinfo
();
?>
NOT
<? phpinfo(); ?> short open tags

added my php directory to the PATH system variable and i start apache manually not as a service

It works for me hope it helps you!
acpadhi at yahoo dot co dot uk
29-Nov-2008 10:53
Attn all users :
Apache 2.2.9 and PHP 5.2.6 on Windows Vista 64. (installed through XAMPP).

After installing Visual Studio 2008 Express edition with SQL server express, I faced the following issue.

With the above combination, the Apache process used to die quite frequently with Windows reporting an error with Apache. Turned out that chaning a few setings in PHP.ini solved to some extent, but it would still happen intermitently when there is a period of inactivity for a few seconds.

After 2 days of struggle, found out that for some reason, PHP loaded as a module in Apache, was causing the problem.

Changed the settigns to use the php-cgi.exe version instead and the problems were gone. Not sure, if others have faced the same issue. Someone from XAMPP community also pointed out the same issue, with a resolution of having to stop the SQL server related processes stopped, however, in my case, the only way how it could work out fine was by loading the CGI Module.

Thought should post, this could help others.

Best, 
Anand Padhi
webssfz at gmail dot com
30-Jun-2008 10:52
@eric dot zhang at rogers dot com:
The line is not necessary since the installer adds these lines to mime.types:
application/x-httpd-php    php
application/x-httpd-php-source    phps
eric dot zhang at rogers dot com
12-Jun-2008 01:44
for me, I have xp, apache 2.2 and php 5.2.6,
to install, just use the installer and choose your modules during installation, and every thing works fine.
After install, I check the httpd.conf and found nothing about "AddType ..." so I assume you don't need it
cormac at kernan dot eu
21-Apr-2008 01:08
All good advice from henke, except where it says "Do not edit the system path".  You will NOT be able to load extensions that have dependencies e.g. MySQL, without adding PHP to your system path.  This threw me for some time as other extensions without dependencies will work without this requirement.
henke at henke37 dot cjb dot net
22-Feb-2008 11:22
Please completely ignore the page contents, it's stale garbage and will just lead to trouble.

Here is how to do it the right way:
Do not copy any php files anywhere
Do not edit the system path
And absolutely do not use the provided cgi directives.

Here is the proper apache directives to load php, preferbly saved as php.conf and included from your main config.

#load the php main library to avoid dll hell
Loadfile "C:\pathtophp\php5ts.dll"

#load the sapi so that apache can use php
LoadModule php5_module "C:\pathtophp\php5apache2_2.dll"

#set the php.ini location so that you don't have to waste time guessing where it is
PHPIniDir "C:\pathtophp"

#Hook the php file extensions, notice that Addtype is NOT USED, since that's just stupid
AddHandler application/x-httpd-php .php
AddHandler application/x-httpd-php-source .phps
cfoesterle at yahoo dot com
10-Feb-2008 04:41
I do not have IIS installed on my server, only Apache. So I had to edit the PHP.ini file and comment out (add leading ";") to the following line "extension=php_iisfunc.dll" as I was getting "Faulting application httpd.exe, faulting module php_iisfunc.dll" errors when this line was active. I also added an additional  "AddType application/x-httpd-php .html" to my httpd.conf file so that inline php would work with files ending with an extension of ".html" on my server.
mjm at alum dot mit dot edu
27-Jan-2008 04:33
If you use the PHP 5 installer, you'll notice that it uses the wrong type of slash on Windows!

Change C:/Program Files/PHP/" to C:\Program Files\PHP\" and everything works great!
rene at metalbunny dot net
27-Nov-2007 10:40
Installed Apache 2.2.6 on WinXP Pro SP2 with PHP 5.2.5 as server module (php5apache2_2.dll)...

Thanks to melanie_c_graham's post, I solved my trouble by commenting out the
PHPIniDir "C:/Program Files/PHP/"
in httpd.conf

With this line present, Apache will generate two "Apache has experienced a problem and needs to close" when attempting to shut down the httpd service.

Starting it up is just smooth, but on shutdown it fails - and with the line present, PHP isn't processed; just produces a blank page (not even an error message).
mhfaust at gmail dot com
23-Nov-2007 03:40
My context -- configuring:
- Windows XP/SP2
- Apache 2.2.6
- PHP 5.2.4 (as Apache module)

I had a problem with PHPIniDir (not loading), when setting it this way:
   PHPIniDir "C:\PHP\"

....but it worked when I changed it to:
   PHPIniDir "C:\PHP"

(deleted trailing slash)

-----------
....This seems relevant to the comment from:
Isaac dot Brown at ArvinMeritor dot com
17-May-2005 06:59

...and contradicts the line in the instructions at the top of this page "A trailing slash may also be necessary for directories."
packard_bell_nec at hotmail dot com
03-Oct-2007 03:24
If you install PHP as an Apache CGI binary, you can add:
AddHandler cgi-script .php
into Apache httpd.conf, and add shebang line to every PHP scripts like:
#!php
<?php
phpinfo
();
?>
. But adding shebang line has a disadvantage that if you decided to install PHP as an Apache module afterwards, then the shebang line WILL appear in the web page.
In fact, you do NOT need to add shebang line to every PHP script even if you install PHP as an Apache CGI binary, because you can add:
ScriptInterpreterSource Registry-Strict
into Apache httpd.conf, and make the registry file and merge it like:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.php\Shell\ExecCGI\Command]
@="\"C:\\Program Files\\PHP\\php-cgi.exe\""
. Then you will NOT need to change the PHP scripts which do not contain shebang line.
simon dot schmitt at med dot uni-heidelberg dot de
28-Sep-2007 02:43
Zend and Microsoft today announced FastCGI for IIS6 and higher: I tried it and it works fine.
Heres the link to the documentation: http://www.iis.net/php
newbe
17-Sep-2007 02:24
I very thank Richard about the following because my appache2.2.4 could't load php5apache2.dll all the time. Then, I found the right answer as below! Good luck!

richard dot blockley at googlemail dot com
21-Aug-2007 08:34
At time of writing this, Apache 2.2 is out. For PHP5 with Apache 2.2, I believe one has to use the module file php5apache2_2.dll. As opposed to php5apache2.dll
melanie_c_graham at hotmail dot com
01-Sep-2007 10:40
For some reason, installing Apache and PHP, I could not use this line item:

PHPIniDir "C:/php"

If I used it, my web server would not restart.  I commented the line out, and voila - PHP is installed perfectly (after an hour or two of swearing at it) and Apache is happy again.

I am going to operate under the assumption that since my system is working just fine without this ini redirection that ini redirection is not required for my configuration.

I am running Windows XP Home, Apache 2.2.4, PHP 5.2.4
richard dot blockley at googlemail dot com
21-Aug-2007 10:34
At time of writing this, Apache 2.2 is out. For PHP5 with Apache 2.2, I believe one has to use the module file php5apache2_2.dll. As opposed to php5apache2.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.
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.
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.
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"
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/"
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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites