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

2017-12-01 00:10:02 924 思小齐 思小齐

准备所需安装包

本次所选安装版本是:nginx1.11.5,php7.1.0,mysql5.7.16,当然可以根据喜欢的版本下载,为了方便管理,我在D盘下新建了wnmp文件夹,里面包含文件夹有mysql,php,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盘;

这里写图片描述

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

这里写图片描述
点击advanced options选择安装在D:/wnmp/mysql中
这里写图片描述

之后一直next或者execute,到设置帐号密码这一步设置自己的密码就行了
这里写图片描述
之后还是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就可以看到欢迎页。

这里写图片描述

四 调试

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

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

这里写图片描述

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

这里写图片描述