如何在windows下搭建Nginx+MySQL+PHP环境

发布时间:2019-08-07 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了如何在windows下搭建Nginx+MySQL+PHP环境脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

准备所需安装包

本次所选安装版本是:nginx1.11.5,php7.1.0,MySQL5.7.16,当然可以根据喜欢的版本下载,为了方便管理,我在D盘下新建了wnmp文件夹,里面包含文件夹有mySQLphp,nginx,www,www为存放项目文件夹。
Nginx: 前往nginx.org/en/download.htML下载;
PHP:前往Windows.php.net/download下载 ,根据自己的电脑系统位数选择对应版本,顺便说下线程安全版(Thread Safe)和非线程安全版(Non Thread Safe),在Windows下是用FastCGI模式运行php,所以就没必要使用线程安全检查,选用非线程安全版效率会高一些。
MySQL:前往dev.mysql.COM/downloads/mysql下载,选择Windows版本。

一 安装MySQL

1.双击安装包进入安装界面,到第二步后建议选择custom(自定义)安装,这样就可以选择mysql文件和数据库文件的存放位置,不然会全部默认安装在c盘

如何在windows下搭建Nginx+MySQL+PHP环境

进入自定义之后只选择安装server就可以,因为其他用不到

如何在windows下搭建Nginx+MySQL+PHP环境


点击advanced options选择安装在D:/wnmp/mysql中

如何在windows下搭建Nginx+MySQL+PHP环境

之后一直next或者execute,到设置帐号密码这一步设置自己的密码就行了

如何在windows下搭建Nginx+MySQL+PHP环境


之后还是next或者execute,直到出现finished,点击,安装mysql完成。
二 安装PHP


1.将php压缩包解压到D盘新建的wnmp/php文件夹中;
2.将php文件夹中的php.ini-development,复制粘贴,将副本改为php.ini;
3.用记事本打开php.ini
    查找到一下代码并将前面的分号去掉
    ;extension_dir = "ext"
    ;cgi.force_redirect = 1
    ;cgi.fix_pathinfo=1
    ;fastcgi.imPErsonate = 1
    ;cgi.rfc2616_headers = 0
    改为:
    extension_dir = "D:/www/php/ext"
    cgi.force_redirect = 1
    cgi.fix_pathinfo=1
    fastcgi.impersonate = 1
    cgi.rfc2616_headers = 0

三 安装Nginx


1.将Nginx压缩包解压到D盘新建的wnmp/nginx文件夹中;
2.打开nginx/conf/nginx.conf进行配置
 server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_LOG  logs/host.access.log  main;
    location / {
        root   html;
        index  index.html index.htm;
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #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;
    #}
}

改为:
server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location / {
        root   D:/wnmp/www;
        index  index.html index.htm index.php;
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ .php$ {
        root           D:/wnmp/www;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
保存后双击nginx.exe就可以直接运行Nginx服务器了,打开浏览器输入localhost就可以看到欢迎页。

如何在windows下搭建Nginx+MySQL+PHP环境

四 调试

1.打开命令提示符输入D:进入到D盘

D:>cd wnmp/php
D:wnmpPHP>php-cgi.exe -b 127.0.0.1:9000 -c php.ini
启动后再新打开一个命令提示符,输入:netstat -a:findstr "9000"查看9000端口是否被监听,如果是说明cgi运行成功。

如何在windows下搭建Nginx+MySQL+PHP环境

        
2.在www目录下新建index.php
<?php
    $con = mysqli_connect('localhost','root','root','test');
    if($con){
        echo '链接数据库成功!';
    }else{
        echo '链接数据库失败!';
    }
    运行之后输出下面结果,说明可以解析PHP文件且可以链接数据库。

如何在windows下搭建Nginx+MySQL+PHP环境

脚本宝典总结

以上是脚本宝典为你收集整理的如何在windows下搭建Nginx+MySQL+PHP环境全部内容,希望文章能够帮你解决如何在windows下搭建Nginx+MySQL+PHP环境所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。