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

Windows下配置Nginx+php+mysql

2010-06-04 15:54 411 查看
一、准备工作

1、下载nginx

 

      http://nginx.org/en/download.html

      以nginx-0.7.65.zip为例

 

2、下载php

 

      http://cn.php.net/get/php-5.2.13-Win32.zip/from/this/mirror

      php-5.3.2-nts-Win32-VC9-x86.zip

 

3、下载RunHiddenConsole

     http://blogbuildingu.com/files/RunHiddenConsole.zip

 

二、安装步骤

 

1、创建安装目录

 

     在C盘创建两个目录nginx和php5,把nginx-0.7.65.zip解压到nginx文件中,把php-5.3.2-nts-Win32-VC9-x86.zip解压到php5文件夹中。将RunHiddenConsole.zip解压,将RunHiddenConsole.exe放到php5目录下。

 

2、参数配置

  

    2.1 php配置

  

    打开php5文件夹,找到php.ini-recommended,更名为php.ini 。用UltraEdit编辑。

 

    找到; cgi.fix_pathinfo=1,去掉前面的;
    找到
    ;extension=php_gd2.dll
    ;extension=php_mysql.dll
    ;extension=php_mysqli.dll

    把前面的;去掉,这样就能支持gd图形、mysql数据库连接,如果需要其他的扩展功能,可以去掉对应的;

 

    找到extension_dir 改为 extension_dir = "C:/php5/ext"。

 

    将C:/php5添加到我的电脑环境变量path中。右键“我的电脑”=》“属性”=》“高级”=》“环境变量”=》“系统变量”=》双击   “path”=>添加“;C:/php5”。

   

    2.2 Nginx配置

 

    打开nginx文件下的conf,用UltraEdit编辑nginx.conf文件,

 

   

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

把里面的#去掉。并将/scripts替换为 $document_root
其中root可以随意指定你所要的目录,特别要注意和
location / {
root html;
index index.html index.htm;
}
里面的root保持一致。否则会出错。

 

配置好的配置文件展示:

#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 {
# 一个进程所处理的最大连接数上限,
# 本地开发,不需要默认的 1024,这里改为 64
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 koi8-r;

#access_log  logs/host.access.log  main;

location / {
root   html;
index  index.html index.htm;
# 3. 没有索引页时,罗列文件和子目录
autoindex on;
autoindex_exact_size on;
autoindex_localtime 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  $document_root$fastcgi_script_name;
# fastcgi_split_path_info     ^(.+/.php)(.*)$;
# fastcgi_param PATH_INFO     $fastcgi_path_info;
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;
#    }
#}

}


    

 

   

3、用命令行启动。
随便找一个目录,桌面也可以,常用的即可。新建文本文件start_nginx.bat批处理文件来启动nginx和php。

RunHiddenConsole.exe 是一个用来隐藏 DOS 窗口的小程序,可以在这里下载。

内容如下

 

@echo off
REM Windows 下无效
REM set PHP_FCGI_CHILDREN=5

REM 每个进程处理的最大请求数,或设置为 Windows 环境变量
set PHP_FCGI_MAX_REQUESTS=1000

echo Starting PHP FastCGI…
RunHiddenConsole C:/php5/php-cgi.exe -b 127.0.0.1:9000 -c C:/php5/php.ini

echo Starting nginx…
C:/nginx/nginx.exe

 

 

RunHiddenConsole.exe 是一个用来隐藏 DOS 窗口的小程序,在上面步骤中放在php5目录下。

 

点击start_nginx.bat 。任务管理器查看进程,有php-cgi.exe和nginx.exe进程。

要是没有nginx进程,可以手工启动。

 

到nginx目下直接运行“nginx.exe”。

 

 

同样 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

 

 

4、测试。

 

在c:/nginx/html下编写test.php

<?php
echo phpinfo();
?>


    

  

  

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息