search
HomeBackend DevelopmentPHP TutorialWindows 上 Nginx + PHP5 的安装与配置

Windows 下 Nginx + PHP5 的安装与配置

安装 PHP5

首先,从 http://windows.php.net/download/ 下载最新的 PHP 5.3 Windows 版本,这里 PHP 以 FastCGI 模式运行,所以请下载 None Thead Safe 版本。

解压至 C:\php5,把压缩包中的 php.ini-recommended,更名为 php.ini,然后打开修改几个选项:

1234567891011
<span style="color: #000099;">error_reporting</span> <span style="color: #000066; font-weight: bold;">=</span><span style="color: #660066;"> E_ALL</span><span style="color: #000099;">display_errors</span> <span style="color: #000066; font-weight: bold;">=</span><span style="color: #660066;"> On</span><span style="color: #000099;">extension_dir</span> <span style="color: #000066; font-weight: bold;">=</span> <span style="color: #993333;">"C:\php5\ext"</span>?<span style="color: #666666; font-style: italic;">; 动态扩展,可以根据需要去掉 extension 前面的注释 ; </span><span style="color: #666666; font-style: italic;">; 如加载 PDO, MySQL</span><span style="color: #000099;">extension</span><span style="color: #000066; font-weight: bold;">=</span><span style="color: #660066;">php_pdo.dll</span><span style="color: #000099;">extension</span><span style="color: #000066; font-weight: bold;">=</span><span style="color: #660066;">php_pdo_mysql.dll</span>?<span style="color: #666666; font-style: italic;">; CGI 设置</span>cgi.fix_pathinfo <span style="color: #000066; font-weight: bold;">=</span><span style="color: #660066;"> 1</span>

?

PHP 加载扩展需要注意依赖性,比如 php_exif.dll 需要 php_mbstring.dll,你必须要把 php_mbstring.dll 放在 php_exif.dll 前面才能加载成功。有些扩展依赖额外的 dll 文件,如 PHP 5.0+ ,php_mysqli.dll 依赖 libmysql.dll,而 php_oci8.dll,你则需要安装 Oracle 8 的客户端。如果你对这些依赖性不是太了解,可以参考一下安装包中的 install.txt 文件。

依赖文件的搜索顺序:首先是 php.exe 所在的目录,如果是 ISAPI 模式,那么会搜索 Web Server 的启动位置,比如 Apache 的 bin 目录;其次是 Windows PATH 环境变量中的目录。这里不要复制任何文件到 Windows 目录中,有必要的话,可以把 C:\php5 加到 PATH 中,便于以后 PHP 的升级。

安装 Nginx

从 v0.7.52 开始,Nginx 开始发布 Windows 版本的 Nginx,你可以在其官方网站上面下载:
http://nginx.net

如果需要老版本的 Nginx for Windows,可以在 Kevin Worthington 的网站上面找找。

我使用的是 0.8.29,下载好以后,解压释放文件到 C:\nginx。

那么如何配置 Nginx,使其可以和 PHP 协同工作?

配置 PHP FastCGI

Nginx 需要和 FastCGI Server 配合才能处理请求,有两种方式运行 PHP FastCGI Server,一种就是使用 PHP 内置的 FastCGI 管理器:

1
C:<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>php-cgi.exe <span style="color: #660033;">-b</span> 127.0.0.1:<span style="color: #000000;">9000</span> <span style="color: #660033;">-c</span> C:<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>php.ini

另外一种方式是使用第三方工具,比如 PHP-FPM 、cgi-fcgi 等。显然!要在 Windows 中使用这些工具是件极其痛苦的事情,你可能需要 Cygwin 之类的东西才行,的确有人这么做了,虽然我觉得那是自寻烦恼。

下一步,修改 Nginx ,将 php 请求转发至 PHP FastCGI Server:

123456
<span style="color: #adadad; font-style: italic;"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span>location ~ ^(.+\.php)(.*)$ {    root            D:/public_html;    fastcgi_param   SCRIPT_FILENAME     $document_root$fastcgi_script_name;      <span style="color: #00007f;">include</span>         php.conf;}

root 也就是 $document_root 指的是你的 php scripts 根目录,设置为你的网站根目录。在 Windows 下,需要注意的是 root 的路径,最好使用 "/" 作为路径分隔符,而不是 Windows 默认的 "\",否则容易出问题,比如,这个路径:D:\public_html\test,就不会起作用,Nginx 会抛出 500 错误,原因是 \test 中 \t 被解析为制表符。当然再加上一个反斜杠转义也是可以的,如:D:\\public_html\\test。

php.conf 配置文件:

123456789101112
<span style="color: #adadad; font-style: italic;"># 连接到本机 9000 端口,这里的端口是指 PHP FastCGI Server 开启的端口,</span><span style="color: #adadad; font-style: italic;"># 请与 php-cgi.exe 开启的端口保持一致</span><span style="color: #adadad; font-style: italic;"># 当 Nginx 收到 php 文件的请求时,会自动转发到 PHP FastCGI Server</span>fastcgi_pass    127.0.0.1:<span style="color: #ff0000;">9000</span>;fastcgi_index   index.php;?<span style="color: #adadad; font-style: italic;"># Nginx 默认是不支持 CGI PATH_INFO,SCRIPT_NAME 的值也不标准(糅合了 PATH_INFO)</span><span style="color: #adadad; font-style: italic;"># 下面的两行指令,可以从 SCRIPT_NAME 中剥离出 PATH_INFO</span>fastcgi_split_path_info     ^(.+\.php)(.*)$;fastcgi_param PATH_INFO     $fastcgi_path_info;?<span style="color: #00007f;">include</span>   fastcgi_params;

创建一个独立的 php.conf 保存配置,纯粹是为了精简 nginx.conf,个人习惯而已,也可以全部写在主配置文件中。

修改 php.ini,设置 cgi.fix_pathinfo = 1,这非常重要,PHP 会修正 SCRIPT_FILENAME 为真实的文件地址,否则 PHP 将无法找到需要处理的 php 文件。

一些其他的设置,主服务器:

1234567891011121314
<span style="color: #adadad; font-style: italic;"># 默认开启的进程数</span>worker_processes  <span style="color: #ff0000;">1</span>;?error_log  logs/error.log;<span style="color: #adadad; font-style: italic;">#error_log  logs/error.log  notice;</span><span style="color: #adadad; font-style: italic;">#error_log  logs/error.log  info;</span>?<span style="color: #adadad; font-style: italic;">#pid        logs/nginx.pid;</span>?events {<span style="color: #adadad; font-style: italic;"># 一个进程所处理的最大连接数上限,</span><span style="color: #adadad; font-style: italic;"># 本地开发,不需要默认的 1024,这里改为 64</span>    worker_connections  <span style="color: #ff0000;">64</span>;}

当某个目录下面不存在默认 index.php index.html 等首页文件时,Nginx 会抛出 403 ERROR,如果你需要罗列此目录,则可以在 http {… } 中加入如下命令:

123
autoindex <span style="color: #0000ff;">on</span>;autoindex_exact_size <span style="color: #0000ff;">on</span>;autoindex_localtime <span style="color: #0000ff;">on</span>;

OK,整合到一起

创建 start_nginx.bat,用于同时启动 PHP FastCGI 和 Nginx:

123456789101112
<span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> off<span style="color: #808080; font-style: italic;">REM Windows 下无效</span><span style="color: #808080; font-style: italic;">REM set PHP_FCGI_CHILDREN=5</span><span style="color: #808080; font-style: italic;">REM 每个进程处理的最大请求数,或设置为 Windows 环境变量</span><span style="color: #b1b100; font-weight: bold;">set</span> PHP_FCGI_MAX_REQUESTS=1000?<span style="color: #b1b100; font-weight: bold;">echo</span> Starting PHP FastCGI...RunHiddenConsole C:/php5/php-cgi.exe -b 127.0.0.1:9000 -c C:/php5/php.ini?<span style="color: #b1b100; font-weight: bold;">echo</span> Starting nginx...C:/nginx/nginx.exe -p C:/nginx

RunHiddenConsole.exe 是一个用来隐藏 DOS 窗口的小程序,可以在这里下载。
start_nginx.bat 开启后,也会有 DOS 窗口,但是可以安全的关掉,并不会关闭 Nginx 和 php-cgi.exe。

同样 stop_nginx.bat,用来关闭:

123456
<span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> off<span style="color: #b1b100; font-weight: bold;">echo</span> Stopping nginx...taskkill /F /IM nginx.exe <span style="color: #33cc33;">></span> <span style="color: #0000ff; font-weight: bold;">nul</span><span style="color: #b1b100; font-weight: bold;">echo</span> Stopping PHP FastCGI...taskkill /F /IM php-cgi.exe <span style="color: #33cc33;">></span> <span style="color: #0000ff; font-weight: bold;">nul</span><span style="color: #00b100; font-weight: bold;">exit</span>

到这里基本配置完毕了。

1 楼 vb2005xu 2012-08-09  
http://www.phpvim.net/web/php/build-php5-4-and-xdebug-on-win32.html

2 楼 vb2005xu 2012-08-14  
http://www.phpvim.net/web/php/script-for-php-buildin-fcgi-server.html

3 楼 vb2005xu 2012-08-15  
winbinder
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment