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

準備所需安裝包php

本次所選安裝版本是: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版本。html

一 安裝MySQLmysql

1.雙擊安裝包進入安裝界面,到第二步後建議選擇custom(自定義)安裝,這樣就能夠選擇mysql文件和數據庫文件的存放位置,否則會所有默認安裝在C盤;

這裏寫圖片描述

進入自定義以後只選擇安裝server就能夠,由於其餘用不到

這裏寫圖片描述
點擊advanced options選擇安裝在D:/wnmp/mysql中
這裏寫圖片描述nginx

以後一直next或者execute,到設置賬號密碼這一步設置本身的密碼就好了
這裏寫圖片描述
以後仍是next或者execute,直到出現finished,點擊,安裝mysql完成。
二 安裝PHPsql

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文件且能夠連接數據庫。

這裏寫圖片描述

相關文章
相關標籤/搜索