首页 > 技术文章 > windows下配置nginx+php环境

NaCl 2017-07-12 17:44 原文

以前学php的时候,都是用集成环境,实习的第一天就要用nginx搭建环境。
下面说说搭建过程。
首先我们先下载好nginx和php
nginx下载地址
然后到php官网上下载php,戳这里
下载后之后。然后就是配置的环节。
我们把他们都解压到E:/WWW这个文件夹下面
我们先来配置nginx,其实需要改动的东西并不多
我们找到server部分
找到root部分

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

然后添加index.php,如下面的代码所示

location / {  
            root   html; 
            index  index.html index.htm index.php;  
        autoindex on;  
        }  

然后找到

# 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;  
#}  

改成这样

location ~ \.php$ {  
            root   html;
            fastcgi_pass   127.0.0.1:9000;  
            fastcgi_index  index.php;  
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; //这句一定要改哇
            fastcgi_param PATH_INFO   $fastcgi_script_name;
            include        fastcgi_params;  
        }  

然后再来配置php
我们现在php文件下找到php.ini-development,将他重命名为php.ini
然后找到

extension_dir = "./ext"

然后修改为:

 extension_dir = "E:/WWW/PHP/ext"

接下来让nginx和php结合起来
我们找到

;cgi.fix_pathinfo=1

然后去掉前面的分号。
然后检验一下nginx是否支持php
我们在E:\WWW\nginx\html目录下简历一个index.php文件。

<?php
 echo "hello world";
 ?>

然后在浏览器输入http://127.0.0.1/index.php
如果出现hello world,说明环境已经配置好了。
附上完整nginx的配置文件,php太长就不复制了

#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  64;  
}  


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       80;  
        server_name  localhost;  

        charset gbk;  

        #access_log  logs/host.access.log  main;  

        location / {  
            root   html; 
            index  index.html index.htm index.php;  
        autoindex on;  
        }  

        #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           html;  
        #    fastcgi_pass   127.0.0.1:9000;  
        #    fastcgi_index  index.php;  
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
        #    include        fastcgi_params;  
        #}  
        location ~ \.php$ {  
            root   html;
            fastcgi_pass   127.0.0.1:9000;  
            fastcgi_index  index.php;  
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
            fastcgi_param PATH_INFO   $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;  
        #}  
    }  


    # 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;  
    #    server_name  localhost;  

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

    #    ssl_session_timeout  5m;  

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;  
    #    ssl_prefer_server_ciphers   on;  

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

}  

推荐阅读