windows nginx php 环境搭建

参考: http://www.cnblogs.com/huayangmeng/archive/2011/06/15/2081337.html

windows下配置nginx+php环境

  刚看到nginx这个词,我很好奇它的读法(engine x),我的直译是“引擎x”,一般引“擎代”表了性能,而“x”大多出现是表示“xtras(额外的效果)”,那么整个词的意思就是类似“极致效果”,“额外性能”。当然这里不是要来唠嗑,以上是题外话。

  nginx相较于我们熟悉的apache、IIS的优势,就我浅入浅出的了解,在于“反向代理”和“负载均衡”。因此考虑到能够为Web服务器节省资源,它可以代替apache来提供Web服务。那么上正题了,nginx有这么多优势,那在windows下如何来配置nginx+php环境?网上看到还是那么多转载来转载去的文章。这里就我配置的过程,来介绍一下:

1、首先需要准备的应用程序包。

  nginx:nginx/Windows-1.0.4

  php:php-5.2.16-nts-Win32-VC6-x86.zip (nginx下php是以FastCGI的方式运行,所以我们下载非线程安全也就是nts的php包)

  (还会用到)RunHiddenConsole:RunHiddenConsole.zip

2、安装与配置。

 1)php的安装与配置。

  直接解压下载好的php包,到D盘wnmp目录(D:\wnmp),这里把解压出来的文件夹重命名成php5。进入文件夹修改php.ini-recommended文件为php.ini,并用Editplus或者Notepad++打开来。找到

    
     
     
extension_dir = " ./ext "

更改为

    
     
     
extension_dir = " D:/wnmp/php5/ext "
往下看,再找到
    
     
     
;extension = php_mysql.dll ;extension = php_mysqli.dll

前面指定了php的ext路径后,只要把需要的扩展包前面所对应的“;”去掉,就可以了。这里打开php_mysql.dll和php_mysqli.dll,让php支持mysql。当然不要忘掉很重要的一步就是,把php5目录下的libmysql.dll文件复制到C:\Windows目录下,也可以在系统变量里面指定路径,当然这里我选择了更为方便的方法^_^。

到这里,php已经可以支持mysql了。

  接下来我们来配置php,让php能够与nginx结合。找到

    
     
     
;cgi.fix_pathinfo = 1

我们去掉这里的封号。

    
     
     
cgi.fix_pathinfo = 1
这一步 非常重要,这里是php的CGI的设置。

 2)nginx的安装与配置。

  把下载好的nginx-1.0.4的包同样解压到D盘的wnmp目录下,并重命名为nginx。接下来,我们来配置nginx,让它能够和php协同工作。进入nginx的conf目录,打开nginx的配置文件nginx.conf,找到

    
     
     
location / { root html;      #这里是站点的根目录 index index.html index.htm; }

root  html;改为root   D:/wnmp/www;

再往下,找到

复制代码
    
     
     
# 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:/wnmp/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: /wnmp/www; fastcgi_pass 127.0.0.1:9000 ; fastcgi_index index . php ; fastcgi_param SCRIPT_FILENAME $document_root $ fastcgi_script_name ; include fastcgi_params ; }
复制代码

保存配置文件,就可以了。

  nginx+php的环境就初步配置好了,来跑跑看。我们可以输入命令 

来启动php,并手动启动nginx,当然也可以利用脚本来实现。

  首先把下载好的RunHiddenConsole.zip包解压到nginx目录内,RunHiddenConsole.exe的作用是在执行完命令行脚本后可以自动关闭脚本,而从脚本中开启的进程不被关闭。然后来创建脚本,命名为“start_nginx.bat”,我们在Notepad++里来编辑它

复制代码
    
     
     
@echo off REM Windows 下无效 REM set PHP_FCGI_CHILDREN=5 REM 每个进程处理的最大请求数,或设置为 Windows 环境变量 set PHP_FCGI_MAX_REQUESTS = 1000 echo Starting PHP FastCGI ... RunHiddenConsole D: / wnmp / php5 / php-cgi . exe -b 127.0 . 0.1 : 9000 -c D: / wnmp / php5 / php . ini echo Starting nginx ... RunHiddenConsole D: / wnmp / nginx / nginx . exe -p D: / wnmp / nginx
复制代码

再另外创建一个名为stop_nginx.bat的脚本用来关闭nginx

复制代码
    
     
     
@echo off echo Stopping nginx ...     taskkill / F / IM nginx . exe > nul echo Stopping PHP FastCGI ... taskkill / F / IM php-cgi . exe > nul exit
复制代码

做好后,是这样的

这样,我们的服务脚本也都创建完毕了。双击start_nginx.bat看看进程管理器是不是有两个nginx.exe的进程和一个php-cgi.exe的进程呢?

这样nginx服务就启动了,而且php也以fastCGI的方式运行了。

到站点目录下,新建一个phpinfo.php的文件,在里面编辑

    
     
     
<? php phpinfo (); ?>

保存后,打开浏览器输入“http://localhost/phpinfo.php”,如果看到

就说明,nginx+php的环境已经配置好了,呵呵~







Nginx   配置文件


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

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

	 
		root   D:\php\pingqiujia; 
		#root   "D:/www/project/pingqiujia/";
   
		 location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }

		if ($request_filename !~ (static|tmp|docment|robots\.txt|ceshi.php|index\.php.*) ) {
            rewrite ^/(.*)$ /index.php?$1 last;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
		
        #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;
        #}

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

}


快捷启动    start_nginx.bat

@echo off
echo Starting PHP FastCGI...
D:\program\nginx-1.6.3\RunHiddenConsole.exe D:\program\php-5.6.15-nts-Win32-VC11-x64\php-cgi.exe -b 127.0.0.1:9000 -c D:\program\php-5.6.15-nts-Win32-VC11-x64\php.ini
echo Starting nginx...
D:\program\nginx-1.6.3\RunHiddenConsole.exe D:/program/nginx-1.6.3/nginx.exe -p D:/program/nginx-1.6.3

快捷关闭  stop_nginx.bat

@echo off
echo Stopping nginx...  
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
exit








猜你喜欢

转载自blog.csdn.net/zwx_lucky/article/details/78035314