SunSeekerX

这条路才刚刚开始啊~

wnmp环境搭建

Coding Sundry 0 评 111 度

wnmp环境搭建

我的服务器只有C盘,所有文件都放在C盘,在C盘新建Wnmp目录

1:MariaDB

  1. 官网下载直接安装即可下载地址https://downloads.mariadb.org
  2. 安装路径:C:\Wnmp\MariaDB 10.4\
  3. 配置默认密码,以及设置是否开启远程链接(远程连接需要防火墙同时打开端口)
  4. 然后是配置服务名
  5. 安装完成,自带了一个HeidiSQL可以用来测试是否安装并运行,安装完成会跟随开机一起启动,可以不用额外的配置

2:nginx

  1. 下载地址:http://nginx.org/en/download.html
  2. 解压到C:\Wnmp\nginx-1.17.0
  3. 在php讲解配置nginx
  4. 新建3个bat启动脚本放在C:\Wnmp\nginx-1.17.0方便管理

start.bat

start nginx

restart.bat

nginx.exe -s reload 

stop.bat

nginx.exe -s quit  

3:php

  1. 下载地址:https://windows.php.net/download#php-7.3,nginx选择非线程安全版本即可
  2. 解压到C:\Wnmp\php
  3. 防止进程关闭RunHiddenConsole.exe
  4. 们将php.ini-production(相当于旧版的php.ini-recommended)后缀改为ini,用notepad++编辑,找到
extension_dir = "./ext"

这一行,改为

extension_dir = "C:/wnmp/php/ext"

然后把后面的

;extension=php_mysql.dll
;extension=php_pdo_mysql.dll
;cgi.fix_pathinfo=1

前的分号删除掉

extension=php_mysql.dll
extension=php_pdo_mysql.dll
cgi.fix_pathinfo=1

网上教程上的

;extension=php_mysql.dll

在php7中也没有了,其实在之前的Mariadb中,sqli已经强制性了,使用sql会warning(不确定是不是真的),mysqli中的i意为improvement

另外php_pdo_mysql.dll的扩展一定要打开

配置nginx

  1. 新建一个php_processes.conf内容如下

upstream php_processes {

   server 127.0.0.1:9000 weight=1;

}


3. nodepad++打开配置文件`C:\Wnmp\nginx-1.17.0\conf\nginx.conf`,所有配置如下

4. ```nginx
#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 {
    # Max value 16384
    worker_connections  8192;
    # Accept multiple connections
    multi_accept on;
}


http {
    include php_processes.conf;
    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;
    ssl_session_timeout 10m;
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1 SSLv3;
    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AES:RSA+3DES:!ADH:!AECDH:!MD5:!DSS; 
    ssl_prefer_server_ciphers on;
    gzip  on;
    # http server
    
    # Begin HTTP Server
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        ## Root and index files.
        root C:\Wnmp\www;
        index  index.php index.html index.htm;
        
        ## If no favicon exists return a 204 (no content error).
        location = /favicon.ico {
            try_files $uri =204;
            log_not_found off;
            access_log off;
        }
        
        ## Don't log robots.txt requests.
        location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
        }
        
        ## Try the requested URI as files before handling it to PHP.
    
        location /app/blog/ {
            if (!-e $request_filename) {
                rewrite ^(.*)$ /foldername/index.php$1 last;
            }
        }
        
        location ~ .*\.php(\/.*)*$ {
            try_files  $uri =404;
            fastcgi_pass   php_processes;
            fastcgi_index  index.php;
            set $path_info "";
            set $real_script_name $fastcgi_script_name;
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                    set $real_script_name $1;
                    set $path_info $2;
            }
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
            include        fastcgi_params;
        }


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

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

}

这里有个很大的坑就是我安装的博客并不在根目录下而是在根目录下的app/blog/下面

typecho需要开启伪静态,网上大都是这个样子的

if (!-e $request_filename) {
    rewrite ^(.*)$ /foldername/index.php$1 last;
}

若php应用安装在根目录下没有问题,但是不在根目录下会造成只有首页能访问,无法访问后台和文章界面

正确的应该修改成如下

location /app/blog/ {
    if (!-e $request_filename) {
        rewrite ^(.*)$ /foldername/index.php$1 last;
    }
}

启动php

C:\Wnmp新建start-php.bat

内容如下

RunHiddenConsole C:/wnmp/php/php-cgi.exe -b 127.0.0.1:9000 -c C:/wnmp/php/php.ini

启动

  1. 先启动C:\Wnmp下面的start-php.bat
  2. 然后启动nginx
  3. 搭建工作就完成的差不多了,现在到www目录下,新建一个phpinfo.php的探针,内容如下:
<?php
   phpinfo();
?>

打开浏览器访问http://localhost/phpinfo.php,如果看到php探针信息,就说明搭建成功了。

18266227-d3e7946e40e352f5.png

总结

在搭建的时候最坑的就是伪静态路径那个问题,百度谷歌了很多次没有解决,后面睡觉前手机上看到一篇文章php项目路径不在根目录还需要额外的配置才解决。

总之集成环境很多,像wnmp,xampp,都可以实现类似的功能,但自己搭建多了一个实践的过程。

知其然,才能知其所以然

参考

搭建wnmp的时候参考了八个比特的博客

原文链接:WNMP搭建笔记

版权声明:本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可

WIFI突然断掉又立马连接,WiFi很慢等问题