Windows下搭建Nginx与PHP环境

软件版本与安装环境

软件版本

php:php7.2.11 VC15 x64 Non Thread Safe

nginx:nginx/Windows-1.14.0

安装环境

windows10专业版

 

搭建流程

1.下载php并且配置环境变量

2.下载nginx

3.todo

 

PHP下载与配置

  1. 根据你的需求下载php所需的版本至本地,然后解压,我解压后的目录为C:\backfirecs\softs\work\web\php\php7.2\php-7.2.10-nts-Win32-VC15-x64
  2. 配置环境变量

       在path环境变量中添加php的环境变量

Windows下搭建Nginx与PHP环境

 3.修改配置文件

将php解压目录中的php.ini-development文件复制一份在相同的目录中并且改名php.ini

Windows下搭建Nginx与PHP环境

修改配置文件php.ini,首先修改extension_dir="ext",然后修改cgi.fix_pathinfo=1

Windows下搭建Nginx与PHP环境

Windows下搭建Nginx与PHP环境

4.检查php是否正常

打开控制台输入php  -v如下图所示控制台显示了版本信息则正常

Windows下搭建Nginx与PHP环境

 

Nginx的安装与配置

1.下载并解压nginx压缩包,解压后如下图

Windows下搭建Nginx与PHP环境

2.运行nginx并且检查是否正常

双击nginx.exe即可启动nginx,可在任务管理器中检查是否启动成功,关闭nginx服务的话直接结束结束进程或者打开cmd进入nginx的安装目录执行./nginx.exe  -s stop也可以关闭服务。

Windows下搭建Nginx与PHP环境

启动成功后我们打开浏览器在地质栏输入localhost,如下图所示则nginx正常

Windows下搭建Nginx与PHP环境

 

3.配置nginx

修改conf/nginx.conf文件如下,是修改的部分加了中文注释


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   C:/backfirecs/documents/work/www;#代码根目录
            index  index.html index.htm index.php;#添加index.php
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           C:/backfirecs/documents/work/www;#代码根目录
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;#修改“/scripts”为“$document_root”
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

4.启动php-cgi

进入php安装目录执行命令 .\php-cgi.exe -b 127.0.0.1:9000 -c php.ini

Windows下搭建Nginx与PHP环境

5.启动nginx

进入nginx安装目录双击nginx.exe启动nginx

 

检查环境是否搭建成功

1.在工程根目录下(我的目录是C:/backfirecs/documents/work/www)添加一个文件叫做index.php,内容为

<?php
    echo phpinfo();

2.打开浏览器,在地址栏输入localhost/index.php,如果网页返回php信息则代表环境搭建成功

Windows下搭建Nginx与PHP环境