Windows下配置nginx+php(wnmp)

转:https://www.cnblogs.com/liangxiaofeng/p/5975393.html

第一部分:准备工作

1.下载所需软件:

  1) nginx : http://nginx.org/en/download.html ,我下载的是nginx稳定版本(Stable version) nginx/Windows-1.16.0

  2) php : https://windows.php.net/download/ ,我下载的是PHP 7.3 (7.3.6) VC15 x64 Thread Safe (2019-May-29 15:24:36) Zip [24.33MB]

  3) mysql : https://dev.mysql.com/downloads/mysql/5.7.html#downloads ,我下载的是mysql-5.7.26-winx64

2.安装目录:

D盘根目录创建两个文件夹:

  1) nginx -> nginx/software -> nginx/software/nginx-1.16.0

  2) php -> php/software -> php/software/php-7.3.6-Win32-VC15-x64

将nginx程序解压安装在nginx/software/nginx-1.16.0/ 目录下

将php程序解压安装在 php/software/php-7.3.6-Win32-VC15-x64/ 目录下

第二部分:安装nginx

运行 nginx/software/nginx-1.16.0/nginx.exe 程序,查看任务管理器,发现nginx进程

注意:该网站的默认目录在 nginx/software/nginx-1.16.0/html 下

测试是否启动nginx。打开浏览器访问http://localhost 或 http://127.0.0.1,看看是否出现“Welcome to nginx!”,出现的证明已经启动成功了。出现以上界面nginx就安装成功了,可能会不成功,列出不成功可能的原因:没有启动的话,看看80端口有占用没

第三部分:安装php

将php7.3.6解压到 php/software/php-7.3.6-Win32-VC15-x64/ 目录下,该目录下有两个文件

  1) php.ini-development

  2) php.ini-production

复制 php.ini-development 重名修改为 php.ini 

搜索 extension_dir,找到: extension_dir = "ext" 先去前面的分号再改为 

extension_dir = "D:\php\software\php-7.3.6-Win32-VC15-x64\ext"

注意:windows系统下 "" 一定要加转义字符 "" ,即为 "\"。不然一些 " " 都会出现问题。

搜索 date.timezone ,找到:;date.timezone = 先去前面的分号再改为 date.timezone = Asia/Shanghai

date.timezone = Asia/Shanghai

搜索 enable_dl ,找到:enable_dl = Off 改为 enable_dl = On

enable_dl = On

搜索 cgi.force_redirect ;cgi.force_redirect = 1 先去前面的分号再改为 cgi.force_redirect = 0

cgi.force_redirect = 0

搜索“fastcgi.impersonate”,找到: ;fastcgi.impersonate = 1 去掉前面的分号

fastcgi.impersonate = 1

搜索“cgi.rfc2616_headers”,找到:;cgi.rfc2616_headers = 0 先去前面的分号再改为 cgi.rfc2616_headers = 1

cgi.rfc2616_headers = 1

搜索“php_mysql”,找到:”extension=php_mysql.dll和extension=php_mysqli.dll  去掉前面的“;”extension=php_mysql.dll和extension=php_mysqli.dll   (支持MYSQL数据库)

其他的配置请按照自己的需求更改。

;extension=bz2
extension=curl
;extension=fileinfo
extension=gd2
;extension=gettext
;extension=gmp
;extension=intl
;extension=imap
;extension=interbase
;extension=ldap
;extension=mbstring
;extension=exif      ; Must be after mbstring as it depends on it
extension=mysqli
;extension=oci8_12c  ; Use with Oracle Database 12c Instant Client
;extension=odbc
extension=openssl
;extension=pdo_firebird
extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
;extension=pdo_pgsql
;extension=pdo_sqlite
;extension=pgsql
;extension=shmop

第四部分:nginx解析php

这里主要讲nginx配置启动php,以cgi运行php

编辑 nginx/software/nginx-1.16.0/conf/nginx.conf

1.修改大概第43~45行之间的

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

修改网站文件的路径,以及添加index.php的默认页。改为:

        location / {
            root   "D:\nginx\project\default";
            index  index.html index.htm index.php;
        }

注意:windows系统下 "" 一定要加转义字符 "" ,即为 "\"。不然一些 " " 都会出现问题。

2.支持php的设置,修改大概在第63-71行的

        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;
        }

先将前面的“#”去掉,同样将root  html;改为root  C:/wnmp/nginx-1.5.8/html;。再把“/scripts”改为“$document_root”,这里的“$document_root”就是指前面“root”所指的站点路径,这是改完后的:

        location ~ .php$ {
           root           "D:\nginx\project\default";
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
        }

注意:windows系统下 "" 一定要加转义字符 "" ,即为 "\"。不然一些 " " 都会出现问题。

开启php-cgi进程,试运行以及编辑运行配置文件

> D:\php\software\php-7.3.6-Win32-VC15-x64\php-cgi.exe -b 127.0.0.1:9000 -c D:\php\software\php-7.3.6-Win32-VC15-x64\php.ini

注意:windows系统下 "" 一定要加转义字符 "" ,即为 "\"。不然一些 " " 都会出现问题。

重新运行nginx.exe。D:\nginx\project\default 下新建一个phpinfo.php,

<?php phpinfo(); ?>

访问http://localhost/phpinfo.php

或者http://127.0.0.1/phpinfo.php

出现如下的信息就说明php已经成功安装:

下载一个RunHiddenConsole.exe,百度网盘

开启php-cgi和nginx.exe,保存为start.bat

@echo off
echo Starting PHP FastCGI...
C:\wnmp\nginx\RunHiddenConsole.exe C:\wnmp\PHP\php-cgi.exe -b 127.0.0.1:9000-c D:\PHP\php.ini
echo Starting nginx...
C:\wnmp\nginx\RunHiddenConsole.exe D:\nginx\nginx.exe -p D:\nginx

注意:windows系统下 "" 一定要加转义字符 "" ,即为 "\"。不然一些 " " 都会出现问题。

停止php-cgi和nginx.exe,保存为stop.bat

@echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
exit

 https://www.inbeijing.org/archives/tag/runhiddenconsole

第五部分:安装mysql

前往 下载MySQL安装程序  下载安装程序,这里我下载的是网络版本

下载window的zip解压目录如上,这时点击 我的电脑->管理->服务 找不到 MySQL 服务

打开cmd到mysql的bin输入net start mysql时,cmd提示 服务名无效,于是我打开控制主板页面,进入管理工具下的“服务”查看MySQL,发现其不存在。所以我接着在cmd输入 mysqld.exe -install,然而遇到权限不足,于是我恍然大悟,应该要以管理员身份打开!

“开始”+x,以管理员身份打开cmd窗口到mysql的bin目录下,输入,mysqld.exe -install。

在windos 的cmd下安装mysql

在mysql的bin目录下面执行: mysqld --install

报错:

信息如下:

Install/Remove of the Service Denied

 

解决办法:

打开cmd.exe程序的时候选择“用管理员身份打开”。

开启mysql服务:

net start mysql

关闭mysql服务:

net stop mysql

命令行登录mysql:

mysql -hlocalhost -uroot -p

操作mysql:

show databases;

如果要退出mysql数据库,输入exit;回车

多个站点的配置

有时候你想在一台服务器上为不同的域名运行不同的站点。

  1) one.vhost.com

  2) two.vhost.com

你可以把两个域名的IP都解析到你的服务器上,但是没法在Nginx的根目录里同时运行两个不同的网站。

这时候,你就需要使用虚拟目录了。

  1) project/one_vhost_com 来存入 one.vhost.com的文件

  2) project/two_vhost_com 来存入 two.vhost.com的文件

下面我们就开始配置了:

1. 在Nginx配置目录下,创建一个”vhost”目录。本例假设Nginx是默认安装,配置目录在

nginx/nginx-1.16.0/conf/vhost

2.创建两个配置文件

  1) one.vhost.com.conf

  2) two.vhost.com.conf

3. 由于类似,这里罗列出 one.vhost.com.conf 的配置(nginx/nginx-1.16.0/conf/vhost/one.vhost.com.conf)

    server {
        listen       80;
        server_name  one.vhost.com;
        root   "D:\wnmp\project\one_vhost_com";
        index  index.html index.htm index.php;

        location / {
            try_files $uri @rewrite;
            location ~ .php$ {
                try_files $uri =404;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_split_path_info  ^((?U).+.php)(/?.+)$;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param  PATH_INFO  $fastcgi_path_info;
                fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
                include        fastcgi_params;
            }
        }

        location @rewrite {
          #rewrite ^/admin.php(.*)$ /admin.php?s=$1 last;
          rewrite ^/index.php(.*)$ /index.php?s=$1 last;
          rewrite . /index.php?s=$uri last;
        }
    }

注意:windows系统下 "" 一定要加转义字符 "" ,即为 "\"。不然一些 " " 都会出现问题。

5. 在nginx.conf配置

http {
    ...
    include vhost/*.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   html;
        #     index  index.html index.htm;
        # }
        location / {
            root   "D:\wnmp\project\default";
            index  index.html index.htm 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           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        location ~ .php$ {
           root           "D:\wnmp\project\default";
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           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;
    #    }
    #}

    # 在Nginx上配置多个站点
    include vhost/*.conf;
}
View Code

顺便大家可以参考phpStudy来进行配置:

#  power by www.php.cn
#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;
    #tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 128k;
  fastcgi_buffers 4 128k;
  fastcgi_busy_buffers_size 256k;
  fastcgi_temp_file_write_size 256k;

  #gzip  on;
  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 32k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
  gzip_disable "MSIE [1-6].";

  server_names_hash_bucket_size 128;
  client_max_body_size     100m; 
  client_header_buffer_size 256k;
  large_client_header_buffers 4 256k;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        root    "D:/phpStudy/PHPTutorial/WWW";
        location / {
           root    "D:/cms/public";
           index  index.html index.htm index.php;
           
           try_files $uri  @rewrite;
           
           location ~ .php$ {
             try_files $uri =404;
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             include        fastcgi_params;
           }
        }
        
        location @rewrite {
          #rewrite ^/admin.php(.*)$ /admin.php?s=$1 last;
          rewrite ^/index.php(.*)$ /index.php?s=$1 last;
          rewrite . /index.php?s=$uri last;
        }

        #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;
        }
        
        location ~ /.(svn|git) {
          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;
    #    server_name  localhost;

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

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

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

include vhosts.conf;

}
View Code

禁止访问小技巧

假如你的Nginx根目录设在”/home/user/www”,你想阻止别人通过”http://IP地址/blog”或”http://IP地址/forum”来访问你的站点,最简单的方法就是禁止IP地址访问。方法如下:

1.打开Nginx网站默认配置文件,记得先备份,将所有内容删除,只留以下配置

server {
    listen 80 default_server;
    server_name _;
    return 404;
}

2.重启Nginx后,别人将无法通过IP地址访问网站了

如果你不想禁止IP地址访问整个目录,只是要防止别人通过IP访问你的博客和论坛。那就需要禁止”/blog”和”/forum”的目录访问。

1.打开Nginx网站默认配置文件,同上面一样,记得先备份

2.在”server { }”部分加上以下配置

location ^~ /blog/ {
    deny all;
}
location ^~ /forum/ {
    deny all;
}

3.重启Nginx即可

Windows下用Nginx配置https服务器

参考地址:https://www.cnblogs.com/chasewade/p/7661290.html

一、安装OpenSSL

先到http://slproweb.com/products/Win32OpenSSL.html 去下载OpenSSL(根据系统选择32位或者64位版本下载安装)。

然后安装在C:OpenSSL-Win64下。

然后配置环境变量。在系统环境变量中添加环境变量:

变量名:OPENSSL_HOME

变量值:C:OpenSSL-Win64in;

(变量值为OPENSSL安装位置下的bin目录)

并在Path变量结尾添加一条: %OPENSSL_HOME%

二、安装Nginx

参考前面

三、生成证书  

1、首先在Nginx安装目录中创建ssl文件夹用于存放证书。比如我的文件目录为 C: ginxssl

在控制台中执行:

cd D:wnmp
ginx
ginx-1.16.0ssl

2、创建私钥

在命令行中执行命令:

# buduhuisi文件名是自己随便起即可
openssl genrsa -des3 -out buduhuisi.key 1024

输入密码后,再次重复输入确认密码。记住此密码,后面会用到。

3、创建csr证书

在命令行中执行命令:

openssl req -new -key buduhuisi.key -out buduhuisi.csr

其中key文件为刚才生成的文件。

执行上述命令后,需要输入一系列的信息。输入的信息中最重要的为Common Name,这里输入的域名即为我们要使用https访问的域名 ,比如我输入的是localhost。其它的内容随便填即可。

以上步骤完成后,ssl文件夹内出现两个文件:buduhuisi.csr 和 buduhuis.key

4、去除密码。

在加载SSL支持的Nginx并使用上述私钥时除去必须的口令,否则会在启动nginx的时候需要输入密码。

复制buduhuisi.key并重命名为buduhuisi.key.org。

在命令行中执行如下命令以去除口令:

openssl rsa -in buduhuisi.key.org -out buduhuisi.key

然后输入密码,这个密码就是上文中在创建私钥的时候输入的密码。

5、生成crt证书

在命令行中执行此命令:

openssl x509 -req -days 365 -in buduhuisi.csr -signkey buduhuisi.key -out buduhuisi.crt

至此,证书生成完毕。我们发现,ssl文件夹中一共生成了4个文件。下面,配置https服务器的时候,我们需要用到的是其中的buduhuisi.crt和buduhuisi.key这两个文件。

四、修改Nginx的nginx.conf配置文件

我的这个文件在C: ginxconf目录下。用任意一个编辑器(如Sublime Text之类)打开这个nginx.conf文件。

找到HTTPS server配置的那一段(即包含有listen 443 ssl配置那一段)。我们发现这段代码被注释掉了。所以,首先我们把该段代码前面的#号去掉。然后分别修改其中的ssl_certificate和ssl_certificate_key配置项为刚才所生成的buduhuisi.crt和buduhuisi.key这两个文件的目录。并配置server_name为localhost。修改后的该段配置如下:

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

    #    ssl_certificate      "D:\wnmp\nginx\nginx-1.16.0\ssl\buduhuisi.crt";
    #    ssl_certificate_key  "D:\wnmp\nginx\nginx-1.16.0\ssl\buduhuisi.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;
    #    }
    # }

这里如果需要给虚拟主机配置,则配置如下

    server {
        listen       80;
        
        # https
        listen       443;

        server_name  one.vhost.com;
        root   "D:\wnmp\project\one_vhost_com";
        index  index.html index.htm index.php;

        # https
        ssl on;
        ssl_certificate      "D:\wnmp\nginx\nginx-1.16.0\ssl\buduhuisi.crt";
        ssl_certificate_key  "D:\wnmp\nginx\nginx-1.16.0\ssl\buduhuisi.key";

        location / {
            try_files $uri @rewrite;
            location ~ .php$ {
                try_files $uri =404;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_split_path_info  ^((?U).+.php)(/?.+)$;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param  PATH_INFO  $fastcgi_path_info;
                fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
                include        fastcgi_params;
            }
        }

        location @rewrite {
          #rewrite ^/admin.php(.*)$ /admin.php?s=$1 last;
          rewrite ^/index.php(.*)$ /index.php?s=$1 last;
          rewrite . /index.php?s=$uri last;
        }
    }

注意一下那两个证书的文件路径的写法。

五、Nginx的常用操作

在继续后面的内容之前,先简单介绍下Windows命令行中操作Nginx的几个常用的语句:

start nginx               # 启动Nginx
nginx.exe -s stop         # 快速停止Nginx,可能并不保存相关信息
nginx.exe -s quit         # 完整有序的停止Nginx,并保存相关信息
nginx.exe -s reload       # 重新载入Nginx,当配置信息修改,需要重新载入这些配置时使用此命令。
nginx.exe -s reopen       # 重新打开日志文件
nginx -v                  # 查看Nginx版本

因为修改了配置文件,所以需要退出控制台,并重新打开一个控制台。执行如下命令:

cd c:
ginx
nginx.exe -s quit
start nginx

即退出Nginx,然后再重新启动它。这时候,在浏览器地址栏输入https://localhost并回车。

这时候,你可能看到“您的连接不是私密连接”的提示,单击页面中的“高级”,并接着单击“继续前往m.test.com(不安全)”,就可以看到Nginx的欢迎界面了。说明https服务器已经配置成功了。

如果你只想用https://localhost访问这个https服务器,那么下面的内容你就不用接着往下看了。

但是,也许你可能还想要用一个别的域名(例如:https://m.test.com)来访问这个服务器。那么怎么做呢?这就需要继续往下看了。

六、修改hosts配置,实现域名映射

要想用别的域名来访问上文配置好的https服务器,也很简单,修改hosts配置就可以了。你可以到这里下载一个hosts管理工具——SwitchHosts。安装号好之后,以管理员身份运行它。并添加上一个hosts项:

127.0.0.1  m.test.com

这样,你就可以通过https://m.test.com来访问配置好的https服务器了。

顺便提一下,关于Mac环境下如何映射一个http路径到一个https路径,可以通过Charles工具来实现。参见这里

原文地址:https://www.cnblogs.com/jiangxiaobo/p/11107563.html