Windows 下 Nginx 配置 PHP 环境

PHP

下载 PHP

PHP 下载地址,下载 x64 Thread Safe 的 Zip 文件。然后随便解压到一个目录。

解压后在文件夹中找到php.ini-development文件复制一份并改名为php.ini.

给PHP指定可加载扩展模块的位置

php.ini中找到extension_dir项目,取消注释并赋值为./ext
修改前:

; Directory in which the loadable extensions (modules) reside.
; https://php.net/extension-dir
;extension_dir = "./"
; On windows:
;extension_dir = "ext"

修改后:

; Directory in which the loadable extensions (modules) reside.
; https://php.net/extension-dir
;extension_dir = "./"
; On windows:
extension_dir = "./ext"

配置 PATH_INFO

php.ini中找到cgi.fix_pathinfo项目,取消注释:

修改前:

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; https://php.net/cgi.fix-pathinfo
;cgi.fix_pathinfo=1

修改后:

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; https://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=1

Nginx

下载 Nginx

Nginx 下载地址,下载nginx/windows-x.xx.x版本,下载完解压到目录。

配置 PHP

打开 nginx 目录中的confnginx.conf文件:

server配置 FastCGI:
原本的配置文件这一段直接就有,是注释着的,打开注释,然后修改fastcgi_param那一行就行了。

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

配置开机自启

在 nginx 目录创建一个 txt 文件,粘贴一下内容:

@echo off
start /b "" "nginx.exe"
exit

然后重命名为start-nginx.bat,并创建一个快捷方式。

在 php 目录创建一个 txt 文件,粘贴一下内容:

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "php-cgi.exe -b 127.0.0.1:9000 -c php.ini", 0, False
Set objShell = Nothing

然后重命名为start-php-cgi.vbs,并创建一个快捷方式。

快捷键Win + r并输入shell:startup回车打开自启目录,将两个快捷方式粘贴进去即可开机自启。

标签: Nginx, 开发环境, PHP

添加新评论