PHP 8.4.1 Released!

在Windows系统上安装 Apache 2.x

本节包括在 Microsoft Windows 系统中针对 Apache 2.x 安装 PHP 的指导与说明。

注意:

请先阅读手工安装步骤

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

下载最新版本的 » Apache 2.x 以及适合的 PHP 版本。先完成手工安装步骤后再回来继续将 PHP 集成入 Apache。

Windows 下有三种方法使 PHP 工作于 Apache 2.x 之中。可以以 handler、CGI、或者 FastCGI 方式运行 PHP。

注意: 记住当在 Windows 环境下的 Apache 配置文件中添加路径值时,所有的反斜线,如 c:\directory\file.ext,应转换为正斜线: c:/directory/file.ext。对目录来说,也必须由斜线结尾。

以 Apache handler 方式安装

注意: 在使用 apache2handler SAPI 时,必须使用线程安全 (TS) 版本的 PHP。

需要将以下几行加入到 Apache 的 httpd.conf 配置文件中以加载 Apache 2.x 的 PHP 模块:

示例 #1 PHP 在 Apache 2.x 中作为 handler

# 在 PHP 8.0.0 之前,模块的名称是 php7_module
LoadModule php_module "c:/php/php8apache2_4.dll"
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>
# 配置 php.ini 的路径
PHPIniDir "C:/php"

注意: 必须使用实际的 PHP 路径替换掉上例中的 c:/php/。 确保在 LoadModule 指令中引用的文件位于所指定的位置。 PHP 7 使用 php7apache2_4.dll, PHP 8 使用 php8apache2_4.dll

以 CGI 方式运行 PHP

要更好地理解在 Apache 下运行 CGI,请参阅 » Apache CGI 文档

要将 PHP 以 CGI 方式运行,需要将 php-cgi 文件放入到用 ScriptAlias 指令所指定的 CGI 目录中。

然后需要给 PHP 文件中添加 #! 的一行来指明 PHP 可执行文件的位置:

示例 #2 Apache 2.x 下 CGI 方式的 PHP

#!C:/php/php.exe
<?php
  phpinfo();
?>

警告

服务器使用 CGI 方式进行部署可能存在几个公开的缺陷。请阅读 CGI 安全一章 以学习 如何抵御这些攻击。

以 FastCGI 方式运行 PHP

以 FastCGI 方式运行 PHP 比起 CGI 方式有很多优点。设定的方式很直接:

» https://www.apachelounge.com 取得 mod_fcgid,该站点有 Win32 可执行文件的下载。按照下载文件中的指示安装此模块。

按以下方法配置 web 服务器,注意用自己系统上的路径替换其中相应的内容:

示例 #3 配置 Apache 以 FastCGI 方式运行 PHP

LoadModule fcgid_module modules/mod_fcgid.so  

# php.ini 文件的位置
FcgidInitialEnv PHPRC        "c:/php" 

<FilesMatch \.php$>
    SetHandler fcgid-script
</FilesMatch>
FcgidWrapper "c:/php/php-cgi.exe" .php  
此时具有 .php 后缀的文件将被 PHP FastCGI 所解析执行。

添加备注

用户贡献的备注 5 notes

up
42
wolfeh1994 at yahoo dot com
10 years ago
Please for the love of god, download the threaded version. I spent over an hour trying to figure out why php5apache2.dll could not be found, and while desperately looking through manuals I went into the php 5 structure and found that it doesn't exist in the non-threaded version.

This really could use a mention somewhere other than the PHP 5 structure, like the paragraph to the left of the homepage which talks about which PHP version to choose, or this part of the manual which covers Apache... Anywhere but structure, seriously. I would have never guessed to look there.
up
8
a solution for simpletons like me
12 years ago
Installing Apache and PHP on Windows 7 Home Premium on a Gateway NV75S laptop with a quad AMD A6-3400M

All I need to do with these programs is to test my website out on my laptop. I have HTML and PHP files. I do not need MySQL as I use html5 storage.

Getting and installing Apache

1 In your browser go to h t t p : / / h t t p d . a p a c h e . o r g / d o w n l o a d . c g i
(without the spaces)
2 Click on httpd-2.2.22-win32-x86-no_ssl.msi
(this is a self-installing executable file without crypto ... no Secure Socket Layer)
(2.2.22 was the latest version on April 25, 2012)
3 Click on the httpd-2.2.22-win32-x86-no_ssl.msi file after it downloads
(single click on the file tab in Chrome or double click on the actual file in Downloads)
4 Click Next
5 Click I accept the terms in the license agreement
6 Click Next
7 Click Next
8 Type localhost in the top box
9 Type localhost in the middle box
10 Type admin@localhost.com in the bottom box
11 Click Next
12 Click Next
13 Click Next
14 Click Install and wait
15 Cick Yes to allow the program to make changes
16 Click Finish

Testing Apache

1 Type localhost in your browser location box (I use Chrome) or type h t t p : / / l o c a l h o s t
(without the spaces)
2 The message It works! should appear.

Getting and installing PHP

1 In your browser go to h t t p : / / w i n d o w s . p h p . n e t / d o w n l o a d /
(without the spaces)
2 Click on the Installer link under PHP 5.3 (5.3.10) VC9 x86 Thread Safe
(Ignore the Do NOT use VC9 version with apache.org binaries comment on the side panel)
3 Click on the php-5.3.10-Win32-VC9-x86.msi file after in downloads
(single click on the file tab in Chrome or double click on the actual file in Downloads)
4 Click Next
5 Click I accept the terms in the License Agreement
6 Click Next
7 Click Next
8 Click Apache 2.2.x Module
9 Click Next
10 Click Browse
11 Double click Apache Software Foundation
12 Double click Apache 2.2
13 Double click conf
14 Click OK
15 Click Next
16 Click Next
17 Click Install and wait
18 Cick Yes to allow the program to make changes
19 Click Finish

Testing PHP with Apache

1 Open Notepad
2 Type 'left bracket character'?php phpinfo(); ?'right bracket character'
3 Save the file to C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs as test.php
4 Type localhost/test.php in your browser location box (I use Chrome) or type h t t p : / / l o c a l h o s t / t e s t . p h p
5 A table with title PHP Version ... should appear

DONE
up
3
farinspace
14 years ago
Running PHP under FastCGI:

Besides the following in your httpd.conf

LoadModule fcgid_module modules/mod_fcgid.so
FcgidInitialEnv PHPRC "c:/php"
AddHandler fcgid-script .php
FcgidWrapper "c:/php/php-cgi.exe" .php

Remember to add the following to the default <Directory "C:/apache/htdocs"> block (or virtual host blocks):

Options ExecCGI
up
0
horica78 at yahoo dot com
5 months ago
This for the fastest architecture of Apache on Windows faster than as module for php

Enable mod_proxy and mod_proxy_fcgi in httpd.conf and run
setx PHP_FCGI_CHILDREN "15" /m
setx PHP_FCGI_MAX_REQUESTS "1000" /m
and download
RunHiddenConsole.exe and start php with c:\hidden\RunHiddenConsole.exe C:\PHP\php-cgi.exe -b 127.0.0.1:9000 and set on VirtualHost :
<Files ~ "\.(php|phtml)$">
SetHandler "proxy:fcgi://127.0.0.1:9000#"
ProxyFCGIBackendType GENERIC
ProxyFCGISetEnvIf "true" SCRIPT_FILENAME "C:%{reqenv:SCRIPT_FILENAME}"
</Files>
retar the Apache service and have fun. Cheers from one huge fan of
up
0
a user
8 years ago
If you are having issues getting the PHPIniDir or LoadModule directives to work and all the suggestions already given do not help, double-check if you are not using fancy quotes around your paths (‘ ’ “ ”).

This happened to me because I copied the statements from a random website. In my text editor the difference was barely noticeable, but to Apache it certainly is!

For example, this will not work:
PHPIniDir “C:/PHP7”

But this will work:
PHPIniDir "C:/PHP7"
To Top