最近在玩Nginx(发音:engine x,可以玩“反向代理”和“负载均衡”的轻量级web服务器,据说阿里巴巴的大牛们都在用这个),现在将【Windows下配置Nginx1.7.11+PHP5.6.7的开发环境】在此说明一下。(网上好多Nginx+PHP配置的版本都比较旧了,但是大同小异,我这里重新总结一个最新的)
1.软件准备
点击下载: Nginx/Windows-1.7.11
点击下载:PHP5.6.7
点击下载:RunHiddenConsole
2.配置
把下载好的压缩包解压到自己喜欢的文件夹,文件名或者文件路径最好不要包含中文字符。
然后配置 nginx的配置文件,位置.\nginx\conf\nginx.conf
<span style="font-family:Microsoft YaHei;font-size:18px;">#2015-3-27-我的Nginx的修改
#运行用户
#user nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes 1;
#全局错误日志及PID文件
#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服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义
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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩
#gzip on;
server {
#侦听80端口
listen 80;
server_name localhost;
#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;
#默认请求
<span style="background-color: rgb(255, 0, 0);"><span style="color:#ffffff;"> location / {
root D:\myWebSite\phpwin64\www; #定义服务器的默认网站根目录位置
index index.html index.htm index.php; #定义首页索引文件的名称
}</span></span>
# 定义错误提示页面
#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;
#}
#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
<span style="background-color: rgb(255, 0, 0);"><span style="color:#ffffff;"> location ~ \.php$ {
root D:\myWebSite\phpwin64\www; 注意此次一定要修改为自己的
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}</span></span>
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
# 禁止访问 .htxxx 文件
#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;
# }
#}
}
</span>
然后配置,php。位置在.\phpwin64\php.ini (把php.ini-development改为php.ini)
; On windows:
;extension_dir ="./ext" 更改为 extension_dir = "自己的php路径/ext" 注意:对于windows的机器,一定是修改前边带有; On windows:的这个 把下边的去掉;号,来支持mysql(mysql的配置连接,之后再说) ;extension=php_mysql.dll ;extension=php_mysqli.dll
然后找到;cgi.fix_pathinfo=1改为cgi.fix_pathinfo=1
3.设置批处理启动文件
新建start_nginx.bat
<span style="font-family:Microsoft YaHei;font-size:18px;"><span style="font-family:Microsoft YaHei;font-size:18px;">@echo off
REM Windows 下无效
REM set PHP_FCGI_CHILDREN=5
REM 每个进程处理的最大请求数,或设置为 Windows 环境变量
set PHP_FCGI_MAX_REQUESTS=1000
echo Starting PHP FastCGI...
D:/myWebSite/nginx/RunHiddenConsole.exe D:/myWebSite/phpwin64/php-cgi.exe -b 127.0.0.1:9000 -c D:/myWebSite/phpwin64/php.ini
echo Starting nginx...
D:/myWebSite/nginx/RunHiddenConsole.exe D:/myWebSite/nginx/nginx.exe -p D:/myWebSite/nginx</span></span>
新建stop_nginx.bat
<span style="font-family:Microsoft YaHei;font-size:18px;">@echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
exit</span>
最好文件夹是这个样子
启动start_nginx.bat,会出现如下线程。
记得在php的文件夹下建立一个www文件夹,并编辑一个<?php phpinfo();?>进去
测试效果