在Windows上快速配置Nginx+Php环境

(1)、 nginx安装
1,从nginx官方下载windows版本的nginx软件包
http://nginx.org/download/nginx-1.9.9.zip
2,解压到D:\nginx
3,通过windows service wrapper Windows服务安装工具将nginx服务注册到windows服务列表中去
3.1 ,下载windows service wrapper,解压到d:\nginx目录下,重命名为winsw.exe
3.2, D:\nginx\winsw.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8" ?>
<service>
<id>nginx</id>
<name>nginx</name>
<description>nginx</description>
<executable>D:\nginx\nginx.exe</executable>
<logpath>D:\nginx\</logpath>
<logmode>roll</logmode>
<depend></depend>
<startargument>-p D:\nginx</startargument>
<stopargument>-p D:\nginx -s stop</stopargument>
</service>

3.3, cmd切换到d:\nginx\winsw.exe install 安装nginx服务到windows服务列表中,我们就可以在windows服务中管理nginx服务了,我们将
nginx服务设置成 自动启动。

(2)、php整合到nginx中去,让nginx支持php解析
2.1, 编辑nginx.conf主配置文件使得nginx支持php
编辑d:\nginx\conf\nginx.conf
修改后的效果如下:

1
2
3
4
5
6
7
8
9
# 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  d:/nginx/html/$fastcgi_script_name;
           include        fastcgi_params;
        }

2.2, 启动php-cgi进程,用来解析php脚本
cmd下运行 C:\PHP>php-cgi.exe -b 127.0.0.1:9000 -c PHP\php.ini (命令行的窗口不能关闭(服务器注销或重启服务器),关闭后php-cgi.exe进程退出,导致php程序不能访问,静态的可以。)
(PHP的安装路径在C:\PHP)
2.3, 制作一个批处理脚本使得php-cgi.exe 在服务器重启的时候自动开启此进程
2.3.1 , 下载RunHiddenConsole.exe(RunHiddenConsole.exe的作用是在执行完命令行脚本后可以自动关闭命令行窗口,而从脚本中开启的进程不被关闭。)
2.3.2 , 制作一个reload.bat的批处理脚本,放在nginx的目录下面,脚本内容为:

1
2
3
4
5
6
7
8
@echo off
taskkill /F /IM nginx.exe
taskkill /F /IM php-cgi.exe
cd d:\nginx
RunHiddenConsole.exe nginx
RunHiddenConsole.exe c:/PHP/php-cgi.exe -b 127.0.0.1:9000 -c c:/PHP/php.ini
RunHiddenConsole.exe nginx.exe
pause

2.3.3, 将此批处理脚本,添加到windows的任务计划中, 在计算机启动时候自动运行此脚本。
Ps: 有了reload.bat脚本后, windows系统服务列表中的nginx服务可以禁用,亲自测试过。
在d:\nginx\html里面创建一个php脚本, index.php内容为:

1
2
3
<?php
phpinfo();
?>

经过测试(http://ip/index.php),php脚本能够正常解析。

(3)、nginx 站点创建
3.1, 创建一个phpmyadmin程序,http://ip:8080来访问phpmyadmin (phpmyadmin程序路径为d:\phpmyadmin)
3.2, 客户程序在d:\www , http://ip:80 来访问客户程序
以下是实现方法:
d:\nginx\conf\nginx.conf文件内容(根据上面的需求修改过的)
测试通过。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
   #                  '$status $body_bytes_sent "$http_referer" '
   #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
   keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   d:/phpmyadmin;
            index  index.html index.htm index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
       #
       error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
       #
       #location ~ \.php$ {
       #    proxy_pass   http://127.0.0.1;
       #}

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

        # deny access to .htaccess files, if Apache's document root
       # concurs with nginx's one
       #
       #location ~ /\.ht {
       #    deny  all;
       #}
   }
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   d:/www;
            index  index.html index.htm index.php;
        }
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
       #
       location ~ \.php$ {
           root           d:/www;
           fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  d:/www/$fastcgi_script_name;
           include        fastcgi_params;
        }
}
    # another virtual host using mix of IP-, name-, and port-based configuration
   #
   #server {
   #    listen       8000;
   #    listen       somename:8080;
   #    server_name  somename  alias  another.alias;

    #    location / {
   #        root   html;
   #        index  index.html index.htm;
   #    }
   #}


    # HTTPS server
   #
   #server {
   #    listen       443 ssl;
   #    server_name  localhost;

    #    ssl_certificate      cert.pem;
   #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
   #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
   #    ssl_prefer_server_ciphers  on;

    #    location / {
   #        root   html;
   #        index  index.html index.htm;
   #    }
   #}

}

相关新闻

联系我们

全国服务热线

400-033-9553

电子邮件:admin@example.com
工作时间:09:00-17:00 周一至周五

在线客服
关注微信
关注微信
分享本页
返回顶部