本文共 2844 字,大约阅读时间需要 9 分钟。
nginx的下载地址:http://nginx.org/en/download.html
下载后将以上Nginx文件解压到c:\wnmp(任意目录均可)目录下,打开c:\wnmp\nginx\conf\nginx.conf配置文件,注意Nginx的端口默认是80,如果当前计算机上已经有监听80端口的程序,例如apache、IIS,请将端口修改为其它 数字。
server { listen 80; ...}
打开一个控制台窗口,进入c:\wnmp\ngix目录,在控制台窗口输入nginx即可启动nginx。
在浏览器里输入:http://localhost/,如果nginx没有问题,在浏览器里可以看到如下显示:
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to .
Commercial support is available at .Thank you for using nginx.
若出现403 forbidden,可能是80端口被占用,在命令行执行如下命令查看:netstat -ano | findstr "80"
若80端口确实被其他进程占用,可以修改nginx配置文件c:\wnmp\nginx\conf\nginx.conf,找到listen 80,把80改成其他没被使用的端口号,例如listen 8080,然后重启nginx访问http://localhost:8080即可。
从PHP的Windows发布站点()下载,建议安装PHP7。
将以上文件解压到c:\wnmp\php目录里,确保c:\wnmp\php\php-cgi.exe文件存在,这是php的cgi服务器程序。
nginx仅仅是一个静态文件服务器,如果要处理动态页面,则需要搭建其它外围程序,例如Apache,IIS等。在这里,nginx执行PHP代码是将请求转发给php内建的php-cgi服务程序来处理的。
(1)location / { root html; index index.html index.htm;}改为:location / { root c:/wnmp/nginx/html; index index.html index.htm index.php;}(2)#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 c:/wnmp/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;}
注意将 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 修改为
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;C:/WNMP/php/php-cgi.exe -b 127.0.0.1:9000 -c C:/WNMP/php/php.ini
如果正常,将打印出PHP的环境信息。
8)快捷方式启动停止nginx与PHP
如果每次都手工启动nginx与php,将非常麻烦,可以编辑一个批处理文件来方便启动nginx与PHP。
在c:\wnmp目录下编辑一个startwnmp.bat,内容如下
@echo offset PHP_FCGI_MAX_REQUESTS=1000echo Starting PHP FastCGI...RunHiddenConsole /l C:/WNMP/php/php-cgi.exe -b 127.0.0.1:9000 -c C:/WNMP/php/php.iniecho Starting nginx...RunHiddenConsole /l C:/WNMP/nginx/nginx.exe -p C:/WNMP/nginx
以上批处理中的RunHiddenConsole为一个外部程序,故名思义,用于隐藏控制台窗口。我们知道,通常情况下,一个控制台程序如果不退出则会一直霸占控制台,导致批处理文件无法执行下一步,RunHiddenConsole的作用就是释放一个长期运行的程序对控制台的占用,让控制台程序运行在后台,类似于linux的守护进程模式。
RunHiddenConsole的官方下载地址为:里面有已编译好的x64位目标代码,一般情况下我们直接用它即可,如果没有符合条件的,也可以用源代码进行编译。
如果需要方便停止nginx与PHP,则在c:\wnmp目录下编辑一个stopwnmp.bat文件,内容如下:
@echo offecho Stopping nginx...taskkill /F /IM nginx.exe > nulecho Stopping PHP FastCGI...taskkill /F /IM php-cgi.exe > nul
结束语:
采用php-cgi模式,则nginx运行php代码将不再需要Apache,而且性能高。搭建起来比WAMP方便多了,笔者推荐使用WNMP来做PHP运行环境。