window+php+nginx环境搭建,打造绿色便携版web服务器

来源:赵克立博客 分类: PHP 标签:服务器nginx发布时间:2017-07-16 14:16:15最后更新:2018-12-16 16:14:13浏览:1199
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2018-12-16 16:14:13
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章

本篇文章配置一个绿色的nginx-php服务器

首先准备需要的软件

nginx-1.12.1.zip   下载地址   http://nginx.org/

php5.6 nts  版本   下载地址  http://php.net/downloads.php  nginx下php是以FastCGI的方式运行,所以我们下载非线程安全也就是nts的php包

RunHiddenConsole.zip   下载地址 http://redmine.lighttpd.net/attachments/660/RunHiddenConsole.zip   RunHiddenConsole.exe的作用是在执行完命令行脚本后可以自动关闭脚本,而从脚本中开启的进程不被关闭

配置php的扩展

打开php.ini文件把扩展目录路径设置好再打开需要的扩展

extension_dir = "./ext"

php和nginx结合

首先在php.ini文件中找到 cgi.fix_pathinfo=1    去掉前面的 分号是为啦让nginx支持path_info,php的东西已经配置完啦剩下的就是nginx

看下php和nginx的目录位置

image.png

打开conf/nginx.conf 修改站点的目录位置

location / {
    root   E:/nginxwww;
    index  index.html index.htm;
}

再往下找到

image.png


改为下面

location ~ \.php$ {
    root           E:/nginxwww;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

去掉前面的#号,根目录位置同样改成跟上面一样的,并且把  /scripts   改为   $document_root
ok已经配置完毕,然后就是启动啦,下面我们创建两个脚本来启动和停止

start_nginx.bat

@echo off
REM set PHP_FCGI_CHILDREN=5REM 每个进程处理的最大请求数
set PHP_FCGI_MAX_REQUESTS=1000
echo Starting PHP FastCGI...
RunHiddenConsole ./php56nts/php-cgi.exe -b 127.0.0.1:9000 -c ./php56nts/php.ini
echo Starting nginx...
RunHiddenConsole ./nginx.exe -p ./

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

最终的目录

image.png

打开浏览器输入loalhost:8088查看效果,因为我配置的是8088端口默认的是80端口

image.png

PATH_INFO的配置

现在很多框架都使用pathinfo来优化url所有我们也要配置下nginx让它支持这两种方法

第一种:把请求路径丢给php去解析

在php.ini里面把cgi.fix_pathinfo=1    这个开启下

location ~ \.php {    #去掉$
     root           E:/nginxwww;
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     fastcgi_param PATH_INFO $fastcgi_script_name; #增加这一句
     include        fastcgi_params;
}

等于把请求路径都传给php然后php会修复这个路径,解析对应的信息到对应的服务器变量中去使用

第二种:nginx使用自己的正则解析

location ~ \.php {    #去掉$
     root           E:/nginxwww;
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     fastcgi_split_path_info ^(.+\.php)(.*)$;     #增加这一句
     fastcgi_param PATH_INFO $fastcgi_path_info;    #增加这一句
     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     include        fastcgi_params;
}

伪静态设置

nginx里面直接写规则就可以使用,直接在前面添加一个判断文件是否存在,如果不存在就转发到index.php

location ~ \.php {    #去掉$
     root           E:/nginxwww;
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     fastcgi_split_path_info ^(.+\.php)(.*)$;     #增加这一句
     fastcgi_param PATH_INFO $fastcgi_path_info;    #增加这一句
     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     include        fastcgi_params;
}

也可以在nginx.conf里面再定义一些转发规则把那些前面没有匹配到的都转发到index.php上来实现隐藏index.php的功能

location / {
     if (!-e $request_filename){
          rewrite ^/(.*)$ /index.php/$1 last;
     }
}



后记:如果想把nginx注册成系统服务运行的话可以到下面位置下载一个工具

https://github.com/kohsuke/winsw/releases


下载后重命名为nginx-server.exe 然后创建一个配置文件nginx-server.xml文件写入

<configuration>
  
  <!-- ID of the service. It should be unique accross the Windows system-->
  <id>nginx-server</id>
  <!-- Display name of the service -->
  <name>nginx-server Service (powered by WinSW)</name>
  <!-- Service description -->
  <description>This service is a service cratead from a minimal configuration</description>
  
  <!-- Path to the executable, which should be started -->
  <executable>%BASE%\nginx.exe</executable>
</configuration>

放入nginx.exe目录中,打开命令行cd到当前目录输入下面命令安装为服务运行

nginx-server install

image.png

如上图所示已经安装进去啦。卸载的时候使用

nginx-server uninstall



微信号:kelicom QQ群:215861553 紧急求助须知
Win32/PHP/JS/Android/Python