• 对于互联网行业者而言,一个新的趋势正在形成:得移动者,得天下
  • 海强说运营:专业提供web网站、ASO、新媒体、自媒体运营服务策划
  • 快报关注:海强说运营,了解天下不同事
  •    1年前 (2017-03-03)  科技 |   3 条评论  4 
    文章评分 0 次,平均分 0.0

    要求

      • 必备知识

        熟悉基本编程环境搭建。

      • 运行环境

        windows 7(64位);
        nginx-1.4.7;
        MySQL Server 5.5
        php-5.4.39-nts

      • 下载地址

        环境下载

    <span class="typ">Nginx</span><span class="pun">是一款轻量级的</span><span class="typ">Web</span> <span class="pun">服务器/反向代理服务器及电子邮件(</span><span class="pln">IMAP</span><span class="pun">/</span><span class="pln">POP3</span><span class="pun">)代理服务器,并在一个</span><span class="pln">BSD</span><span class="pun">-</span><span class="pln">like </span><span class="pun">协议下发行。由俄罗斯的程序设计师</span><span class="typ">Igor</span> <span class="typ">Sysoev</span><span class="pun">所开发,供俄国大型的入口网站及搜索引擎</span><span class="typ">Rambler</span><span class="pun">(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上</span><span class="pln">nginx</span><span class="pun">的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用</span><span class="pln">nginx</span><span class="pun">网站用户有:新浪、网易、腾讯等。</span>

    上面这段介绍,摘自百度! 看了介绍就知道nginx很有逼格了shi不shi啊!那么问题来了,这么有逼格的配置,在windows下如何搭建呢? 好吧,就让我给大家介绍一下,windows下如何搭建 nginx+mysql+php 环境吧。

    官方下载地址

    至于我使用的版本已经上传到百度云提供大家下载了哦,请戳以下链接:

    Nginx

    这么有逼格的服务器,我就不多做介绍,前面也提到了一些。我就直接安装吧,把下载下来的nginx-1.4.7.zip文件,解压到指定的目录就OK了,下面贴一下我的目录结构哦

    wnmp(windows+nginx+mysql+php)环境搭建和配置

    下面是nginx的一些基本操作指令,当然你也可以直接双击nginx.exe文件来启动服务器(^_^)

    <span class="pln">start nginx    </span><span class="com">//</span><span class="com">启动服务</span><span class="pln">
     nginx </span><span class="pun">-</span><span class="pln">s stop    </span><span class="com">//</span><span class="com"> 停止nginx</span><span class="pln">
    nginx </span><span class="pun">-</span><span class="pln">s reload    </span><span class="com">//</span><span class="com"> 重新加载配置文件</span><span class="pln">
    nginx </span><span class="pun">-</span><span class="pln">s quit    </span><span class="com">//</span><span class="com"> 退出nginx</span>

    在浏览器地址中输入localhost ,如出现下图说明安装成功了!

    wnmp(windows+nginx+mysql+php)环境搭建和配置

    MySql

    关于mysql的安装,请自行去解决(^_^),我不想多说了!好吧,可以参考我之前写一篇文章,里面有详细的介绍哦,你会明白的(^_^)

    PHP

    关于PHP的安装,我也不想说了! 额,你其实也可以参考。。。你会懂的,哈哈!好吧,往下看吧!

    php提供了两种版本可以选择,即线程安全和非线程安全

    • TS:Thread Safe 线程安全, 执行时会进行线程(Thread)安全检查,以防止有新要求就启动新线程的CGI执行方式而耗尽系统资源
    • NTS:Non Thread Safe 非线程安全, 在执行时不进行线程(Thread)安全检查

    PHP的两种执行方式:ISAPI和FastCGI。

    • CGI(通用网关接口/Common Gateway Interface)一般是可执行程序,例如EXE文件,和WEB服务器各自占据着不同的进程,而且一般一个CGI程序只能处理一个用户请求。这样,当用 户请求数量非常多时,会大量占用系统的资源,如内存、CPU时间等,造成效能低下。
    • ISAPI(Internet Server Application Program Interface)是微软提供的一套面向WEB服务的API接口,它能实现CGI提供的全部功能,并在此基础上进行了扩展,如提供了过滤器应用程序接 口。ISAPI应用大多数以DLL动态库的形式使用,可以在被用户请求后执行,,在处理完一个用户请求后不会马上消失,而是继续驻留在内存中等待处理别的 用户输入。此外,ISAPI的DLL应用程序和WEB服务器处于同一个进程中,效率要显著高于CGI。所以如果是以ISAPI来执行PHP,建议选择Thread Safe版本
    • FastCGI是可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。传统的CGI解释器的反复加载是 CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性等。nginx下php是以FastCGI的方式运行,所以我们下载Non Thread Safe版本.

    我知道你已经懂了,shi不shi呀! 不过网上有些人下的TS有的人下的NTS的,选择困难症有犯了,哈哈~~~!下非线程安全的吧,因为我就下的就是这个,待会自己去百度云下的时候别选错了哦~~

    将压缩文件解压到指定的文件夹(这里你爱咋整就咋整,可以参考我上面的目录结构)。配置php.ini文件,php提供了两个模板,php.ini-development和php.ini-production,前者适合开发程式使用(测试用),后者拥有较高的安全性设定,则适合上线当产品使用。这里我们将php.ini-development文件改为php.ini做配置文件使用。

    修改扩展dll文件目录:

    <span class="com">//这里根据自己的实际情况而定</span><span class="pln">
    extension_dir </span><span class="pun">=</span> <span class="str">"E:\2015\wnmp\php\ext"</span>

    加入扩展:

    选择需要运行哪些扩展,只需将extension前面的注释去掉,例如:

    <span class="pln">extension</span><span class="pun">=</span><span class="pln">php_mysql</span><span class="pun">.</span><span class="pln">dll
    extension</span><span class="pun">=</span><span class="pln">php_mysqli</span><span class="pun">.</span><span class="pln">dll</span>

    CGI 设置

    <span class="pln">enable_dl </span><span class="pun">=</span> <span class="typ">On</span><span class="pln">
    cgi</span><span class="pun">.</span><span class="pln">force_redirect </span><span class="pun">=</span> <span class="lit">0</span><span class="pln">
    cgi</span><span class="pun">.</span><span class="pln">fix_pathinfo</span><span class="pun">=</span><span class="lit">1</span><span class="pln">
    fastcgi</span><span class="pun">.</span><span class="pln">impersonate </span><span class="pun">=</span> <span class="lit">1</span><span class="pln">
    cgi</span><span class="pun">.</span><span class="pln">rfc2616_headers </span><span class="pun">=</span> <span class="lit">1</span>

    配置Nginx

    这里所说的配置,主要是讲如何让Nginx对PHP提供支持!!打开nginx目录下conf文件夹里的nginx.conf(这就是我的配置文件了)

    修改如下代码,位置如下

    wnmp(windows+nginx+mysql+php)环境搭建和配置

    修改网站的根目录,添加index.php的默认页支持,修改后的代码如下。

    <span class="pln">location </span><span class="pun">/</span> <span class="pun">{</span><span class="pln">
                root   E</span><span class="pun">:</span><span class="str">/2015/</span><span class="pln">wnmp</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">html</span><span class="pun">;</span><span class="pln">
                index  index</span><span class="pun">.</span><span class="pln">html index</span><span class="pun">.</span><span class="pln">htm index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">;</span>
    <span class="pun">}</span>

    让nginx支持PHP的设置,代码位置如下

    wnmp(windows+nginx+mysql+php)环境搭建和配置

    先将前面的“#”去掉,同样将root  html;改为root  E:/2015/wnmp/nginx/html;。再把“/scripts”改为“$document_root”,这里的“$document_root”就是指前面“root”所指的站点路径,改完后的代码如下:

    <span class="pln">location </span><span class="pun">~</span><span class="pln"> \.php$ </span><span class="pun">{</span><span class="pln">
                root           E</span><span class="pun">:</span><span class="str">/2015/</span><span class="pln">wnmp</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">html</span><span class="pun">;</span><span class="pln">
                fastcgi_pass   </span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">0.1</span><span class="pun">:</span><span class="lit">9000</span><span class="pun">;</span><span class="pln">
                fastcgi_index  index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">;</span><span class="pln">
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name</span><span class="pun">;</span><span class="pln">
                include        fastcgi_params</span><span class="pun">;</span>
    <span class="pun">}</span>

    上述配置改为后,别玩了要重启nginx哦,操作指令我已经到上面给出了啊!

    测试

    启动php内置的cgi程序,在9000端口监听nginx发过来的请求:

    <span class="pln">E</span><span class="pun">:</span><span class="pln">\2</span><span class="lit">015</span><span class="pln">\wnmp\php</span><span class="pun">&gt;</span><span class="pln">php</span><span class="pun">-</span><span class="pln">cgi</span><span class="pun">.</span><span class="pln">exe </span><span class="pun">-</span><span class="pln">b </span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">0.1</span><span class="pun">:</span><span class="lit">9000</span><span class="pun">-</span><span class="pln">c E</span><span class="pun">:</span><span class="pln">\wnmp\php\php</span><span class="pun">.</span><span class="pln">ini</span>

    PS:上一步操作中如果没有重启nginx的话,现在重启一遍吧!!

    在网站更目录(上步操作中root所指向的路径我这里是 E:/2015/wnmp/nginx/html)下创建phpinfo.php文件,代码如下

    <span class="pun">&lt;?</span><span class="pln">php </span><span class="pln">phpinfo</span><span class="pun">();</span> <span class="pun">?&gt;</span>

    在浏览器地址栏中输入http://localhost/phpinfo.php,一次性点亮,是不是很Happy!!

    wnmp(windows+nginx+mysql+php)环境搭建和配置

    RunHiddenConsole配置

    首先把下载好的RunHiddenConsole.zip包解压到nginx目录内,RunHiddenConsole.exe的作用是在执行完命令行脚本后可以自动关闭脚本,而从脚本中开启的进程不被关闭。

    创建start_nginx.bat文件

    <span class="lit">@echo</span><span class="pln"> off
    REM </span><span class="typ">Windows</span> <span class="pun">下无效</span><span class="pln">
    REM </span><span class="kwd">set</span><span class="pln"> PHP_FCGI_CHILDREN</span><span class="pun">=</span><span class="lit">5</span><span class="pln">
    
    REM </span><span class="pun">每个进程处理的最大请求数,或设置为</span> <span class="typ">Windows</span> <span class="pun">环境变量</span>
    <span class="kwd">set</span><span class="pln"> PHP_FCGI_MAX_REQUESTS</span><span class="pun">=</span><span class="lit">1000</span><span class="pln">
     
    echo </span><span class="typ">Starting</span><span class="pln"> PHP </span><span class="typ">FastCGI</span><span class="pun">...</span>
    <span class="typ">RunHiddenConsole</span><span class="pln"> E</span><span class="pun">:</span><span class="str">/2015/</span><span class="pln">wnmp</span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="pln">php</span><span class="pun">-</span><span class="pln">cgi</span><span class="pun">.</span><span class="pln">exe </span><span class="pun">-</span><span class="pln">b </span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">0.1</span><span class="pun">:</span><span class="lit">9000</span> <span class="pun">-</span><span class="pln">c E</span><span class="pun">:</span><span class="str">/2015/</span><span class="pln">wnmp</span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">ini
     
    echo </span><span class="typ">Starting</span><span class="pln"> nginx</span><span class="pun">...</span>
    <span class="typ">RunHiddenConsole</span><span class="pln"> E</span><span class="pun">:</span><span class="str">/2015/</span><span class="pln">wnmp</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">.</span><span class="pln">exe </span><span class="pun">-</span><span class="pln">p E</span><span class="pun">:</span><span class="str">/2015/</span><span class="pln">wnmp</span><span class="pun">/</span><span class="pln">nginx</span>

    创建stop_nginx.bat脚本,对应的是用来关闭nginx服务

    <span class="lit">@echo</span><span class="pln"> off
    echo </span><span class="typ">Stopping</span><span class="pln"> nginx</span><span class="pun">...</span><span class="pln">  
    taskkill </span><span class="pun">/</span><span class="pln">F </span><span class="pun">/</span><span class="pln">IM nginx</span><span class="pun">.</span><span class="pln">exe </span><span class="pun">&gt;</span><span class="pln"> nul
    echo </span><span class="typ">Stopping</span><span class="pln"> PHP </span><span class="typ">FastCGI</span><span class="pun">...</span><span class="pln">
    taskkill </span><span class="pun">/</span><span class="pln">F </span><span class="pun">/</span><span class="pln">IM php</span><span class="pun">-</span><span class="pln">cgi</span><span class="pun">.</span><span class="pln">exe </span><span class="pun">&gt;</span><span class="pln"> nul
    </span><span class="kwd">exit</span>

    文件目录结构如下

    wnmp(windows+nginx+mysql+php)环境搭建和配置

    好吧,整个就弄好了!!是不是比较复杂,没关系! 请问你有到linux下交叉编译安装过服务器环境吗?(^_^)hahah~~~。关于linux下服务器环境的“一揽子安装计划”,将到后续的文章中介绍!!点关注就对了(^_^)

     

    除特别注明外,本站所有文章均为海强说运营-海强自媒体-博来号-博来网-分享社群运营,温暖你我原创,转载请注明出处来自https://blog.bolaiedu.com/9368.html

    关于

    发表评论

    表情 格式
    1. 只能说你是个 被愚弄的SB

      新用户150352 评论达人 LV.1 11个月前 (05-12) [0] [0]
    2. 113龙渊的头发不见了

      新用户701121 评论达人 LV.1 12个月前 (05-04) [0] [0]
    3. 坦克表示意义不大

      新用户833443 评论达人 LV.1 1年前 (2017-04-19) [0] [0]
    切换注册

    登录

    忘记密码 ?

    您也可以使用第三方帐号快捷登录

    切换登录

    注册

    扫一扫二维码分享