Home > php教程 > php手册 > body text

Windows7 configuration Nginx+php+mysql tutorial

WBOY
Release: 2016-09-05 08:45:41
Original
979 people have browsed it

Windows7 configuration Nginx+php+mysql tutorial

 I am learning PHP recently and want to record my learning experience and write some experiences for reference and communication only. This article is suitable for students who are new to PHP and want to learn and build their own Nginx+php+mysql environment.

 Of course, you can also choose integrated installation packages, such as wamp, etc., but I recommend that you build the environment manually so that you can better understand PHP and its operation process. Let’s go directly to the topic.

Steps:

1. Prepare installation package, etc.

  (1) nginx-1.10.1.zip, the download link is http://nginx.org/en/download.html. I personally chose the stable version.

  (2) php-5.6.25-nts-Win32-VC11-x86.zip or x64.zip The download link is http://windows.php.net/download#php-5.6, select the corresponding version according to the system type.

  (3) mysql-installer--------.msi The download link is http://dlsw.baidu.com/sw-search-sp/soft/ea/12585/mysql-5.6.24-win32. 1432006610.zip, since the mysql official website requires registration, I directly posted the Baidu address.

2. Install the mysql service (If you have installed mysql before, you don’t need to download and install it, just start the mysql service during operation.)

 Double-click the mysql-install-------.msi installation package to enter the installation environment interface, select the default options for installation, use Typical (typical installation), and start the MySQL configuration wizard, select the default option configuration, and select UTF for encoding here -8. After setting the database password (usually root), "Execute" and click "Finish" to complete the installation. Make sure the mysql service is turned on. The method to check whether the service is turned on is: win+R, enter services.msc and press Enter. Check whether the mysql service is available and in the starting state as shown in the figure below.

   

3. Install nginx-1.10.1

    (1) Unzip the nginx-1.10.1.zip file to C:wnmpnginx, enter the folder, find nginx.exe, double-click nginx.exe to start nginx.

   Enter the Process tab of the Task Manager. If you find the nginx.exe process, it is confirmed that nginx has been started.

  Open the browser and type: http://127.0.0.1 in the browser address bar. If you see the interface as shown below, it means nginx is working normally.

   .

(2) Enter the C:wnmpnginxconf folder and open the nginx configuration file nginx.conf with Notepad or WordPad

   Find the following two places to modify, the modification content is as follows

<span style="color: #000000;">server {
    listen       </span>80<span style="color: #008000;">;
</span>    server_name  localhost<span style="color: #008000;">;
</span>    #modify by lee 20160902<span style="color: #000000;"> for php -s
    location / {
        root   C:/wnmp/www</span><span style="color: #008000;">;
</span>        index  index.html index.htm index.php<span style="color: #008000;">;
</span><span style="color: #000000;">    }
    # pass the PHP scripts to FastCGI server listening on </span>127.0.0.1:9000<span style="color: #000000;"> 
    location ~ \.php$ {
         root           C:/wnmp/www</span><span style="color: #008000;">;
</span>         fastcgi_pass   127.0.0.1:9001<span style="color: #008000;">;
</span>         fastcgi_index  index.php<span style="color: #008000;">;
</span>         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name<span style="color: #008000;">;
</span>         include        fastcgi_params<span style="color: #008000;">;
</span><span style="color: #000000;">    }

    #modify by lee </span>20160902<span style="color: #000000;"> for php -e
}</span>
Copy after login

 4.安装php-5.6.25-nts-Win32-VC11-x64/x86

  (1)解压php-5.6.25-nts-Win32-VC11-x64/x86.zip文件到C:\wnmp\php。

  (2)进入C:\wnmp\php目录内,备份php.ini-development一份,将php.ini-development重命名为php.ini

     查找定位并修改:

      ; extension_dir = "ext" ,删除前面的分号并修改为:extension_dir = "C:/wnmp/php/ext"

      ;extension=php_gd2.dll,删除前面的分号:extension=php_gd2.dll

      ;extension=php_mbstring.dll,删除前面的分号:extension=php_mbstring.dll

      ;extension=php_mysql.dll,删除前面的分号:extension=php_mysql.dll

      ;extension=php_mysqli.dll,删除前面的分号:extension=php_mysqli.dll

      ;extension=php_pdo_mysql.dll,删除前面的分号:extension=php_pdo_mysql.dll

      ;cgi.force_redirect = 1,删除前面的分号:cgi.force_redirect = 1

      ;cgi.fix_pathinfo=1,删除前面的分号:cgi.fix_pathinfo=1

      ;cgi.rfc2616_headers = 0,删除前面的分号:cgi.rfc2616_headers = 1

  (3)配置环境变量:

      打开环境变量配置窗口,

      在系统环境变量中添加 变量  变量名:PHP_HOME    变量值:C:\wnmp\php

      在系统环境变量中添加 变量  变量名:NGINX_HOME  变量值:C:\wamp\nginx

      在path变量最后添加:%PHP_HOME%;%NGINX_HOME%;  (注意添加分号)

  (4)启动php-cgi 服务:

      win+R 输入cmd回车,进入命令提示符界面,进入C:\wnmp\php>,

      使用命令   php-cgi.exe -b 127.0.0.1:9001 -c php.ini 

      重新打开一个新的命令提示符界面,

        输入命令: netstat -ano|findstr "9001"会出现 9001端口号占用情况

        再次输入命令: tasklist|findstr "2892"  (注:2892为上一个命令执行完毕后,最后一列显示的进程号)

        如下图所示:表示 php-cgi服务启动成功

      

  (5)编写启动脚本 start.bat (其实到上步为止,已经配置完成,但是为了以后便于启动各种服务,特此编写一个启动脚本。)

      新建文本文档,重命名为start.bat

      用记事本打开后,做如下编辑:

(如果所以文件目录与本教程所述完全一致,则此脚本可以直接使用,如果有区别,则需要对NGINX_DIR和PHP_DIR做相应修改后使用)

<span style="color: #000000;">@echo off
rem the use of bat
 
</span><span style="color: #0000ff;">echo</span> ==================begin========================<span style="color: #000000;">
 
cls 
SET NGINX_PATH</span>=<span style="color: #000000;">C:
SET NGINX_DIR</span>=<span style="color: #000000;">C:\wnmp\nginx\
SET PHP_DIR</span>=<span style="color: #000000;">C:\wnmp\php\
color 0a 
TITLE Nginx Manager
 
CLS 
 
ECHO. 
ECHO. </span>* * Nginx Manage App   *<span style="color: #000000;">  
ECHO. 
 
:MENU 
 
ECHO. </span>* nginx process list *<span style="color: #000000;">  
tasklist</span>|findstr /i <span style="color: #800000;">"</span><span style="color: #800000;">nginx.exe</span><span style="color: #800000;">"</span><span style="color: #000000;">
 
ECHO. 
    ECHO.  [</span><span style="color: #800080;">1</span><span style="color: #000000;">] start Nginx  
    ECHO.  [</span><span style="color: #800080;">2</span><span style="color: #000000;">] stop Nginx  
    ECHO.  [</span><span style="color: #800080;">3</span><span style="color: #000000;">] restart Nginx  
    ECHO.  [</span><span style="color: #800080;">4</span><span style="color: #000000;">] exit 
ECHO. 
 
ECHO.Please input the number:
set </span>/p ID=<span style="color: #000000;">
    IF </span><span style="color: #800000;">"</span><span style="color: #800000;">%id%</span><span style="color: #800000;">"</span>==<span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;"> GOTO start 
    IF </span><span style="color: #800000;">"</span><span style="color: #800000;">%id%</span><span style="color: #800000;">"</span>==<span style="color: #800000;">"</span><span style="color: #800000;">2</span><span style="color: #800000;">"</span><span style="color: #000000;"> GOTO stop 
    IF </span><span style="color: #800000;">"</span><span style="color: #800000;">%id%</span><span style="color: #800000;">"</span>==<span style="color: #800000;">"</span><span style="color: #800000;">3</span><span style="color: #800000;">"</span><span style="color: #000000;"> GOTO restart 
    IF </span><span style="color: #800000;">"</span><span style="color: #800000;">%id%</span><span style="color: #800000;">"</span>==<span style="color: #800000;">"</span><span style="color: #800000;">4</span><span style="color: #800000;">"</span><span style="color: #000000;"> EXIT
PAUSE 
 
:start 
    call :startNginx
    call :startPhpFastCGI
    GOTO MENU
 
:stop 
    call :shutdownNginx
    GOTO MENU
 
:restart 
    call :shutdownNginx
    call :startNginx
    GOTO MENU
 
:shutdownNginx
    ECHO. 
    ECHO.Stop Nginx...... 
    taskkill </span>/F /IM nginx.exe ><span style="color: #000000;"> nul
    ECHO.Stopping PHP FastCGI......

    ECHO.OK,Stop all nginx process and php fastcgi
    goto :eof
 
:startNginx
    ECHO. 
    ECHO.Start Nginx...... 
    IF NOT EXIST </span><span style="color: #800000;">"</span><span style="color: #800000;">%NGINX_DIR%nginx.exe</span><span style="color: #800000;">"</span> ECHO <span style="color: #800000;">"</span><span style="color: #800000;">%NGINX_DIR%nginx.exe</span><span style="color: #800000;">"</span><span style="color: #000000;"> is not exists 
 
    </span>%NGINX_PATH%<span style="color: #000000;"> 
 
    cd </span><span style="color: #800000;">"</span><span style="color: #800000;">%NGINX_DIR%</span><span style="color: #800000;">"</span><span style="color: #000000;"> 
 
    IF EXIST </span><span style="color: #800000;">"</span><span style="color: #800000;">%NGINX_DIR%nginx.exe</span><span style="color: #800000;">"</span><span style="color: #000000;"> (
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">start '' nginx.exe</span><span style="color: #800000;">"</span><span style="color: #000000;">
        start </span><span style="color: #800000;">""</span><span style="color: #000000;"> nginx.exe
    )
    ECHO.OK
    goto :eof
    
:startPhpFastCGI
    ECHO. 
    ECHO.Start PHP FastCGI......
    IF NOT EXIST </span><span style="color: #800000;">"</span><span style="color: #800000;">%PHP_DIR%php-cgi.exe</span><span style="color: #800000;">"</span> ECHO <span style="color: #800000;">"</span><span style="color: #800000;">%PHP_DIR%php-cgi.exe</span><span style="color: #800000;">"</span><span style="color: #000000;"> is not exists 
 
    </span>%NGINX_PATH%<span style="color: #000000;"> 
 
    cd </span><span style="color: #800000;">"</span><span style="color: #800000;">%PHP_DIR%</span><span style="color: #800000;">"</span><span style="color: #000000;"> 
 
    IF EXIST </span><span style="color: #800000;">"</span><span style="color: #800000;">%PHP_DIR%php-cgi.exe</span><span style="color: #800000;">"</span><span style="color: #000000;"> (
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">start '' php-cgi.exe</span><span style="color: #800000;">"</span><span style="color: #000000;">
        start </span>/b  php-cgi.exe -b <span style="color: #800080;">127.0</span>.<span style="color: #800080;">0.1</span>:<span style="color: #800080;">9001</span> -<span style="color: #000000;">c php.ini
    )
    ECHO.OK
    goto :eof</span>
Copy after login

   (6)测试成功

      在C:\wnmp下新建www文件夹,在www文件夹下新建php文件 index.php,并做如下编辑

      如下mysqli_connect()代码部分需要 做相应更改。

    

<html>
 <head>
  <title>test <span style="color: #008080;">mysql</span>+php+nginx</title>
 </head>
 <body>
  <?<span style="color: #000000;">php
   </span><span style="color: #800080;">$link</span> = <span style="color: #008080;">mysqli_connect</span>("localhost","root","root"<span style="color: #000000;">);
   </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$link</span><span style="color: #000000;">){
    </span><span style="color: #0000ff;">echo</span> "db connect success!"<span style="color: #000000;">;
   }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
    </span><span style="color: #0000ff;">echo</span> "db connect failed!"<span style="color: #000000;">;
   }
  </span>?>
  <br>
  <?php <span style="color: #008080;">phpinfo</span>();?>
 </body>
</html> 
Copy after login

      双击上述 start.bat脚本文件,键盘输入 1后按下回车键

        如下图所示,则表示nginx-php启动成功

      

      打开浏览器,在浏览器地址栏内键入:http://127.0.0.1,如看到如下图所示界面,说明wnmp配置成功。

      

  至此,WNMP配置成功!

  下一篇文章会是 如何在nginx上配置Virtual Hosts 和Openssl的设定。

  这是小弟首次发帖,如果有不足之处还请指正,欢迎评论交流。

 

  参考链接:http://jingyan.baidu.com/article/636f38bb408ee4d6b84610b9.html

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!