初识Nginx,Windows下环境搭建

环境:
Windows 7 32位
nginx 1.1.7 dev
php 5.3.8
mysql 5.5.17
RunHiddenConsole和MySQL绿色版下载http://dl.dbank.com/c0ejffpgkx

开始前的准备: 

1.目录:
wnmp/
    nginx/
        start.bat
        conf/vhosts.conf
    php/
    mysql/
    wwwroot/
    vhosts/
    RunHiddenConsole.exe
    start.bat
    stop.bat

解压nginx、php分别到nginx、php目录

2.修改php.ini

一定要记得修改extension_dir,改成ext即可。之前好几次粗心忘了改,结果所有扩展死活不起作用。

然后修改如下值:
cgi.force_redirect=1
cgi.fix_pathinfo=1
fastcgi.impersonate=1
cgi.rfc2616_headers=1

分别什么意思,在php.ini里面相应位置都有说明。

然后开启需要开启的扩展,有些扩展是有依赖的,就是开启某些扩展之前必须要先开启其依赖的扩展才能正常使用。

3.修改nginx/conf/nginx.conf

  1. worker_processes  1;   
  2. error_log  logs/error.log;   
  3. events {   
  4.     worker_connections  64;#这里就不用默认的1024了,我们本地调试用不了这么多   
  5. }   

然后是http里关于fastcgi的配置,记得配置在http内server外。

  1. http {   
  2.     #......   
  3.     fastcgi_connect_timeout 300;   
  4.     fastcgi_send_timeout 300;   
  5.     fastcgi_read_timeout 300;   
  6.     fastcgi_buffer_size 64k;   
  7.     fastcgi_buffers 4 64k;   
  8.     fastcgi_busy_buffers_size 128k;   
  9.     fastcgi_temp_file_write_size 128k;   
  10.        
  11.     server {   
  12.         #...   
  13.     }   
  14. }  

在然后是默认网站的设置

  1. server {   
  2.     listen 80;   
  3.     servername localhost 127.0.0.1;   
  4.     index index.htm index.html index.php;   
  5.        
  6.     root html;#这里是相对目录,如果要换到整个环境根目录下的wwwroot必须使用绝对路径,../../wwwroot是不行的,方法还在摸索中。其他虚拟主机同样   
  7.        
  8.     location ~ .*\.(php)?$ {   
  9.         fastcgi_pass 127.0.0.1:9000   
  10.         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;   
  11.         fastcgi_split_path_info  ^(.+\.php)(.*)$;   
  12.         include conf/fastcgi_params;   
  13.     }   
  14. }  

然后在配置其他“虚拟主机配置”

  1. http {   
  2.     server {   
  3.         #.....   
  4.     }   
  5.   
  6.     include conf/vhosts.conf   
  7. }  

vhosts.conf

  1. server {   
  2.     listen 80;   
  3.     servername a.com *.ab.com;   
  4.     index index.htm index.html index.php;   
  5.        
  6.     root X:\wnmp\vhosts\xxx;#这里是相对目录,如果要换到整个环境根目录下的wwwroot必须使用绝对路径,../../wwwroot是不行的,方法还在摸索中。其他虚拟主机同样   
  7.        
  8.     location ~ .*\.(php)?$ {   
  9.         fastcgi_pass 127.0.0.1:9000   
  10.         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;   
  11.         fastcgi_split_path_info     ^(.+\.php)(.*)$;   
  12.         include conf/fastcgi_params;   
  13.     }   
  14. }  

多个虚拟主机直接在vhosts.conf里加配置就行。

4.编写配置脚本

nginx/start.bat

  1. cd nginx   
  2. %cd%/../RunHiddenConsole nginx   
  3. cd ..  

根目录下的start.bat

  1. @echo off   
  2. set PHP_FCGI_MAX_REQUESTS=1000   
  3. echo 正在启动PHP FastCGI...   
  4. RunHiddenConsole php\php-cgi.exe -c 127.0.0.1:9000 -c php\php.ini   
  5. echo 正在启动Nginx...   
  6. call nginx\start.bat   
  7. echo 正在启动MySQL...   
  8. RunHiddenConsole  mysql\bin\mysqld-nt.exe   
  9. echo 启动成功...  

根目录下的stop.bat

  1. @echo off   
  2. echo 正在停止PHP FastCGI...   
  3. taskkill /F /IM php-cgi.exe > nul   
  4. echo 正在停止Nginx...   
  5. taskkill /F /IM nginx.exe > nul   
  6. echo 正在停止MySQL   
  7. taskkill /F /IM mysqld-nt.exe  

到此,配置结束。

修补一下nginx的漏洞(实际上是php的)

修改nginx/conf/fastcgi_params.conf最开头加上

  1. if ($request_filename ~* (.*)\.php) {   
  2.     set $php_url $1;   
  3. }   
  4. if (!-e $php_url.php) {   
  5.     return 403;   
  6. }  
  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓