您的位置:首页 > 运维架构 > Nginx

Windows下配置Nginx+PHP

2016-03-07 14:34 603 查看
声明:本文参考了网上资料,请见最后的"参考网址"

第一部分:准备工作

1.首先是下载软件

Nginx官网下载:http://nginx.org/
PHP官网下载:http://windows.php.net/download/

RunHiddenConsole(用来隐藏Nginx和PHP的小工具) 下载地址:http://redmine.lighttpd.net/attachments/660/RunHiddenConsole.zip 

2.建立D:\MyWebServer目录;建立D:\MyWebServer\WWW目录;建立D:\MyWebServer\Utils;

   将Nginx解压到D:\MyWebServer\Nginx;

   将PHP解压到D:\MyWebServer\PHP;

   将RunHiddenConsole.zip解压到D:\MyWebServer\Utils; 

3.下载并安装PHP版本对应的Visual C++ Redistributable

第二部分:安装Nginx
1.运行D:\MyWebServer\Nginx\nginx.exe

2.测试是否启动nginx:打开浏览器访问http://localhost 或 http://127.0.0.1,看看是否出现“Welcome to nginx!”,如果出现就证明启动成功了。没有启动的话,看看80端口有占用没。

    注意:此时该网站的默认目录在"D:\MyWebServer\Nginx\html"下

第三部分:安装PHP(这里主要讲Nginx配置启动PHP,以CGI运行PHP)

    对于D:\MyWebServer\Nginx\conf\nginx.conf配置文件

1.修改第36行

server {

      listen       80;

              server_name  localhost;

              ...

    }

    修改成 listen   8080;

     即将端口改成8080

2.修改大概第43~45行之间的

        location / {

            root   html;
            index  index.html index.htm;

}

修改网站文件的路径,以及添加index.php的默认页:
        location / {

            root   D:/MyWebServer/WWW;

            index  index.html index.htm index.php;

        }

3.支持PHP的设置,修改大概在第63-71行的

        # 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  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}

先将前面的“#”去掉,同样将root  html;改为root  D:\MyWebServer\WWW;。再把“/scripts”改为“$document_root”,这里的“$document_root”就是指前面“root”所指的站点路径,这是改完后的:

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        location ~ \.php$ {

            root           D:/MyWebServer/WWW;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

        }

4.复制D:\MyWebServer\PHP\ext\php.ini-development成D:\MyWebServer\PHP\ext\php.ini;

修改以下配置:

搜索“extension_dir”,找到: ;extension_dir = "ext" 去掉前面的分号

搜索“date.timezone”,找到:;date.timezone = 先去前面的分号再改为 date.timezone = Asia/Shanghai

搜索“enable_dl”,找到:enable_dl = Off 改为 enable_dl = On

搜索“cgi.force_redirect” ;cgi.force_redirect = 1 先去前面的分号再改为 cgi.force_redirect = 0

搜索“fastcgi.impersonate”,找到: ;fastcgi.impersonate = 1 去掉前面的分号

搜索“cgi.rfc2616_headers”,找到:;cgi.rfc2616_headers = 0 先去前面的分号再改为 cgi.rfc2616_headers = 1

//搜索“php_mysql”,找到:”extension=php_mysql.dll和extension=php_mysqli.dll  去掉前面的分号

其他的配置请按照自己的需求更改。

第四部分:编辑运行配置文件
1.在D:\MyWebServer\WWW下新建一个index.php,內容如下:

<?php

    phpinfo();

?>

2.在D:\MyWebServer\Utils下建立Start.bat,内容如下:

@echo off

echo Starting PHP FastCGI...

RunHiddenConsole.exe  ..\PHP\php-cgi.exe -b 127.0.0.1:9000 -c ../PHP/php.ini

echo Starting Nginx...

RunHiddenConsole.exe  ..\Nginx\nginx.exe  -p  ..\Nginx

3.在D:\MyWebServer\Utils下建立Stop.bat,内容如下:

@echo off

echo Stopping Nginx...

taskkill /F /IM nginx.exe > nul

echo Stopping PHP FastCGI...

taskkill /F /IM php-cgi.exe > nul

4.访问http://localhost:8080/index.php,应显示PHP Version页面

参考网址:
http://www.cnblogs.com/wuzhenbo/p/3493518.html http://jingyan.baidu.com/article/a948d6517c63e60a2dcd2e39.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Windows Nginx PHP SQL Server